Class: ElectricProfile::Question

Inherits:
Object
  • Object
show all
Defined in:
lib/electric_profile_ruby/question.rb

Constant Summary collapse

TYPES =
['multi_choice', 'number', 'short_text', 'long_text']

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(atts) ⇒ Question

Returns a new instance of Question.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/electric_profile_ruby/question.rb', line 7

def initialize(atts)
  atts = atts.inject({}){ |memo, (k, v) | memo[k.to_sym] = v; memo }
  @id = atts[:id]
  @customerId = atts[:customerId]
  @name = atts[:name]
  @prompt = atts[:prompt]
  @type = atts[:type]
  @answerOptions = atts[:answerOptions] || {}
  @allowMultiple = atts[:allowMultiple] || false
  @categories = atts[:categories] || []
  @exclusiveAnswerOptions = atts[:exclusiveAnswerOptions]
  @answerOptionOrder = atts[:answerOptionOrder]
  @regExValidation = atts[:regExValidation]
  @regExValidationMessage = atts[:regExValidationMessage] || {}
  @baseLogic = atts[:baseLogic]
  @derived = atts[:derived] || false
  @derivationLogic = atts[:derivationLogic] || []
end

Instance Attribute Details

#allowMultipleObject

Returns the value of attribute allowMultiple.



4
5
6
# File 'lib/electric_profile_ruby/question.rb', line 4

def allowMultiple
  @allowMultiple
end

#answerOptionOrderObject

Returns the value of attribute answerOptionOrder.



4
5
6
# File 'lib/electric_profile_ruby/question.rb', line 4

def answerOptionOrder
  @answerOptionOrder
end

#answerOptionsObject

Returns the value of attribute answerOptions.



4
5
6
# File 'lib/electric_profile_ruby/question.rb', line 4

def answerOptions
  @answerOptions
end

#baseLogicObject

Returns the value of attribute baseLogic.



4
5
6
# File 'lib/electric_profile_ruby/question.rb', line 4

def baseLogic
  @baseLogic
end

#categoriesObject

Returns the value of attribute categories.



4
5
6
# File 'lib/electric_profile_ruby/question.rb', line 4

def categories
  @categories
end

#customerIdObject

Returns the value of attribute customerId.



4
5
6
# File 'lib/electric_profile_ruby/question.rb', line 4

def customerId
  @customerId
end

#derivationLogicObject

Returns the value of attribute derivationLogic.



4
5
6
# File 'lib/electric_profile_ruby/question.rb', line 4

def derivationLogic
  @derivationLogic
end

#derivedObject

Returns the value of attribute derived.



4
5
6
# File 'lib/electric_profile_ruby/question.rb', line 4

def derived
  @derived
end

#errorObject

Returns the value of attribute error.



4
5
6
# File 'lib/electric_profile_ruby/question.rb', line 4

def error
  @error
end

#exclusiveAnswerOptionsObject

Returns the value of attribute exclusiveAnswerOptions.



4
5
6
# File 'lib/electric_profile_ruby/question.rb', line 4

def exclusiveAnswerOptions
  @exclusiveAnswerOptions
end

#idObject

Returns the value of attribute id.



4
5
6
# File 'lib/electric_profile_ruby/question.rb', line 4

def id
  @id
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/electric_profile_ruby/question.rb', line 4

def name
  @name
end

#promptObject

Returns the value of attribute prompt.



4
5
6
# File 'lib/electric_profile_ruby/question.rb', line 4

def prompt
  @prompt
end

#regExValidationObject

Returns the value of attribute regExValidation.



4
5
6
# File 'lib/electric_profile_ruby/question.rb', line 4

def regExValidation
  @regExValidation
end

#regExValidationMessageObject

Returns the value of attribute regExValidationMessage.



4
5
6
# File 'lib/electric_profile_ruby/question.rb', line 4

def regExValidationMessage
  @regExValidationMessage
end

#typeObject

Returns the value of attribute type.



4
5
6
# File 'lib/electric_profile_ruby/question.rb', line 4

def type
  @type
end

Class Method Details

.allObject



97
98
99
100
101
102
103
104
# File 'lib/electric_profile_ruby/question.rb', line 97

def self.all
  client = Client.new
  if client.fetch_questions
    client.data["Items"].map { |atts| new atts }
  else
    raise StandardError, client.error
  end
end

.all_with_category(category) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/electric_profile_ruby/question.rb', line 106

def self.all_with_category(category)
  client = Client.new
  if client.fetch_questions(categoryIncluded: category)
    client.data["Items"].map { |atts| new atts }
  else
    raise StandardError, client.error
  end
end

.all_without_category(category) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/electric_profile_ruby/question.rb', line 115

def self.all_without_category(category)
  client = Client.new
  if client.fetch_questions(categoryExcluded: category)
    client.data["Items"].map { |atts| new atts }
  else
    raise StandardError, client.error
  end
end

.find(id) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/electric_profile_ruby/question.rb', line 88

def self.find(id)
  client = Client.new
  if client.fetch_question(id)
    new client.data
  else
    raise StandardError, client.error
  end
end

Instance Method Details

#initialize_clientObject



124
125
126
# File 'lib/electric_profile_ruby/question.rb', line 124

def initialize_client
  @client ||= Client.new
end

#saveObject



26
27
28
29
30
31
32
# File 'lib/electric_profile_ruby/question.rb', line 26

def save
  if @id
    save_existing
  else
    save_new
  end
end

#save_existingObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/electric_profile_ruby/question.rb', line 61

def save_existing
  initialize_client
  attributes = {
    id: @id,
    customerId: @customerId,
    name: @name,
    prompt: @prompt,
    type: @type,
    answerOptions: @answerOptions,
    allowMultiple: @allowMultiple,
    categories: @categories,
    exclusiveAnswerOptions: @exclusiveAnswerOptions,
    answerOptionOrder: @answerOptionOrder,
    regExValidation: @regExValidation,
    regExValidationMessage: @regExValidationMessage,
    baseLogic: @baseLogic,
    derived: @derived,
    derivationLogic: @derivationLogic
  }
  if @client.update_question(attributes)
    true
  else
    @error = @client.error
    false
  end
end

#save_newObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/electric_profile_ruby/question.rb', line 34

def save_new
  initialize_client
  attributes = {
    name: @name,
    prompt: @prompt,
    type: @type,
    answerOptions: @answerOptions,
    allowMultiple: @allowMultiple,
    categories: @categories,
    exclusiveAnswerOptions: @exclusiveAnswerOptions,
    answerOptionOrder: @answerOptionOrder,
    regExValidation: @regExValidation,
    regExValidationMessage: @regExValidationMessage,
    baseLogic: @baseLogic,
    derived: @derived,
    derivationLogic: @derivationLogic
  }
  if @client.create_question(attributes)
    @id = @client.data["id"]
    @customerId = @client.data["customerId"]
    true
  else
    @error = @client.error
    false
  end
end