Module: ApiModel::InstanceMethods
- Included in:
- Base
- Defined in:
- lib/api_model/instance_methods.rb
Instance Method Summary collapse
-
#property_exists?(property_name) ⇒ Boolean
Overrides Hashie::Trash to catch errors from trying to set properties which have not been defined and defines it automatically.
-
#set_errors_from_hash(errors_hash, obj = self) ⇒ Object
Convenience method to handle error hashes and set them as ActiveModel errors on instances.
Instance Method Details
#property_exists?(property_name) ⇒ Boolean
Overrides Hashie::Trash to catch errors from trying to set properties which have not been defined and defines it automatically
6 7 8 9 10 11 |
# File 'lib/api_model/instance_methods.rb', line 6 def property_exists?(property_name) super property_name rescue NoMethodError Log.debug "Could not set #{property_name} on #{self.class.name}. Defining it now." self.class.property property_name.to_sym end |
#set_errors_from_hash(errors_hash, obj = self) ⇒ Object
Convenience method to handle error hashes and set them as ActiveModel errors on instances. Using the ‘obj`, you can move the errors on to child classes if needed.
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/api_model/instance_methods.rb', line 15 def set_errors_from_hash(errors_hash, obj = self) errors_hash.each do |field,| if .is_a?(Array) .each do || obj.errors.add field.to_sym, end else obj.errors.add field.to_sym, end end end |