lib/simple_model/attributes.rb in simple_model-0.2.6 vs lib/simple_model/attributes.rb in simple_model-1.0.0
- old
+ new
@@ -4,33 +4,15 @@
require 'active_support/core_ext/object/blank'
module Attributes
include ExtendCore
- attr_accessor :id, :saved
#Set attribute values to those supplied at initialization
def initialize(*attrs)
set_attributes(attrs.extract_options!)
end
- def new_record
- @new_record = true if @new_record.nil?
- @new_record
- end
-
- def new_record?
- new_record
- end
-
- def new_record=(new_record)
- @new_record = new_record
- end
-
- def persisted?
- saved.to_b
- end
-
# Place to store set attributes and their values
def attributes
@attributes ||= {}
@attributes
end
@@ -38,14 +20,12 @@
def set_attributes(attrs)
attrs.each do |attr|
self.send("#{attr[0].to_sym}=",attr[1])
end
end
-
alias :update_attributes :set_attributes
-
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
@@ -75,11 +55,10 @@
define_reader_with_options(attr,options)
define_method("#{attr.to_s}=") do |val|
instance_variable_set("@#{attr}", val.to_s.to_b)
attributes[attr] = val
val
-
end
define_method ("#{attr.to_s}?") do
send("#{attr.to_s}".to_sym).to_s.to_b
end
@@ -104,11 +83,11 @@
end
alias :has_int :has_ints
# Creates setter and getter methods for currency attributes
# attributes are cast to BigDecimal and rounded to nearest cent
- # Warning, rounding occurs on all sets, so if you need to keep higher prescsion
+ # #Warning, rounding occurs on all sets, so if you need to keep higher prescsion
# use has_decimals
def has_currency(*attrs)
options = attrs.extract_options!
attrs.each do |attr|
attr_reader attr
@@ -182,11 +161,10 @@
val
end
end
end
-
def fetch_alias_name(attr)
alias_name = (attr.to_s << "_old=").to_sym
self.module_eval("alias #{alias_name} #{attr}")
alias_name
@@ -195,11 +173,12 @@
# Defines a reader method that returns a default value if current value
# is nil, if :default is present in the options hash
def define_reader_with_options(attr,options)
unless options[:default].blank?
define_method (attr.to_s) do
- val = instance_variable_get("@#{attr.to_s}")
- val = options[:default] if val.nil?
+ default = options[:default].is_a?(Symbol) ? self.send(options[:default]) : options[:default]
+ val = instance_variable_get("@#{attr.to_s}")
+ val = default if val.nil?
val
end
end
end
end