Class: Census::UserData

Inherits:
Object
  • Object
show all
Defined in:
lib/census/user_data.rb

Instance Method Summary collapse

Constructor Details

#initialize(user, data_group = nil) ⇒ UserData

Returns a new instance of UserData.



4
5
6
7
8
# File 'lib/census/user_data.rb', line 4

def initialize(user, data_group = nil)
  @user = user
  @data_group = data_group
  reload
end

Instance Method Details

#[](key) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/census/user_data.rb', line 24

def [](key)
  if @data_groups
    find_data_group(key)
  else
    question = find_question(key)
    @user.first_answer_for(question).formatted_data if question
  end
end

#[]=(key, value) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/census/user_data.rb', line 33

def []=(key, value)
  if @data_groups
    raise ArgumentError, "Can't be invoked on a Data Group"
  else
    question = find_question(key)
    @user.first_answer_for(question).update_attribute(:data, question.format_data(value).to_s) if question
  end
end

#each_pairObject



42
43
44
# File 'lib/census/user_data.rb', line 42

def each_pair
  self.keys.each{ |key| yield key, self.send(key.parameterize.underscore.to_sym) }
end

#nameObject



20
21
22
# File 'lib/census/user_data.rb', line 20

def name
  @data_group.name if @data_group
end

#reloadObject



10
11
12
13
14
15
16
17
18
# File 'lib/census/user_data.rb', line 10

def reload
  if @data_group
    @questions = []
    define_question_methods(@data_group)
  else
    @data_groups = []
    define_data_group_methods
  end
end