Class: SKUI::JSON
- Inherits:
-
Object
show all
- Includes:
- Enumerable
- Defined in:
- src/SKUI/json.rb
Overview
Sortable Hash that preserves the insertion order. When converted to strings it output a JavaScript JSON string that can be used in WebDialogs for instance.
Based of Bill Kelly’s InsertOrderPreservingHash
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(*args, &block) ⇒ JSON
Returns a new instance of JSON.
16
17
18
19
20
21
22
23
24
|
# File 'src/SKUI/json.rb', line 16
def initialize( *args, &block )
if args.size == 1 && args[0].is_a?( Hash )
@hash = args[0].dup
@ordered_keys = @hash.keys
else
@hash = Hash.new( *args, &block )
@ordered_keys = []
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args) ⇒ Object
117
118
119
|
# File 'src/SKUI/json.rb', line 117
def method_missing( *args )
@hash.send( *args )
end
|
Class Method Details
.object_to_js(object, format = false) ⇒ String
Converts given Ruby object to a JavaScript string.
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
# File 'src/SKUI/json.rb', line 151
def self.object_to_js( object, format = false )
if object.is_a?( self )
object.to_s( format )
elsif object.is_a?( Hash )
self.new( object ).to_s( format )
elsif object.is_a?( Symbol )
object.inspect.inspect
elsif object.is_a?( Regexp )
o = object.options
i = o & Regexp::EXTENDED == Regexp::EXTENDED ? 'i' : '' i = o & Regexp::IGNORECASE == Regexp::IGNORECASE ? 'i' : ''
m = o & Regexp::MULTILINE == Regexp::MULTILINE ? 'm' : ''
"/#{object.source}/#{i}#{m}"
elsif object.nil?
'null'
elsif object.is_a?( Array )
data = object.map { |obj| object_to_js( obj, format ) }
"[#{data.join(',')}]"
elsif object.is_a?( Geom::Point3d )
"new Point3d( #{object.to_a.join(', ')} )"
elsif object.is_a?( Geom::Vector3d )
"new Vector3d( #{object.to_a.join(', ')} )"
elsif object.is_a?( Sketchup::Color )
"new Color( #{object.to_a.join(', ')} )"
elsif object.respond_to?( :to_js )
object.to_js
else
object.inspect
end
end
|
Instance Method Details
#[]=(key, value) ⇒ Object
34
35
36
37
|
# File 'src/SKUI/json.rb', line 34
def []=( key, value )
@ordered_keys << key unless @hash.has_key?( key )
@hash[key] = value
end
|
#clear ⇒ Object
40
41
42
43
|
# File 'src/SKUI/json.rb', line 40
def clear
@hash.clear
@ordered_keys.clear
end
|
#delete(key, &block) ⇒ Object
86
87
88
89
|
# File 'src/SKUI/json.rb', line 86
def delete( key, &block )
@ordered_keys.delete( key )
@hash.delete( key, &block )
end
|
#delete_if(&block) ⇒ Object
100
101
102
103
|
# File 'src/SKUI/json.rb', line 100
def delete_if( &block )
reject!( &block )
self
end
|
#each ⇒ Object
Also known as:
each_pair
46
47
48
|
# File 'src/SKUI/json.rb', line 46
def each
@ordered_keys.each { |key| yield( key, @hash[key] ) }
end
|
#each_key ⇒ Object
57
58
59
|
# File 'src/SKUI/json.rb', line 57
def each_key
@ordered_keys.each { |key| yield key }
end
|
#each_value ⇒ Object
52
53
54
|
# File 'src/SKUI/json.rb', line 52
def each_value
@ordered_keys.each { |key| yield( @hash[key] ) }
end
|
#initialize_copy(source) ⇒ Object
27
28
29
30
31
|
# File 'src/SKUI/json.rb', line 27
def initialize_copy( source )
super
@hash = @hash.dup
@ordered_keys = @ordered_keys.dup
end
|
#key?(key) ⇒ Boolean
Also known as:
has_key?, include?, member?
62
63
64
|
# File 'src/SKUI/json.rb', line 62
def key?( key )
@hash.key?( key )
end
|
#keys ⇒ Object
70
71
72
|
# File 'src/SKUI/json.rb', line 70
def keys
@ordered_keys
end
|
#merge!(hash) ⇒ Object
106
107
108
109
110
111
112
113
114
|
# File 'src/SKUI/json.rb', line 106
def merge!( hash )
hash.each { |key, value|
if @hash.key?( key )
@hash[key] = value
else
self[key] = value
end
}
end
|
#reject! ⇒ Object
92
93
94
95
96
97
|
# File 'src/SKUI/json.rb', line 92
def reject!
del = []
each_pair { |key, value| del << key if yield( key, value ) }
del.each { |key| delete( key ) }
del.empty? ? nil : self
end
|
#to_hash ⇒ Hash
123
124
125
|
# File 'src/SKUI/json.rb', line 123
def to_hash
@hash.dup
end
|
#to_s(format = false) ⇒ String
Also known as:
inspect
Compile JSON Hash into a string.
133
134
135
136
137
138
139
140
141
|
# File 'src/SKUI/json.rb', line 133
def to_s( format = false )
data = self.map { |key, value|
json_key = ( key.is_a?(Symbol) ) ? key.to_s.inspect : key.inspect
json_value = self.class.object_to_js( value, format )
"#{json_key}: #{json_value}"
}
json_string = (format) ? data.join(",\n\t") : data.join(", ")
return (format) ? "{\n\t#{json_string}\n}\n" : "{#{json_string}}"
end
|
#values ⇒ Object
75
76
77
|
# File 'src/SKUI/json.rb', line 75
def values
@ordered_keys.map { |key| @hash[key] }
end
|