Class: Nylas::Model::Attributes
- Inherits:
-
Object
- Object
- Nylas::Model::Attributes
- Defined in:
- lib/nylas/model/attributes.rb
Overview
Stores the actual model data to allow for type casting and clean/dirty checking
Instance Attribute Summary collapse
-
#attribute_definitions ⇒ Object
Returns the value of attribute attribute_definitions.
-
#data ⇒ Object
Returns the value of attribute data.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
-
#initialize(attribute_definitions) ⇒ Attributes
constructor
A new instance of Attributes.
-
#merge(new_data) ⇒ Object
Merges data into the registry while casting input types correctly.
-
#serialize(keys: attribute_definitions.keys, enforce_read_only: false) ⇒ String
Serialize the object.
- #serialize_all_for_api(keys: attribute_definitions.keys) ⇒ Object
-
#serialize_for_api(keys: attribute_definitions.keys) ⇒ String
Serialize the object to an API-compatible JSON string.
-
#to_h(keys: attribute_definitions.keys, enforce_read_only: false) ⇒ Hash
Convert the object to hash.
Constructor Details
#initialize(attribute_definitions) ⇒ Attributes
Returns a new instance of Attributes.
9 10 11 12 |
# File 'lib/nylas/model/attributes.rb', line 9 def initialize(attribute_definitions) @attribute_definitions = attribute_definitions @data = Registry.new(default_attributes) end |
Instance Attribute Details
#attribute_definitions ⇒ Object
Returns the value of attribute attribute_definitions.
7 8 9 |
# File 'lib/nylas/model/attributes.rb', line 7 def attribute_definitions @attribute_definitions end |
#data ⇒ Object
Returns the value of attribute data.
7 8 9 |
# File 'lib/nylas/model/attributes.rb', line 7 def data @data end |
Instance Method Details
#[](key) ⇒ Object
14 15 16 |
# File 'lib/nylas/model/attributes.rb', line 14 def [](key) data[key] end |
#[]=(key, value) ⇒ Object
18 19 20 21 22 |
# File 'lib/nylas/model/attributes.rb', line 18 def []=(key, value) data[key] = cast(key, value) rescue Nylas::Registry::MissingKeyError # Don't crash when a new attribute is added end |
#merge(new_data) ⇒ Object
Merges data into the registry while casting input types correctly
25 26 27 28 29 |
# File 'lib/nylas/model/attributes.rb', line 25 def merge(new_data) new_data.each do |attribute_name, value| self[attribute_name] = value end end |
#serialize(keys: attribute_definitions.keys, enforce_read_only: false) ⇒ String
Serialize the object
50 51 52 |
# File 'lib/nylas/model/attributes.rb', line 50 def serialize(keys: attribute_definitions.keys, enforce_read_only: false) JSON.dump(to_h(keys: keys, enforce_read_only: enforce_read_only)) end |
#serialize_all_for_api(keys: attribute_definitions.keys) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/nylas/model/attributes.rb', line 61 def serialize_all_for_api(keys: attribute_definitions.keys) api_keys = keys.delete_if { |key| attribute_definitions[key].read_only == true } JSON.dump( api_keys.each_with_object({}) do |key, casted_data| casted_data[key] = attribute_definitions[key].serialize(self[key]) end ) end |
#serialize_for_api(keys: attribute_definitions.keys) ⇒ String
Serialize the object to an API-compatible JSON string
57 58 59 |
# File 'lib/nylas/model/attributes.rb', line 57 def serialize_for_api(keys: attribute_definitions.keys) serialize(keys: keys, enforce_read_only: true) end |
#to_h(keys: attribute_definitions.keys, enforce_read_only: false) ⇒ Hash
Convert the object to hash
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/nylas/model/attributes.rb', line 35 def to_h(keys: attribute_definitions.keys, enforce_read_only: false) casted_data = {} keys.each do |key| value = attribute_to_hash(key, enforce_read_only) # If the value is an empty hash but we specify that it is valid (via default value), serialize it casted_data[key] = value unless value.nil? || (value.respond_to?(:empty?) && value.empty? && !(attribute_definitions[key].default == value && value.is_a?(Hash))) end casted_data end |