Class: Frame::FrameObject
- Inherits:
-
Object
- Object
- Frame::FrameObject
- Includes:
- Enumerable
- Defined in:
- lib/frame/frame_object.rb
Overview
Base object class for all Frame API responses.
FrameObject provides dynamic attribute access and JSON serialization for API responses. All API resources inherit from this class.
Attributes are dynamically created based on the API response, allowing for flexible access to all fields:
customer.name # => "John Doe"
customer["name"] # => "John Doe"
customer[:name] # => "John Doe"
Direct Known Subclasses
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#original_values ⇒ Object
readonly
Returns the value of attribute original_values.
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #each(&blk) ⇒ Object
-
#initialize(id = nil, opts = {}) ⇒ FrameObject
constructor
A new instance of FrameObject.
- #initialize_from(values, opts = {}) ⇒ Object
- #inspect ⇒ Object
- #keys ⇒ Object
- #serialize_params(obj) ⇒ Object
- #to_hash ⇒ Object
- #to_s(*_args) ⇒ Object
- #update_attributes(values) ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize(id = nil, opts = {}) ⇒ FrameObject
Returns a new instance of FrameObject.
21 22 23 24 25 26 |
# File 'lib/frame/frame_object.rb', line 21 def initialize(id = nil, opts = {}) @id = id @values = {} @original_values = {} @unsaved_values = Set.new end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
19 20 21 |
# File 'lib/frame/frame_object.rb', line 19 def id @id end |
#original_values ⇒ Object (readonly)
Returns the value of attribute original_values.
19 20 21 |
# File 'lib/frame/frame_object.rb', line 19 def original_values @original_values end |
Class Method Details
.construct_from(values, opts = {}) ⇒ Object
28 29 30 31 32 |
# File 'lib/frame/frame_object.rb', line 28 def self.construct_from(values, opts = {}) obj = new(values[:id]) obj.initialize_from(values, opts) obj end |
Instance Method Details
#[](key) ⇒ Object
56 57 58 |
# File 'lib/frame/frame_object.rb', line 56 def [](key) @values[key.to_sym] end |
#[]=(key, value) ⇒ Object
60 61 62 |
# File 'lib/frame/frame_object.rb', line 60 def []=(key, value) send(:"#{key}=", value) end |
#each(&blk) ⇒ Object
94 95 96 |
# File 'lib/frame/frame_object.rb', line 94 def each(&blk) @values.each(&blk) end |
#initialize_from(values, opts = {}) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/frame/frame_object.rb', line 34 def initialize_from(values, opts = {}) @original_values = values.dup @values = values.dup # Make sure all keys are symbols @values.keys.each do |k| @values[k.to_sym] = @values.delete(k) unless k.is_a?(Symbol) end # Add accessors for all keys remove_accessors(@values.keys) add_accessors(@values.keys) self end |
#inspect ⇒ Object
76 77 78 79 |
# File 'lib/frame/frame_object.rb', line 76 def inspect id_string = @id.nil? ? "" : " id=#{@id}" "#<#{self.class}:0x#{object_id.to_s(16)}#{id_string}> JSON: " + JSON.pretty_generate(@values) end |
#keys ⇒ Object
64 65 66 |
# File 'lib/frame/frame_object.rb', line 64 def keys @values.keys end |
#serialize_params(obj) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/frame/frame_object.rb', line 98 def serialize_params(obj) params = {} obj.instance_variable_get(:@values).each do |key, value| params[key] = if value.is_a?(FrameObject) value.serialize_params(value) elsif value.is_a?(Array) value.map { |v| v.is_a?(FrameObject) ? v.serialize_params(v) : v } else value end end params end |
#to_hash ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/frame/frame_object.rb', line 81 def to_hash @values.each_with_object({}) do |(key, value), hash| hash[key] = case value when FrameObject value.to_hash when Array value.map { |v| v.respond_to?(:to_hash) ? v.to_hash : v } else value end end end |
#to_s(*_args) ⇒ Object
72 73 74 |
# File 'lib/frame/frame_object.rb', line 72 def to_s(*_args) JSON.pretty_generate(@values) end |
#update_attributes(values) ⇒ Object
50 51 52 53 54 |
# File 'lib/frame/frame_object.rb', line 50 def update_attributes(values) values.each do |k, v| @values[k] = Util.convert_to_frame_object(v) end end |
#values ⇒ Object
68 69 70 |
# File 'lib/frame/frame_object.rb', line 68 def values @values.values end |