Module: AttrDefault::InstanceMethods

Defined in:
lib/attr_default.rb

Instance Method Summary collapse

Instance Method Details

#_attr_default_setObject



63
64
65
# File 'lib/attr_default.rb', line 63

def _attr_default_set
  @_attr_default_set ||= {}
end

#default_value_for(attr_name) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/attr_default.rb', line 48

def default_value_for(attr_name)
  attr_name = attr_name.to_s
  if self.class._attr_defaults.has_key?(attr_name)
    attr_default = self.class._attr_defaults[attr_name]
    attr_default.is_a?(Proc) ? instance_exec(&attr_default) : (attr_default.dup rescue attr_default)
  else
    column_data = self.class.columns_hash[attr_name] or raise ArgumentError, "#{self.class.name}##{attr_name} not found"
    column_data.default
  end
end

#dupObject Also known as: clone



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/attr_default.rb', line 67

def dup
  result = super
  result.created_at = nil unless !result.class.columns_hash.has_key?('created_at')
  result.updated_at = nil unless !result.class.columns_hash.has_key?('updated_at')
  if self.new_record?
    result.instance_variable_set(:@_attr_default_set, self._attr_default_set.dup)
  else
    result.instance_variable_set(:@_attr_defaults_set_from_dup, true)
  end
  result
end

#reset_to_default_value(attr_name) ⇒ Object



59
60
61
# File 'lib/attr_default.rb', line 59

def reset_to_default_value(attr_name)
  send("#{attr_name}=", default_value_for(attr_name))
end