Sha256: 0910ca48725689de45a4e31fd73a45e3e81c752b7ac0d3823cf119554b07c8ef

Contents?: true

Size: 1000 Bytes

Versions: 10

Compression:

Stored size: 1000 Bytes

Contents

module N4j::Attributes
  extend ActiveSupport::Concern

  include ActiveModel::AttributeMethods
  include ActiveModel::Dirty

  included do
    attribute_method_suffix('=')
  end

  module ClassMethods
    def attributes
      @attributes ||= Set.new
    end

    def attribute(name)
      attributes << name.to_s
      define_attribute_methods [name.to_s]
    end
  end

  def attribute(key)
    instance_variable_get("@#{key}")
  end

  def attribute=(key,value)
    # puts "Setting attribute #{key}!"
    send("#{key}_will_change!") unless value == send(key)
    instance_variable_set("@#{key}", value)
  end

  def attributes
    self.class.attributes.inject({}) do |hsh, attr|
      hsh[attr] = send(attr)
      hsh
    end
  end

  def load_attributes(new_attributes)
    self.place_in_batch = nil
    self.class.attributes.each do |key|
      unless new_attributes[key].blank?
        value = new_attributes[key].dup
        instance_variable_set("@#{key}", value)
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
n4j-0.0.1.7 lib/n4j/attributes.rb
n4j-0.0.1.6.3 lib/n4j/attributes.rb
n4j-0.0.1.6.1 lib/n4j/attributes.rb
n4j-0.0.1.6 lib/n4j/attributes.rb
n4j-0.0.1.5 lib/n4j/attributes.rb
n4j-0.0.1.4 lib/n4j/attributes.rb
n4j-0.0.1.3 lib/n4j/attributes.rb
n4j-0.0.1.2 lib/n4j/attributes.rb
n4j-0.0.1.1 lib/n4j/attributes.rb
n4j-0.0.1 lib/n4j/attributes.rb