Class: Interrogative::Question

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(name, text, owner = nil, attrs = {}, &instance_block) ⇒ Question

Returns a new instance of Question.

See Also:

  • Interrogative#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

#attrsObject

Returns the value of attribute attrs.



12
13
14
# File 'lib/interrogative/question.rb', line 12

def attrs
  @attrs
end

#instanceObject

Returns the value of attribute instance.



12
13
14
# File 'lib/interrogative/question.rb', line 12

def instance
  @instance
end

#nameObject

Returns the value of attribute name.



12
13
14
# File 'lib/interrogative/question.rb', line 12

def name
  @name
end

#textObject

Returns the value of attribute text.



12
13
14
# File 'lib/interrogative/question.rb', line 12

def text
  @text
end

Instance Method Details

#cloneObject

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_evaled.



37
38
39
# File 'lib/interrogative/question.rb', line 37

def for_instance(instance)
  self.clone.tap{|q| q.instance = instance }
end

#optionsArray, 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_evaled 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 }.

Returns:

  • (Array, Hash)

    the possible answers for the question.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/interrogative/question.rb', line 56

def options
  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?

  options_method = "#{@name}_options".intern
  if @owner.respond_to? options_method
    return @owner.send options_method
  end
end

#to_hashHash

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.

Returns:

  • (Hash)


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 = options
  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.

Returns:

  • (String)

See Also:



95
96
97
# File 'lib/interrogative/question.rb', line 95

def to_json(opts={})
  self.to_hash.to_json(opts)
end