Module: ApiModel::InstanceMethods

Included in:
Base
Defined in:
lib/api_model/instance_methods.rb

Instance Method Summary collapse

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

Returns:

  • (Boolean)


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,messages|
    if messages.is_a?(Array)
      messages.each do |message|
        obj.errors.add field.to_sym, message
      end
    else
      obj.errors.add field.to_sym, messages
    end
  end
end