Module: EasyTalk::Model::InstanceMethods
- Defined in:
- lib/easy_talk/model.rb
Overview
Instance methods mixed into models that include EasyTalk::Model
Instance Method Summary collapse
-
#==(other) ⇒ Object
Allow comparison with hashes.
-
#as_json(_options = {}) ⇒ Object
Override as_json to include both defined and additional properties.
- #initialize(attributes = {}) ⇒ Object
- #method_missing(method_name, *args) ⇒ Object
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
-
#to_h ⇒ Object
to_h includes both defined and additional properties.
-
#to_hash ⇒ Object
Add to_hash method to convert defined properties to hash.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/easy_talk/model.rb', line 110 def method_missing(method_name, *args) method_string = method_name.to_s if method_string.end_with?('=') property_name = method_string.chomp('=') if self.class.additional_properties_allowed? @additional_properties[property_name] = args.first else super end elsif self.class.additional_properties_allowed? && @additional_properties.key?(method_string) @additional_properties[method_string] else super end end |
Instance Method Details
#==(other) ⇒ Object
Allow comparison with hashes
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/easy_talk/model.rb', line 154 def ==(other) case other when Hash # Convert both to comparable format for comparison self_hash = (self.class.schema_definition.schema[:properties] || {}).keys.each_with_object({}) do |prop, hash| hash[prop] = send(prop) end # Handle both symbol and string keys in the other hash other_normalized = other.transform_keys(&:to_sym) self_hash == other_normalized else super end end |
#as_json(_options = {}) ⇒ Object
Override as_json to include both defined and additional properties
144 145 146 |
# File 'lib/easy_talk/model.rb', line 144 def as_json( = {}) to_hash.merge(@additional_properties) end |
#initialize(attributes = {}) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/easy_talk/model.rb', line 51 def initialize(attributes = {}) @additional_properties = {} provided_keys = attributes.keys.to_set(&:to_sym) super # Perform initial mass assignment schema_def = self.class.schema_definition return unless schema_def.respond_to?(:schema) && schema_def.schema.is_a?(Hash) (schema_def.schema[:properties] || {}).each do |prop_name, prop_definition| process_property_initialization(prop_name, prop_definition, provided_keys) end end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
126 127 128 129 130 131 |
# File 'lib/easy_talk/model.rb', line 126 def respond_to_missing?(method_name, include_private = false) return super unless self.class.additional_properties_allowed? method_string = method_name.to_s method_string.end_with?('=') || @additional_properties.key?(method_string) || super end |
#to_h ⇒ Object
to_h includes both defined and additional properties
149 150 151 |
# File 'lib/easy_talk/model.rb', line 149 def to_h to_hash.merge(@additional_properties) end |
#to_hash ⇒ Object
Add to_hash method to convert defined properties to hash
134 135 136 137 138 139 140 141 |
# File 'lib/easy_talk/model.rb', line 134 def to_hash properties_to_include = (self.class.schema_definition.schema[:properties] || {}).keys return {} if properties_to_include.empty? properties_to_include.each_with_object({}) do |prop, hash| hash[prop.to_s] = send(prop) end end |