Class: TreezorConnect::TreezorObject
- Inherits:
-
Object
- Object
- TreezorConnect::TreezorObject
- Defined in:
- lib/treezor_connect/treezor_object.rb
Direct Known Subclasses
Class Method Summary collapse
- .constantize_object_class(object_class) ⇒ Object
- .construct_from(data, opts = {}) ⇒ Object
- .create_instance(object_class, id) ⇒ Object
- .define_readers(instance) ⇒ Object
- .extract_id(data, object_class) ⇒ Object
- .set_attributes(instance, data) ⇒ Object
Instance Method Summary collapse
-
#initialize ⇒ TreezorObject
constructor
A new instance of TreezorObject.
- #to_hash ⇒ Object
Constructor Details
#initialize ⇒ TreezorObject
Returns a new instance of TreezorObject.
5 6 7 |
# File 'lib/treezor_connect/treezor_object.rb', line 5 def initialize @attributes = {} end |
Class Method Details
.constantize_object_class(object_class) ⇒ Object
24 25 26 27 |
# File 'lib/treezor_connect/treezor_object.rb', line 24 def self.constantize_object_class(object_class) object_class_path = object_class.split('/').map { |value| Util.camelize(value) }.join('::') Module.const_get("TreezorConnect::#{object_class_path}") end |
.construct_from(data, opts = {}) ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/treezor_connect/treezor_object.rb', line 15 def self.construct_from(data, opts = {}) object_class = constantize_object_class(opts[:object_class]) id = extract_id(data, object_class) instance = create_instance(object_class, id) set_attributes(instance, data) define_readers(instance) instance end |
.create_instance(object_class, id) ⇒ Object
34 35 36 37 38 |
# File 'lib/treezor_connect/treezor_object.rb', line 34 def self.create_instance(object_class, id) instance = object_class.new instance.instance_variable_set(:@attributes, { 'id' => id }) instance end |
.define_readers(instance) ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/treezor_connect/treezor_object.rb', line 45 def self.define_readers(instance) @attributes = instance.instance_variable_get(:@attributes) @attributes.each_key do |key| instance.define_singleton_method(key) do @attributes[key] end end end |
.extract_id(data, object_class) ⇒ Object
29 30 31 32 |
# File 'lib/treezor_connect/treezor_object.rb', line 29 def self.extract_id(data, object_class) primary_key = object_class::OBJECT_PRIMARY_KEY data.delete(primary_key) end |
.set_attributes(instance, data) ⇒ Object
40 41 42 43 |
# File 'lib/treezor_connect/treezor_object.rb', line 40 def self.set_attributes(instance, data) snake_cased_data = data.transform_keys { |key| Util.snake_case(key) } instance.instance_variable_get(:@attributes).merge!(snake_cased_data) end |
Instance Method Details
#to_hash ⇒ Object
9 10 11 12 13 |
# File 'lib/treezor_connect/treezor_object.rb', line 9 def to_hash @attributes.transform_values do |value| transform_value(value) end end |