Class: TT::JSON
- Inherits:
-
Object
show all
- Includes:
- Enumerable
- Defined in:
- TT_Lib2/json.rb
Overview
Sortable Hash that preserves the insertion order. Prints out JSON strings
of the content.
Based of Bill Kelly's InsertOrderPreservingHash
Instance Method Summary
collapse
Constructor Details
#initialize(*args, &block) ⇒ JSON
Returns a new instance of JSON
23
24
25
26
27
28
29
30
31
|
# File 'TT_Lib2/json.rb', line 23
def initialize(*args, &block)
if args.size == 1 && args[0].is_a?(Hash)
@h = args[0].dup
@ordered_keys = @h.keys
else
@h = 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
124
125
126
|
# File 'TT_Lib2/json.rb', line 124
def method_missing(*args)
@h.send(*args)
end
|
Instance Method Details
#[]=(key, val) ⇒ Object
41
42
43
44
|
# File 'TT_Lib2/json.rb', line 41
def []=(key, val)
@ordered_keys << key unless @h.has_key? key
@h[key] = val
end
|
#clear ⇒ Object
81
82
83
84
|
# File 'TT_Lib2/json.rb', line 81
def clear
@ordered_keys.clear
@h.clear
end
|
#delete(k, &block) ⇒ Object
87
88
89
90
|
# File 'TT_Lib2/json.rb', line 87
def delete(k, &block)
@ordered_keys.delete k
@h.delete(k, &block)
end
|
#delete_if(&block) ⇒ Object
101
102
103
104
|
# File 'TT_Lib2/json.rb', line 101
def delete_if(&block)
reject!(&block)
self
end
|
#each ⇒ Object
Also known as:
each_pair
47
48
49
|
# File 'TT_Lib2/json.rb', line 47
def each
@ordered_keys.each {|k| yield(k, @h[k])}
end
|
#each_key ⇒ Object
58
59
60
|
# File 'TT_Lib2/json.rb', line 58
def each_key
@ordered_keys.each {|k| yield k}
end
|
#each_value ⇒ Object
53
54
55
|
# File 'TT_Lib2/json.rb', line 53
def each_value
@ordered_keys.each {|k| yield(@h[k])}
end
|
#initialize_copy(source) ⇒ Object
34
35
36
37
38
|
# File 'TT_Lib2/json.rb', line 34
def initialize_copy(source)
super
@h = @h.dup
@ordered_keys = @ordered_keys.dup
end
|
#key?(key) ⇒ Boolean
Also known as:
has_key?, include?, member?
63
64
65
|
# File 'TT_Lib2/json.rb', line 63
def key?(key)
@h.key?(key)
end
|
#keys ⇒ Object
71
72
73
|
# File 'TT_Lib2/json.rb', line 71
def keys
@ordered_keys
end
|
#merge!(hash) ⇒ Object
107
108
109
110
111
112
113
114
115
|
# File 'TT_Lib2/json.rb', line 107
def merge!(hash)
hash.each { |key, value|
if @h.key?(key)
@h[key] = value
else
self[key] = value
end
}
end
|
#reject! ⇒ Object
93
94
95
96
97
98
|
# File 'TT_Lib2/json.rb', line 93
def reject!
del = []
each_pair {|k,v| del << k if yield k,v}
del.each {|k| delete k}
del.empty? ? nil : self
end
|
#to_hash ⇒ Hash
130
131
132
|
# File 'TT_Lib2/json.rb', line 130
def to_hash
@h.dup
end
|
#to_s(format = false) ⇒ Object
Also known as:
inspect
Compile JSON Hash into a string.
137
138
139
140
141
142
143
144
145
|
# File 'TT_Lib2/json.rb', line 137
def to_s(format=false)
arr = self.map { |k,v|
key = ( k.is_a?(Symbol) ) ? k.to_s.inspect : k.inspect
value = TT::Javascript.to_js(v, format)
"#{key}: #{value}"
}
str = (format) ? arr.join(",\n\t") : arr.join(", ")
return (format) ? "{\n\t#{str}\n}\n" : "{#{str}}"
end
|
#values ⇒ Object
76
77
78
|
# File 'TT_Lib2/json.rb', line 76
def values
@ordered_keys.map {|k| @h[k]}
end
|