interrogative 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.2.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "interrogative"
8
- s.version = "0.1.2"
8
+ s.version = "0.2.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"]
12
- s.date = "2012-02-17"
12
+ s.date = "2012-05-10"
13
13
  s.description = " A simple interface for keeping track of HTML-form-like questions without \n feeling like you're accomodating HTML forms.\n"
14
14
  s.email = "alloyd@jibe.com"
15
15
  s.extra_rdoc_files = [
data/lib/interrogative.rb CHANGED
@@ -2,41 +2,78 @@ require 'interrogative/question'
2
2
 
3
3
  # A mixin for curious classes.
4
4
  module Interrogative
5
- # Get the array of all noted questions.
6
- #
7
- # @return [Array<Question>] array of all noted questions.
8
- def questions; @_questions; end
9
5
 
10
- # Give instructions for dealing with new questions.
6
+ # Methods applicable on both the class and instance levels.
7
+ module BaseMethods
8
+ # Give instructions for dealing with new questions.
9
+ #
10
+ # @param [Proc] postprocessor a block to run after adding a question;
11
+ # the question is given as the argument.
12
+ def when_questioned(&postprocessor)
13
+ (@_question_postprocessors||=[]) << postprocessor
14
+ end
15
+
16
+ # Give a new question.
17
+ #
18
+ # @param [Symbol, String] name the name (think <input name=...>) of
19
+ # the question.
20
+ # @param [String] label the text of the question (think <label>).
21
+ # @param [Hash] attrs additional attributes for the question.
22
+ # @option attrs [Boolean] :long whether the question has a long answer
23
+ # (think <textarea> vs <input>).
24
+ # @option attrs [Boolean] :multiple whether the question could have
25
+ # multiple answers.
26
+ # @return [Question] the new Question.
27
+ def question(name, text, attrs={})
28
+ q = Question.new(name, text, self, attrs)
29
+ (@_questions||=[]) << q
30
+
31
+ unless @_question_postprocessors.nil?
32
+ @_question_postprocessors.each do |postprocessor|
33
+ postprocessor.call(q)
34
+ end
35
+ end
36
+
37
+ return q
38
+ end
39
+ end
40
+
41
+ # Methods tailored to the class level.
11
42
  #
12
- # @param [Proc] postprocessor a block to run after adding a question;
13
- # the question is given as the argument.
14
- def when_questioned(&postprocessor)
15
- (@_question_postprocessors||=[]) << postprocessor
43
+ # These handle inheritance of questions.
44
+ module ClassMethods
45
+ include BaseMethods
46
+
47
+ # Get the array of all noted questions.
48
+ #
49
+ # @return [Array<Question>] array of all noted questions.
50
+ def questions
51
+ qs = []
52
+ qs |= superclass.questions if superclass.respond_to? :questions
53
+ qs |= (@_questions||=[])
54
+ qs
55
+ end
16
56
  end
17
57
 
18
- # Give a new question.
58
+ # Methods tailored to the instance level.
19
59
  #
20
- # @param [Symbol, String] name the name (think <input name=...>) of
21
- # the question.
22
- # @param [String] label the text of the question (think <label>).
23
- # @param [Hash] attrs additional attributes for the question.
24
- # @option attrs [Boolean] :long whether the question has a long answer
25
- # (think <textarea> vs <input>).
26
- # @option attrs [Boolean] :multiple whether the question could have
27
- # multiple answers.
28
- # @return [Question] the new Question.
29
- def question(name, text, attrs={})
30
- q = Question.new(name, text, self, attrs)
31
- (@_questions||=[]) << q
32
-
33
- unless @_question_postprocessors.nil?
34
- @_question_postprocessors.each do |postprocessor|
35
- postprocessor.call(q)
36
- end
60
+ # These handle getting questions from the class level.
61
+ module InstanceMethods
62
+ include BaseMethods
63
+
64
+ def questions
65
+ qs = []
66
+ qs |= self.class.questions if self.class.respond_to? :questions
67
+ qs |= (@_questions||=[])
68
+ qs
37
69
  end
70
+ end
38
71
 
39
- return q
72
+ def self.included(base)
73
+ base.extend(Interrogative::ClassMethods)
74
+ base.instance_eval do
75
+ include Interrogative::InstanceMethods
76
+ end
40
77
  end
41
78
  end
42
79
 
@@ -1,11 +1,77 @@
1
1
  require 'teststrap'
2
2
 
3
- context "interrogative" do
4
- setup do
5
- false
3
+ class NoQuestionsTest
4
+ include Interrogative
5
+ end
6
+
7
+ class QuestionsTest
8
+ include Interrogative
9
+
10
+ question :has_questions, "Does this have questions?"
11
+ end
12
+
13
+ class SubclassWithNoQuestionsTest < QuestionsTest
14
+ end
15
+
16
+ class SubclassWithQuestionsTest < QuestionsTest
17
+ question :is_subclass, "Is this a subclass?"
18
+ end
19
+
20
+ context "class with no questions" do
21
+ setup { NoQuestionsTest }
22
+
23
+ asserts(:questions).kind_of Array
24
+ asserts(:questions).empty
25
+ end
26
+
27
+ context "class with one question" do
28
+ context "(class)" do
29
+ setup { QuestionsTest }
30
+
31
+ denies(:questions).empty
32
+ asserts(:questions).size(1)
6
33
  end
7
34
 
8
- asserts "i'm a failure :(" do
9
- topic
35
+ context "(an instance)" do
36
+ setup { QuestionsTest.new }
37
+
38
+ denies(:questions).empty
39
+ asserts(:questions).size(1)
40
+ end
41
+
42
+ context "(an instance with a question of its own)" do
43
+ setup do
44
+ QuestionsTest.new.tap {|q| q.question :is_instance, "Is this an instance?" }
45
+ end
46
+
47
+ asserts(:questions).size(2)
48
+ end
49
+ end
50
+
51
+ context "subclass with no questions of its own" do
52
+ setup { SubclassWithNoQuestionsTest }
53
+
54
+ denies(:questions).empty
55
+ end
56
+
57
+ context "subclass with a question of its own" do
58
+ setup { SubclassWithQuestionsTest }
59
+
60
+ denies(:questions).empty
61
+ asserts(:questions).size(2)
62
+
63
+ context "(instance)" do
64
+ setup { SubclassWithQuestionsTest.new }
65
+
66
+ denies(:questions).empty
67
+ asserts(:questions).size(2)
68
+ end
69
+
70
+ context "(instance with a question of its own)" do
71
+ setup do
72
+ SubclassWithQuestionsTest.new.tap {|i| i.question :is_instance, "Is this an instance?" }
73
+ end
74
+
75
+ asserts(:questions).size(3)
10
76
  end
11
77
  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.1.2
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-17 00:00:00.000000000 Z
12
+ date: 2012-05-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
16
- requirement: &70173227943280 !ruby/object:Gem::Requirement
16
+ requirement: &70273680769440 !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: *70173227943280
24
+ version_requirements: *70273680769440
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: riot
27
- requirement: &70173227942160 !ruby/object:Gem::Requirement
27
+ requirement: &70273680785160 !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: *70173227942160
35
+ version_requirements: *70273680785160
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: yard
38
- requirement: &70173227941440 !ruby/object:Gem::Requirement
38
+ requirement: &70273680784220 !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: *70173227941440
46
+ version_requirements: *70273680784220
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rdoc
49
- requirement: &70173227940400 !ruby/object:Gem::Requirement
49
+ requirement: &70273680783100 !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: *70173227940400
57
+ version_requirements: *70273680783100
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: bundler
60
- requirement: &70173227956200 !ruby/object:Gem::Requirement
60
+ requirement: &70273680782080 !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: *70173227956200
68
+ version_requirements: *70273680782080
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: jeweler
71
- requirement: &70173227955600 !ruby/object:Gem::Requirement
71
+ requirement: &70273680780860 !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: *70173227955600
79
+ version_requirements: *70273680780860
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: redcarpet
82
- requirement: &70173227954480 !ruby/object:Gem::Requirement
82
+ requirement: &70273680779660 !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: *70173227954480
90
+ version_requirements: *70273680779660
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: -252886752722838641
128
+ hash: 3214481857536529281
129
129
  required_rubygems_version: !ruby/object:Gem::Requirement
130
130
  none: false
131
131
  requirements: