Class: Answers::Question

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

Constant Summary collapse

QUESTION_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: :answers, 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 = {}) ⇒ Question

Returns a new instance of Question.



18
19
20
21
22
23
# File 'lib/answers/question.rb', line 18

def initialize(params={})
  QUESTION_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



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/answers/question.rb', line 25

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|
    !QUESTION_ATTRS.map {|attribute| attribute[:name]}.include?(ivar_to_sym[k])
  end 
  
  attributes
end