Class: DZero::Segments::Base

Inherits:
Object
  • Object
show all
Includes:
Concerns::Parser, Concerns::Serializer
Defined in:
lib/dzero/segments/base.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concerns::Serializer

#to_json, #to_s

Methods included from Concerns::Parser

included

Constructor Details

#initialize(initial_hash = {}) ⇒ Base

Returns a new instance of Base.



65
66
67
68
69
70
71
72
73
# File 'lib/dzero/segments/base.rb', line 65

def initialize(initial_hash = {})
  @hash = {}

  unless self.class.segment_identifier.nil?
    self[:segment_identification] = self.class.segment_identifier
  end

  self.merge(initial_hash)
end

Class Attribute Details

.segment_id_to_klassObject

Returns the value of attribute segment_id_to_klass.



11
12
13
# File 'lib/dzero/segments/base.rb', line 11

def segment_id_to_klass
  @segment_id_to_klass
end

.segment_id_to_symbolObject

Returns the value of attribute segment_id_to_symbol.



10
11
12
# File 'lib/dzero/segments/base.rb', line 10

def segment_id_to_symbol
  @segment_id_to_symbol
end

.segment_identifierObject

Returns the value of attribute segment_identifier.



12
13
14
# File 'lib/dzero/segments/base.rb', line 12

def segment_identifier
  @segment_identifier
end

.symbolObject

Returns the value of attribute symbol.



13
14
15
# File 'lib/dzero/segments/base.rb', line 13

def symbol
  @symbol
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



75
76
77
# File 'lib/dzero/segments/base.rb', line 75

def hash
  @hash
end

Class Method Details

.build(initial_hash = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/dzero/segments/base.rb', line 43

def build(initial_hash = {})
  given_identifier = initial_hash[get_field_by_symbol(:segment_identification)]
  segment_klass    = segment_id_to_klass[given_identifier]
  if segment_klass
    segment_klass.new(initial_hash)
  else
    self.new(initial_hash)
  end
end

.field_id_to_symbolObject



22
23
24
# File 'lib/dzero/segments/base.rb', line 22

def field_id_to_symbol
  { 'AM' => :segment_identification }
end

.get_field_by_symbol(sym) ⇒ Object



26
27
28
# File 'lib/dzero/segments/base.rb', line 26

def get_field_by_symbol(sym)
  field_id_to_symbol.invert[sym]
end

.get_klass_by_identifier(identifier) ⇒ Object



39
40
41
# File 'lib/dzero/segments/base.rb', line 39

def get_klass_by_identifier(identifier)
  segment_id_to_klass[identifier] || self
end

.get_klass_by_symbol(sym) ⇒ Object



34
35
36
37
# File 'lib/dzero/segments/base.rb', line 34

def get_klass_by_symbol(sym)
  identifier = segment_id_to_symbol.invert[sym]
  get_klass_by_identifier(identifier)
end

.get_symbol_by_field(sym) ⇒ Object



30
31
32
# File 'lib/dzero/segments/base.rb', line 30

def get_symbol_by_field(sym)
  field_id_to_symbol[sym]
end

.inherited(klass) ⇒ Object



58
59
60
61
62
63
# File 'lib/dzero/segments/base.rb', line 58

def self.inherited(klass)
  super
  klass.symbol = klass.name.demodulize.underscore.to_sym
  klass.segment_id_to_symbol = segment_id_to_symbol
  klass.segment_id_to_klass  = segment_id_to_klass
end

.register_segment(klass, identifier:) ⇒ Object



15
16
17
18
19
20
# File 'lib/dzero/segments/base.rb', line 15

def register_segment(klass, identifier:)
  klass.segment_identifier = identifier

  self.segment_id_to_klass[identifier] = klass
  self.segment_id_to_symbol[identifier] = klass.symbol
end

Instance Method Details

#[](key) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/dzero/segments/base.rb', line 77

def [](key)
  if key.is_a?(Symbol)
    key = self.class.get_field_by_symbol(key)
  end

  @hash[key]
end

#[]=(key, value) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/dzero/segments/base.rb', line 85

def []=(key, value)
  if key.is_a?(Symbol)
    key = self.class.get_field_by_symbol(key)
  end

  @hash[key] = value
end

#merge(hash = {}) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/dzero/segments/base.rb', line 93

def merge(hash = {})
  hash.each do |key, value|
    self[key] = value
  end

  self
end