Module: SimpleModel::Attributes

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::AttributeMethods, ExtendCore
Included in:
Base
Defined in:
lib/simple_model/attributes.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



6
7
8
# File 'lib/simple_model/attributes.rb', line 6

def attributes
  @attributes
end

Instance Method Details

#get(attr) ⇒ Object Also known as: read



25
26
27
# File 'lib/simple_model/attributes.rb', line 25

def get(attr)
  send(attr)
end

#get_attribute(attr) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/simple_model/attributes.rb', line 50

def get_attribute(attr)
  val = attributes[attr]
  options = self.class.defined_attributes[attr] || {}
  if (options.key?(:default) && (!initialized?(attr) || (!options[:allow_blank] && val.blank?)))
    val = attributes[attr] = fetch_default_value(options[:default])
  end
  if options[:on_get]
    options[:on_get].call(self,val)
  else
    val
  end
end

#get_attribute?(attr) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
71
# File 'lib/simple_model/attributes.rb', line 63

def get_attribute?(attr)
  return false unless val = get_attribute(attr)
  if val.respond_to?(:blank?)
    return !val.blank?
  elsif val.respond_to?(:to_b) 
    return val.to_b
  end
  !val.nil?
end

#initialize(*attrs) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/simple_model/attributes.rb', line 8

def initialize(*attrs)
  attrs = attrs.extract_options!
  attrs = attributes_for_init(attrs)
  attrs = self.class.before_initialize.call(self,attrs) if self.class.before_initialize
  set(attrs)
  self.class.after_initialize.call(self) if self.class.after_initialize
end

#initialized?(attr) ⇒ Boolean

Returns true if attribute has been initialized

Returns:

  • (Boolean)


21
22
23
# File 'lib/simple_model/attributes.rb', line 21

def initialized?(attr)
  attributes.key?(attr)
end

#set(*attrs) ⇒ Object Also known as: set_attributes

Accepts a hash where the keys are methods and the values are values to be set. set(:foo => “bar”, :dime => 0.1)



32
33
34
35
36
# File 'lib/simple_model/attributes.rb', line 32

def set(*attrs)
  attrs.extract_options!.each do |attr,val|
    send("#{attr}=",val)
  end
end

#set_attribute(attr, val) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/simple_model/attributes.rb', line 39

def set_attribute(attr,val)
  options = self.class.defined_attributes[attr] || {}
  if allow_attribute_action?(val,options)
    val = fetch_default_value(options[:default]) if (!options[:allow_blank] && options.key?(:default) && val.blank?)
    val = options[:on_set].call(self,val) if options[:on_set] #(!options.key?(:on_set) || (val.blank? && !options[:allow_blank]) )
    send("#{attr}_will_change!") if (initialized?(attr) && val != attributes[attr])
    attributes[attr] = val
    options[:after_set].call(self,val) if options[:after_set]
  end
end