Sha256: 609f08e0367b13bf605c24eb025dd0c6c6b1be90e310946ebbe67402ef59cd42
Contents?: true
Size: 1.82 KB
Versions: 1
Compression:
Stored size: 1.82 KB
Contents
module Attribrutal module Model def self.included(base) base.extend ClassMethods end def initialize( attrs = {} ) all_attributes = attrs.symbolize_keys @raw_attributes = all_attributes.select {|k,v| attribute_keys.include? k } other_attributes = all_attributes.reject {|k,v| attribute_keys.include? k } other_attributes.map {|key, val| self.send("#{key}=", val) if respond_to?("#{key}=") } end def raw_attributes @raw_attributes end def attributes attribute_keys.inject(Hash.new) do |attributes, attribute| attributes[attribute] = self.send(attribute) attributes end end def attribute_keys self.class.attribute_keys end module ClassMethods def attribute (sym, coercer=nil, attrs = {}) define_method(sym) do default_value = case attrs[:default].class.name when "NilClass", "TrueClass", "FalseClass", "Numeric", "Fixnum", "Symbol" attrs[:default] when "Proc" attrs[:default].call else attrs[:default].clone end if coercer && coercer.respond_to?(:coerce) coercer.send(:coerce, raw_attributes[sym], default_value) else raw_attributes[sym] ||= default_value end end define_method("#{sym}=".to_sym) do |value| raw_attributes[sym] = value end if @attributes @attributes.merge!({ sym => coercer }) else @attributes = { sym => coercer } end end def attributes @attributes end def attribute_keys @attributes.keys end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
attribrutal-0.0.10 | lib/attribrutal/model.rb |