Class: AskAwesomely::Field::Field
- Inherits:
-
Object
- Object
- AskAwesomely::Field::Field
show all
- Includes:
- JsonBuilder
- Defined in:
- lib/ask_awesomely/field/field.rb
Direct Known Subclasses
Dropdown, Email, Legal, LongText, MultipleChoice, Number, OpinionScale, PictureChoice, Rating, ShortText, Statement, Website, YesNo
Constant Summary
collapse
- VALID_FIELD_TYPES =
%i(
short_text
long_text
multiple_choice
picture_choice
statement
dropdown
yes_no
number
rating
opinion_scale
email
website
legal
).freeze
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#build_json
Constructor Details
#initialize(type, &block) ⇒ Field
Allow init with properties common to all fields
33
34
35
36
37
|
# File 'lib/ask_awesomely/field/field.rb', line 33
def initialize(type, &block)
@state = OpenStruct.new(type: type)
self.instance_eval(&block) if block_given?
end
|
Instance Attribute Details
#state ⇒ Object
Returns the value of attribute state.
22
23
24
|
# File 'lib/ask_awesomely/field/field.rb', line 22
def state
@state
end
|
Class Method Details
.of_type(type, &block) ⇒ Object
24
25
26
27
28
29
30
|
# File 'lib/ask_awesomely/field/field.rb', line 24
def self.of_type(type, &block)
field_type = type.to_s.split('_').map(&:capitalize).join
field = AskAwesomely::Field.const_get(field_type)
field.new(type, &block)
rescue NameError => e
raise FieldTypeError, "Field #{type} <#{field}> does not exist, please use one of: #{VALID_FIELD_TYPES.join(", ")}"
end
|
Instance Method Details
#ask(question) ⇒ Object
Also known as:
say
39
40
41
|
# File 'lib/ask_awesomely/field/field.rb', line 39
def ask(question)
@state.question = question
end
|
#description(text) ⇒ Object
45
46
47
|
# File 'lib/ask_awesomely/field/field.rb', line 45
def description(text)
@state.description = text
end
|
#ref(name) ⇒ Object
57
58
59
|
# File 'lib/ask_awesomely/field/field.rb', line 57
def ref(name)
@state.ref = name.to_s
end
|
#required ⇒ Object
49
50
51
|
# File 'lib/ask_awesomely/field/field.rb', line 49
def required
@state.required = true
end
|
#skip(condition) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/ask_awesomely/field/field.rb', line 61
def skip(condition)
if condition[:if]
cond_if = condition[:if]
@state.skip = -> (context) { cond_if.call(context) == true }
end
if condition[:unless]
cond_unless = condition[:unless]
@state.skip = -> (context) { cond_unless.call(context) != true }
end
end
|
53
54
55
|
# File 'lib/ask_awesomely/field/field.rb', line 53
def tags(*tags)
@state.tags = tags
end
|