interrogative 0.2.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.3.0
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "interrogative"
8
- s.version = "0.2.2"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Adam Lloyd"]
@@ -37,8 +37,8 @@ module Interrogative
37
37
  # @option attrs [Boolean] :multiple whether the question could have
38
38
  # multiple answers.
39
39
  # @return [Question] the new Question.
40
- def question(name, text, attrs={})
41
- q = Question.new(name, text, self, attrs)
40
+ def question(name, text, attrs={}, &instance_block)
41
+ q = Question.new(name, text, self, attrs, &instance_block)
42
42
  (@_questions||=[]) << q
43
43
 
44
44
  postprocess_question(q)
@@ -56,7 +56,7 @@ module Interrogative
56
56
  # Get the array of all noted questions.
57
57
  #
58
58
  # @return [Array<Question>] array of all noted questions.
59
- def questions
59
+ def questions(instance=nil)
60
60
  qs = []
61
61
  qs |= superclass.questions if superclass.respond_to? :questions
62
62
  qs |= (@_questions||=[])
@@ -72,7 +72,7 @@ module Interrogative
72
72
 
73
73
  def questions
74
74
  qs = []
75
- qs |= self.class.questions if self.class.respond_to? :questions
75
+ qs |= self.class.questions(self) if self.class.respond_to? :questions
76
76
  qs |= (@_questions||=[])
77
77
  qs
78
78
  end
@@ -12,15 +12,21 @@ module Interrogative
12
12
  attr_accessor :name, :text, :attrs
13
13
 
14
14
  # @see Interrogative#question
15
- def initialize(name, text, owner=nil, attrs={})
15
+ def initialize(name, text, owner=nil, attrs={}, &instance_block)
16
16
  @name = name or raise ArgumentError, "A question must have a name."
17
17
  @text = text or raise ArgumentError, "A question must have a label."
18
18
  @owner = owner
19
19
  @attrs = attrs
20
+ @instance_block = instance_block
20
21
  end
21
22
 
22
23
  # Possible answers for the question.
23
24
  #
25
+ # If a block was passed to the initializer, then the result of
26
+ # that block is returned. If the `instance` argument is provided,
27
+ # the block will be `instance_eval`ed in the context of that
28
+ # instance.
29
+ #
24
30
  # Returns `nil` unless the class that included Interrogative
25
31
  # responds to a method called `#{ name }_options`; otherwise, it
26
32
  # returns the result of calling that method.
@@ -29,28 +35,43 @@ module Interrogative
29
35
  # In the case of a Hash, the format should be `{ text => value }`.
30
36
  #
31
37
  # @return [Array, Hash] the possible answers for the question.
32
- def options
38
+ def options(instance=nil)
39
+ if (@instance_block)
40
+ if not instance.nil?
41
+ return instance.instance_eval &@instance_block
42
+ else
43
+ return @instance_block.call
44
+ end
45
+ end
46
+
33
47
  return nil if @owner.nil?
34
48
 
35
49
  options_method = "#{@name}_options".intern
36
50
  if @owner.respond_to? options_method
37
- @owner.send options_method
51
+ return @owner.send options_method
38
52
  end
39
53
  end
40
54
 
55
+ # Equivalent to calling `hash_for_instance` without an instance.
56
+ #
57
+ # @see #hash_for_instance
58
+ def to_hash
59
+ hash_for_instance(nil)
60
+ end
61
+
41
62
  # Returns a hash representation of the question.
42
63
  #
43
64
  # Attributes are merged into the top level, along with `:text` and
44
65
  # `:name`. Possible options are nested under `:options`.
45
66
  #
46
67
  # @return [Hash]
47
- def to_hash
68
+ def hash_for_instance(instance=nil)
48
69
  h = @attrs.merge({
49
70
  :text => text,
50
71
  :name => name,
51
72
  })
52
73
 
53
- o = options
74
+ o = options(instance)
54
75
  h[:options] = o if not o.nil?
55
76
  return h
56
77
  end
@@ -59,7 +80,14 @@ module Interrogative
59
80
  # representation.
60
81
  #
61
82
  # @return [String]
62
- # @see #to_hash
83
+ # @see #hash_for_instance
84
+ def json_for_instance(instance=nil, opts={})
85
+ self.hash_for_instance(instance).to_json(opts)
86
+ end
87
+
88
+ # Equivalent to calling `json_for_instance` without an instance.
89
+ #
90
+ # @see #json_for_instance
63
91
  def to_json(opts={})
64
92
  self.to_hash.to_json(opts)
65
93
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: interrogative
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-05-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
16
- requirement: &70224616106960 !ruby/object:Gem::Requirement
16
+ requirement: &70347291288840 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70224616106960
24
+ version_requirements: *70347291288840
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: riot
27
- requirement: &70224616105880 !ruby/object:Gem::Requirement
27
+ requirement: &70347291287780 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70224616105880
35
+ version_requirements: *70347291287780
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: yard
38
- requirement: &70224616105320 !ruby/object:Gem::Requirement
38
+ requirement: &70347291287080 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0.7'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70224616105320
46
+ version_requirements: *70347291287080
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rdoc
49
- requirement: &70224616104320 !ruby/object:Gem::Requirement
49
+ requirement: &70347291286060 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '3.12'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70224616104320
57
+ version_requirements: *70347291286060
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: bundler
60
- requirement: &70224616120040 !ruby/object:Gem::Requirement
60
+ requirement: &70347291301860 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: 1.0.0
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70224616120040
68
+ version_requirements: *70347291301860
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: jeweler
71
- requirement: &70224616119480 !ruby/object:Gem::Requirement
71
+ requirement: &70347291301240 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ~>
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: 1.8.3
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70224616119480
79
+ version_requirements: *70347291301240
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: redcarpet
82
- requirement: &70224616118320 !ruby/object:Gem::Requirement
82
+ requirement: &70347291300180 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,7 +87,7 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *70224616118320
90
+ version_requirements: *70347291300180
91
91
  description: ! " A simple interface for keeping track of HTML-form-like questions
92
92
  without \n feeling like you're accomodating HTML forms.\n"
93
93
  email: alloyd@jibe.com
@@ -125,7 +125,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
125
125
  version: '0'
126
126
  segments:
127
127
  - 0
128
- hash: 3808566574672159458
128
+ hash: -3466753285083621786
129
129
  required_rubygems_version: !ruby/object:Gem::Requirement
130
130
  none: false
131
131
  requirements: