Class: StaticStruct
- Inherits:
-
Object
show all
- Defined in:
- lib/static_struct.rb
Instance Method Summary
collapse
Constructor Details
Returns a new instance of StaticStruct.
2
3
4
5
6
7
8
|
# File 'lib/static_struct.rb', line 2
def initialize(hash)
if !hash.is_a?(Hash)
raise "StaticStruct must be initialized with a hash."
end
@table = build(hash)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(mid, *args) ⇒ Object
10
11
12
|
# File 'lib/static_struct.rb', line 10
def method_missing(mid, *args)
value_of(mid)
end
|
Instance Method Details
#==(other) ⇒ Object
26
27
28
29
|
# File 'lib/static_struct.rb', line 26
def ==(other)
return false unless(other.is_a?(StaticStruct))
return @table == other.table
end
|
#[](key) ⇒ Object
14
15
16
|
# File 'lib/static_struct.rb', line 14
def [](key)
value_of(key)
end
|
#as_json(options = nil) ⇒ Object
39
40
41
|
# File 'lib/static_struct.rb', line 39
def as_json(options = nil)
to_h.as_json
end
|
#to_h ⇒ Object
31
32
33
|
# File 'lib/static_struct.rb', line 31
def to_h
Hash[@table.map {|k, v| [k.to_sym, build_hash(v)]}]
end
|
#to_json(options = nil) ⇒ Object
35
36
37
|
# File 'lib/static_struct.rb', line 35
def to_json(options = nil)
to_h.to_json
end
|
#value_of(key) ⇒ Object
18
19
20
21
22
23
24
|
# File 'lib/static_struct.rb', line 18
def value_of(key)
if !@table.has_key?(key.to_sym)
raise "Property #{key} was not found for StaticStruct##{object_id}."
end
@table[key.to_sym]
end
|