Class: Mikunyan::ObjectValue

Inherits:
Object
  • Object
show all
Defined in:
lib/mikunyan/object_value.rb

Overview

Class for representing decoded object

Direct Known Subclasses

BaseObject

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, endian, value = nil) ⇒ ObjectValue

Constructor

Parameters:

  • name (String)

    object name

  • type (String)

    object type name

  • endian (Symbol)

    endianness

  • value (Object) (defaults to: nil)

    object



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

Parameters:

  • name (String)

    key

Returns:

  • (Object)

    value



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

#attrHash<String,Mikunyan::ObjectValue>

Returns the current value of attr.

Returns:



11
12
13
# File 'lib/mikunyan/object_value.rb', line 11

def attr
  @attr
end

#endianSymbol

endianness

Returns:

  • (Symbol)

    the current value of endian



11
12
13
# File 'lib/mikunyan/object_value.rb', line 11

def endian
  @endian
end

#is_structBoolean

Returns the current value of is_struct.

Returns:

  • (Boolean)

    the current value of is_struct



11
12
13
# File 'lib/mikunyan/object_value.rb', line 11

def is_struct
  @is_struct
end

#nameString

object name

Returns:

  • (String)

    the current value of name



11
12
13
# File 'lib/mikunyan/object_value.rb', line 11

def name
  @name
end

#typeString

object type name

Returns:

  • (String)

    the current value of type



11
12
13
# File 'lib/mikunyan/object_value.rb', line 11

def type
  @type
end

#valueObject

object

Returns:

  • (Object)

    the current value of value



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

Parameters:

  • key (Integer, String) (defaults to: nil)

    index or key

Returns:

  • (Object)

    value



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

Parameters:

  • name (String)

    key

  • value (Object)

    value

Returns:

  • (Object)

    value



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

Returns:

  • (Boolean)


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

Parameters:

  • key (String)

Returns:

  • (Boolean)


55
56
57
# File 'lib/mikunyan/object_value.rb', line 55

def key?(key)
  @attr.key?(key)
end

#keysArray

Return all keys

Returns:

  • (Array)

    list of 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?

Returns:

  • (Boolean)


89
90
91
# File 'lib/mikunyan/object_value.rb', line 89

def respond_to_missing?(symbol, _include_private)
  @attr.key?(symbol.to_s)
end

#simplifyObject

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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


36
37
38
# File 'lib/mikunyan/object_value.rb', line 36

def value?
  value && !value.is_a?(Array)
end