module Her::Model::Attributes::ClassMethods

Public Instance Methods

attributes(*attributes) click to toggle source

Define the attributes that will be used to track dirty attributes and validations

@param [Array] attributes @example

class User
  include Her::Model
  attributes :name, :email
end
# File lib/her_extension/model/attributes.rb, line 18
def attributes(*attributes)
  define_attribute_methods attributes

  attributes.each do |attribute|
    attribute = attribute.to_sym

    unless instance_methods.include?(:"#{attribute}=")
      define_method("#{attribute}=") do |value|
        @attributes[:"#{attribute}"] = nil unless @attributes.include?(:"#{attribute}")
        self.send(:"#{attribute}_will_change!") if @attributes[:"#{attribute}"] != value
        @attributes[:"#{attribute}"] = value
      end
    end

    unless instance_methods.include?(:"#{attribute}?")
      define_method("#{attribute}?") do
        @attributes.include?(:"#{attribute}") && @attributes[:"#{attribute}"].present?
      end
    end
  end
end