Module: Ascribe::Attributes

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::Validations
Defined in:
lib/ascribe/attributes.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#assign_attributes(attrs) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/ascribe/attributes.rb', line 101

def assign_attributes(attrs)
  attribute_keys.each do |attr_key|
    value = read_attribute(attr_key)
    write_attribute(attr_key, value)
  end

  # Set all attributes supplied in the attrs hash
  attrs.each_pair do |key, value|
    if respond_to?(:"#{key}")
      val = 
      write_attribute(key, value)
    else
      options[key.to_s] = value
    end
  end
end

#attribute_keysObject



154
155
156
# File 'lib/ascribe/attributes.rb', line 154

def attribute_keys
  self.class.attributes.keys
end

#attributesObject



134
135
136
137
138
139
140
141
142
# File 'lib/ascribe/attributes.rb', line 134

def attributes
  attributes = {}
  
  self.class.attributes.each do |key, attribute|
    name = attribute.name
    attributes[name] = read_attribute(name) if respond_to?(name)
  end
  return attributes
end

#attributes=(attrs = {}) ⇒ Object



144
145
146
147
148
149
150
151
152
# File 'lib/ascribe/attributes.rb', line 144

def attributes=(attrs={})
  return if attrs.blank?
  
  attrs.each_pair do |key, value|
    if respond_to?(:"#{key}")
      write_attribute(key, value)
    end
  end
end

#initialize(attrs = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ascribe/attributes.rb', line 73

def initialize(attrs={})
  self.class.attributes.each_pair do |key, attribute|
    (class << self; self; end).class_eval do
      define_method(attribute.name) do |*value, &block|
        if !block.nil?
          write_attribute(attribute.name, block)
        elsif !value.blank?
          write_attribute(attribute.name, value.first)
        else
          read_attribute(attribute.name)
        end
      end
      define_method("#{attribute.name}=") do |*value, &block|
        if !block.nil?
          write_attribute(attribute.name, block)
        elsif !value.blank?
          write_attribute(attribute.name, value.first)
        end
      end 
    end
  end
  assign_attributes(attrs)
end

#inspectObject



162
163
164
165
166
167
168
# File 'lib/ascribe/attributes.rb', line 162

def inspect
  attrs = attributes.map do |attribute|
    "@#{attribute[0]}=#{attribute[1] ? attribute[1] : "nil"}"
  end
  result = attrs + ["@options=#{options}"]
  "#<#{self.class.name} #{result.join(" ")}>"
end

#optionsObject



97
98
99
# File 'lib/ascribe/attributes.rb', line 97

def options
  @options ||= {}
end

#read_attribute(name) ⇒ Object



122
123
124
125
126
127
# File 'lib/ascribe/attributes.rb', line 122

def read_attribute(name)
  if attribute = self.class.attributes[name.to_s]
    value = attribute.get(instance_variable_get(:"@#{name}"))
    instance_variable_set(:"@#{name}", value)
  end
end

#to_hashObject



158
159
160
# File 'lib/ascribe/attributes.rb', line 158

def to_hash
  attributes.merge("options" => options)
end

#update(attrs = {}) ⇒ Object



118
119
120
# File 'lib/ascribe/attributes.rb', line 118

def update(attrs={})
  assign_attributes(attrs)
end

#write_attribute(name, value) ⇒ Object



129
130
131
132
# File 'lib/ascribe/attributes.rb', line 129

def write_attribute(name, value)
  attribute = self.class.attributes[name.to_s]
  instance_variable_set(:"@#{name}", attribute.set(value))
end