Class: Answers::Answer

Inherits:
BaseModel show all
Defined in:
lib/answers/answer.rb

Constant Summary collapse

ANSWER_ATTRS =
[
  {name: :id, read_only: true},
  {name: :created_at, read_only: true},
  {name: :updated_at, read_only: true},
  {name: :text, read_only: false},
  {name: :in_language, read_only: false},
  {name: :question_id, read_only: false}
]

Instance Method Summary collapse

Methods inherited from BaseModel

all, #delete, find, hash_key, #hash_key, #new?, resource_name, #resource_name, #save

Constructor Details

#initialize(params = {}) ⇒ Answer



20
21
22
23
24
25
# File 'lib/answers/answer.rb', line 20

def initialize(params={})
  ANSWER_ATTRS.each do |attribute|
    instance_variable_set("@#{attribute[:name]}", params[attribute[:name]]) if params[attribute[:name]]
  end
  update_attributes!(params)
end

Instance Method Details

#attributesObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/answers/answer.rb', line 27

def attributes
  ivar_to_sym = Proc.new {|ivar| ivar.to_s.sub(/^@/, '').to_sym}
  
  attributes = instance_variables.inject({}) do |r, s|
    r.merge!({ivar_to_sym[s] => instance_variable_get(s)})
  end.delete_if do |k,v|
    !ANSWER_ATTRS.map {|attribute| attribute[:name]}.include?(ivar_to_sym[k])
  end 
  
  attributes
end