Class: Mikunyan::ObjectValue
- Inherits:
-
Object
- Object
- Mikunyan::ObjectValue
- Defined in:
- lib/mikunyan/object_value.rb
Overview
Class for representing decoded object
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attr ⇒ Hash<String,Mikunyan::ObjectValue>
The current value of attr.
-
#endian ⇒ Symbol
endianness.
-
#is_struct ⇒ Boolean
The current value of is_struct.
-
#name ⇒ String
object name.
-
#type ⇒ String
object type name.
-
#value ⇒ Object
object.
Instance Method Summary collapse
-
#[](key = nil) ⇒ Object
Return value of selected index or key.
-
#[]=(name, value) ⇒ Object
Set value of selected key.
-
#array? ⇒ Boolean
Return whether object is array or not.
-
#initialize(name, type, endian, value = nil) ⇒ ObjectValue
constructor
Constructor.
-
#key?(key) ⇒ Boolean
Return whether object contains key.
-
#keys ⇒ Array
Return all keys.
-
#method_missing(name, *args) ⇒ Object
Return value of called key.
-
#respond_to_missing?(symbol, _include_private) ⇒ Boolean
Implementation of respond_to_missing?.
-
#simplify ⇒ Object
Simplifies self, or serializes self with ruby primitive types.
-
#struct? ⇒ Boolean
Return whether object is struct or not.
-
#value? ⇒ Boolean
Return whether object is value or not.
Constructor Details
#initialize(name, type, endian, value = nil) ⇒ ObjectValue
Constructor
19 20 21 22 23 24 25 26 |
# File 'lib/mikunyan/object_value.rb', line 19 def initialize(name, type, endian, value = nil) @name = name @type = type @endian = endian @value = value @is_struct = false @attr = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
Return value of called key
83 84 85 86 |
# File 'lib/mikunyan/object_value.rb', line 83 def method_missing(name, *args) n = name.to_s @attr.key?(n) ? @attr[n] : super end |
Instance Attribute Details
#attr ⇒ Hash<String,Mikunyan::ObjectValue>
Returns the current value of attr.
11 12 13 |
# File 'lib/mikunyan/object_value.rb', line 11 def attr @attr end |
#endian ⇒ Symbol
endianness
11 12 13 |
# File 'lib/mikunyan/object_value.rb', line 11 def endian @endian end |
#is_struct ⇒ Boolean
Returns the current value of is_struct.
11 12 13 |
# File 'lib/mikunyan/object_value.rb', line 11 def is_struct @is_struct end |
#name ⇒ String
object name
11 12 13 |
# File 'lib/mikunyan/object_value.rb', line 11 def name @name end |
#type ⇒ String
object type name
11 12 13 |
# File 'lib/mikunyan/object_value.rb', line 11 def type @type end |
#value ⇒ Object
object
11 12 13 |
# File 'lib/mikunyan/object_value.rb', line 11 def value @value end |
Instance Method Details
#[](key = nil) ⇒ Object
Return value of selected index or key
62 63 64 65 66 67 68 69 70 |
# File 'lib/mikunyan/object_value.rb', line 62 def [](key = nil) if key.nil? @value elsif array? && key.is_a?(Integer) @value[key] else @attr[key] end end |
#[]=(name, value) ⇒ Object
Set value of selected key
76 77 78 |
# File 'lib/mikunyan/object_value.rb', line 76 def []=(name, value) @attr[name] = value end |
#array? ⇒ Boolean
Return whether object is array or not
30 31 32 |
# File 'lib/mikunyan/object_value.rb', line 30 def array? value&.is_a?(Array) end |
#key?(key) ⇒ Boolean
Return whether object contains key
55 56 57 |
# File 'lib/mikunyan/object_value.rb', line 55 def key?(key) @attr.key?(key) end |
#keys ⇒ Array
Return all keys
48 49 50 |
# File 'lib/mikunyan/object_value.rb', line 48 def keys @attr.keys end |
#respond_to_missing?(symbol, _include_private) ⇒ Boolean
Implementation of respond_to_missing?
89 90 91 |
# File 'lib/mikunyan/object_value.rb', line 89 def respond_to_missing?(symbol, _include_private) @attr.key?(symbol.to_s) end |
#simplify ⇒ Object
Simplifies self, or serializes self with ruby primitive types
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/mikunyan/object_value.rb', line 94 def simplify if @type == 'pair' [@attr['first'].simplify, @attr['second'].simplify] elsif @type == 'map' && @value.is_a?(Array) @value.map {|e| [e['first'].simplify, e['second'].simplify]}.to_h elsif is_struct @attr.transform_values(&:simplify) elsif @value.is_a?(Array) @value.map {|e| e.is_a?(ObjectValue) ? e.simplify : e} elsif @value.is_a?(ObjectValue) @value.simplify else @value end end |
#struct? ⇒ Boolean
Return whether object is struct or not
42 43 44 |
# File 'lib/mikunyan/object_value.rb', line 42 def struct? is_struct end |
#value? ⇒ Boolean
Return whether object is value or not
36 37 38 |
# File 'lib/mikunyan/object_value.rb', line 36 def value? value && !value.is_a?(Array) end |