Class: Gillbus::V2::Structs::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/gillbus/v2/structs/base.rb

Defined Under Namespace

Classes: Enum

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_data:, **fields) ⇒ Base

Returns a new instance of Base.



13
14
15
16
17
18
# File 'lib/gillbus/v2/structs/base.rb', line 13

def initialize(raw_data:, **fields)
  @raw_data = raw_data
  fields.each do |field_name, field_value|
    instance_variable_set(:"@#{field_name}", field_value)
  end
end

Instance Attribute Details

#raw_dataObject (readonly)

Returns the value of attribute raw_data.



4
5
6
# File 'lib/gillbus/v2/structs/base.rb', line 4

def raw_data
  @raw_data
end

Class Method Details

.field(name, type, from: name.to_s, default: nil, enrich_with: nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/gillbus/v2/structs/base.rb', line 20

def self.field(name, type, from: name.to_s, default: nil, enrich_with: nil)
  attr_reader name
  @fields_settings ||= []
  @fields_settings << {
    name: name,
    type: type,
    from: from,
    default: default,
    enrich_with: enrich_with,
  }
end

.from_raw_data(raw_data) ⇒ Object



32
33
34
35
# File 'lib/gillbus/v2/structs/base.rb', line 32

def self.from_raw_data(raw_data)
  fields = Parser.new.parse_fields(raw_data, @fields_settings)
  new(raw_data: raw_data, **fields)
end