Class: Lazymodel::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::AttributeMethods, ActiveModel::Conversion, ActiveModel::Validations
Defined in:
lib/lazymodel/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Base

don’t override this method without calling super!



36
37
38
39
40
# File 'lib/lazymodel/base.rb', line 36

def initialize(attributes={})
  attributes.each do |attribute, value|
    send("#{attribute}=", value) if value.present?
  end
end

Class Method Details

.activemodel_attributes(*args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/lazymodel/base.rb', line 9

def activemodel_attributes(*args)
  opts = args.extract_options!
  # define attributes and prefixed/suffixed methods
  attr_accessor *args
  define_prefix_or_affix_method :prefix, opts[:prefix]
  define_prefix_or_affix_method :suffix, opts[:suffix]
  define_attribute_methods args
  # _attributes stores at class level the
  # the attribute names
  class_attribute :_attributes
  self._attributes = args
end

Instance Method Details

#attributesObject



42
43
44
45
46
# File 'lib/lazymodel/base.rb', line 42

def attributes
  _attributes.inject(HashWithIndifferentAccess.new) do |hash, attribute|
    hash.update attribute => send(attribute)
  end
end

#persisted?Boolean

override at need

Returns:

  • (Boolean)


53
54
55
# File 'lib/lazymodel/base.rb', line 53

def persisted?
  false
end

#to_modelObject



48
49
50
# File 'lib/lazymodel/base.rb', line 48

def to_model
  self
end