Class: Yar::Object

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/Yar/object.rb,
lib/yar/object.rb

Overview

TODO: change to Struct

Direct Known Subclasses

Carrier, City, Copyright, Schedule, Station

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Object

Returns a new instance of Object.



8
9
10
# File 'lib/Yar/object.rb', line 8

def initialize(attributes)
  super to_ostruct(attributes)
end

Instance Method Details

#to_ostruct(obj) ⇒ Object

Convert Array or Hash to OpenStruct



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/Yar/object.rb', line 13

def to_ostruct(obj)
  case obj
  when Hash
    # obj.to_struct
    # rubocop:disable Style/HashTransformValues
    OpenStruct.new(obj.map {|key, val| [key, to_ostruct(val)] }.to_h)
    # rubocop:enable Style/HashTransformValues
  when Array
    obj.map {|o| to_ostruct(o) }
  else
    obj
  end
end