Class: ForgedModel::Model

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Dirty, ActiveModel::Model, ActiveModel::Serialization
Defined in:
lib/forged_model/model.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActiveModel::Model

included, #persisted?

Constructor Details

#initialize(params = {}) ⇒ Model

Returns a new instance of Model.



31
32
33
34
35
# File 'lib/forged_model/model.rb', line 31

def initialize(params={})
  load(params)
  changed_attributes.clear
  super()
end

Class Method Details

.define_attributes(*methods) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/forged_model/model.rb', line 15

def define_attributes(*methods)
  self.attribute_list += methods
  attr_reader *methods
  methods.each do |method|
    class_eval(<<-EOS, __FILE__, __LINE__ + 1)
      def #{method}=(value)
        #{method}_will_change!
        @#{method} = value
      end
    EOS
  end
  define_attribute_methods methods
end

Instance Method Details

#attributesObject



37
38
39
40
41
# File 'lib/forged_model/model.rb', line 37

def attributes
  Hash[self.class.attribute_list \
    .select{ |name| instance_variable_defined? "@#{name}" } \
    .map { |name, _| [name, self.send(name)] }]
end