class Tr8n::Base
Attributes
attributes[R]
Public Class Methods
attributes(*attrs)
click to toggle source
# File lib/tr8n/base.rb, line 49 def self.attributes(*attrs) @attribute_names ||= [] @attribute_names += attrs.collect{|a| a.to_sym} unless attrs.nil? @attribute_names end
belongs_to(*attrs)
click to toggle source
# File lib/tr8n/base.rb, line 54 def self.belongs_to(*attrs) self.attributes(*attrs); end
has_many(*attrs)
click to toggle source
# File lib/tr8n/base.rb, line 55 def self.has_many(*attrs) self.attributes(*attrs); end
hash_value(hash, key, opts = {})
click to toggle source
# File lib/tr8n/base.rb, line 76 def self.hash_value(hash, key, opts = {}) return nil unless hash.is_a?(Hash) return hash[key.to_s] || hash[key.to_sym] if opts[:whole] value = hash key.to_s.split('.').each do |part| return nil unless value.is_a?(Hash) value = value[part.to_sym] || value[part.to_s] end value end
new(attrs = {})
click to toggle source
# File lib/tr8n/base.rb, line 36 def initialize(attrs = {}) @attributes = {} update_attributes(attrs) end
Public Instance Methods
hash_value(hash, key, opts = {})
click to toggle source
# File lib/tr8n/base.rb, line 88 def hash_value(hash, key, opts = {}) self.class.hash_value(hash, key, opts) end
method_missing(meth, *args, &block)
click to toggle source
Calls superclass method
# File lib/tr8n/base.rb, line 57 def method_missing(meth, *args, &block) method_name = meth.to_s method_suffix = method_name[-1, 1] method_key = method_name.to_sym if %w(= ?).include?(method_suffix) method_key = method_name[0..-2].to_sym end if self.class.attributes.index(method_key) if method_suffix == '=' attributes[method_key] = args.first return attributes[method_key] end return attributes[method_key] end super end
to_hash(*attrs)
click to toggle source
# File lib/tr8n/base.rb, line 92 def to_hash(*attrs) if attrs.nil? or attrs.empty? # default hashing only includes basic types keys = [] self.class.attributes.each do |key| value = attributes[key] next if value.kind_of?(Tr8n::Base) or value.kind_of?(Hash) or value.kind_of?(Array) keys << key end else keys = attrs end hash = {} keys.each do |key| hash[key] = attributes[key] end proc = Proc.new { |k, v| v.kind_of?(Hash) ? (v.delete_if(&proc); nil) : v.nil? } hash.delete_if(&proc) hash end
update_attributes(attrs = {})
click to toggle source
# File lib/tr8n/base.rb, line 41 def update_attributes(attrs = {}) attrs.each do |key, value| #pp [self.class.name, key, self.class.attributes, self.class.attributes.include?(key.to_sym)] next unless self.class.attributes.include?(key.to_sym) @attributes[key.to_sym] = value end end