Class: Interrogative::Question
- Inherits:
-
Object
- Object
- Interrogative::Question
- Defined in:
- lib/interrogative/question.rb
Overview
A question with a unique name and a textual representation.
Designed to translate well into an HTML form element without betraying the fact that it's meant to transform into an HTML form element.
Instance Attribute Summary collapse
-
#attrs ⇒ Object
Returns the value of attribute attrs.
-
#instance ⇒ Object
Returns the value of attribute instance.
-
#name ⇒ Object
Returns the value of attribute name.
-
#text ⇒ Object
Returns the value of attribute text.
Instance Method Summary collapse
-
#clone ⇒ Object
Return a copy of this question, deep copying fields where appropriate.
-
#for_instance(instance) ⇒ Object
Returns a copy of this question that is bound to some object.
-
#initialize(name, text, owner = nil, attrs = {}, &instance_block) ⇒ Question
constructor
A new instance of Question.
-
#options ⇒ Array, Hash
Possible answers for the question.
-
#to_hash ⇒ Hash
Returns a hash representation of the question.
-
#to_json(opts = {}) ⇒ String
Returns a JSON object created from the question's hash representation.
Constructor Details
#initialize(name, text, owner = nil, attrs = {}, &instance_block) ⇒ Question
Returns a new instance of Question.
15 16 17 18 19 20 21 22 |
# File 'lib/interrogative/question.rb', line 15 def initialize(name, text, owner=nil, attrs={}, &instance_block) @name = name or raise ArgumentError, "A question must have a name." @text = text or raise ArgumentError, "A question must have a label." @owner = owner @attrs = attrs @instance = nil @instance_block = instance_block end |
Instance Attribute Details
#attrs ⇒ Object
Returns the value of attribute attrs.
12 13 14 |
# File 'lib/interrogative/question.rb', line 12 def attrs @attrs end |
#instance ⇒ Object
Returns the value of attribute instance.
12 13 14 |
# File 'lib/interrogative/question.rb', line 12 def instance @instance end |
#name ⇒ Object
Returns the value of attribute name.
12 13 14 |
# File 'lib/interrogative/question.rb', line 12 def name @name end |
#text ⇒ Object
Returns the value of attribute text.
12 13 14 |
# File 'lib/interrogative/question.rb', line 12 def text @text end |
Instance Method Details
#clone ⇒ Object
Return a copy of this question, deep copying fields where appropriate.
Currently, the only field that is deep copied is attrs
.
27 28 29 30 31 |
# File 'lib/interrogative/question.rb', line 27 def clone q = super q.attrs = self.attrs.clone return q end |
#for_instance(instance) ⇒ Object
Returns a copy of this question that is bound to some object.
This object will be used as the instance on which the instance_block
,
if provided, is instance_eval
ed.
37 38 39 |
# File 'lib/interrogative/question.rb', line 37 def for_instance(instance) self.clone.tap{|q| q.instance = instance } end |
#options ⇒ Array, Hash
Possible answers for the question.
If a block was passed to the initializer, then the result of
that block is returned. If the instance
argument is provided,
the block will be instance_eval
ed in the context of that
instance.
Returns nil
unless the class that included Interrogative
responds to a method called #{ name }_options
; otherwise, it
returns the result of calling that method.
Options should be either an Array or a Hash.
In the case of a Hash, the format should be { text => value }
.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/interrogative/question.rb', line 56 def if (@instance_block) if not @instance.nil? return @instance.instance_eval &@instance_block else return @instance_block.call end end return nil if @owner.nil? = "#{@name}_options".intern if @owner.respond_to? return @owner.send end end |
#to_hash ⇒ Hash
Returns a hash representation of the question.
Attributes are merged into the top level, along with :text
and
:name
. Possible options are nested under :options
.
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/interrogative/question.rb', line 79 def to_hash h = @attrs.merge({ :text => text, :name => name, }) o = h[:options] = o if not o.nil? return h end |
#to_json(opts = {}) ⇒ String
Returns a JSON object created from the question's hash representation.
95 96 97 |
# File 'lib/interrogative/question.rb', line 95 def to_json(opts={}) self.to_hash.to_json(opts) end |