interrogative 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/interrogative.gemspec +1 -1
- data/lib/interrogative.rb +29 -11
- data/lib/interrogative/question.rb +16 -21
- metadata +16 -16
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
data/interrogative.gemspec
CHANGED
data/lib/interrogative.rb
CHANGED
@@ -17,32 +17,35 @@ module Interrogative
|
|
17
17
|
#
|
18
18
|
# Useful when you need to postprocess questions at a higher level.
|
19
19
|
#
|
20
|
-
# @param [Question] the Question
|
20
|
+
# @param [Question] question the `Question`.
|
21
|
+
# @return [Question] the `Question`.
|
21
22
|
def postprocess_question(question)
|
22
23
|
unless @_question_postprocessors.nil?
|
23
24
|
@_question_postprocessors.each do |postprocessor|
|
24
25
|
postprocessor.call(question)
|
25
26
|
end
|
26
27
|
end
|
28
|
+
question
|
27
29
|
end
|
28
30
|
|
29
31
|
# Give a new question.
|
30
32
|
#
|
31
|
-
# @param [Symbol, String] name the name (think
|
33
|
+
# @param [Symbol, String] name the name (think \<input name=...>) of
|
32
34
|
# the question.
|
33
|
-
# @param [String] label the text of the question (think
|
35
|
+
# @param [String] label the text of the question (think \<label>).
|
34
36
|
# @param [Hash] attrs additional attributes for the question.
|
35
37
|
# @option attrs [Boolean] :long whether the question has a long answer
|
36
|
-
# (think
|
38
|
+
# (think \<textarea> vs \<input>).
|
37
39
|
# @option attrs [Boolean] :multiple whether the question could have
|
38
40
|
# multiple answers.
|
39
|
-
# @
|
41
|
+
# @param [Proc] instance_block a block returning option values.
|
42
|
+
# @return [Question] the new `Question`.
|
43
|
+
# @see Question#options
|
40
44
|
def question(name, text, attrs={}, &instance_block)
|
41
45
|
q = Question.new(name, text, self, attrs, &instance_block)
|
42
46
|
(@_questions||=[]) << q
|
43
47
|
|
44
48
|
postprocess_question(q)
|
45
|
-
|
46
49
|
return q
|
47
50
|
end
|
48
51
|
end
|
@@ -53,10 +56,10 @@ module Interrogative
|
|
53
56
|
module ClassMethods
|
54
57
|
include BaseMethods
|
55
58
|
|
56
|
-
# Get the array of all noted questions.
|
59
|
+
# Get the array of all noted questions for this class and its superclasses.
|
57
60
|
#
|
58
61
|
# @return [Array<Question>] array of all noted questions.
|
59
|
-
def questions
|
62
|
+
def questions
|
60
63
|
qs = []
|
61
64
|
qs |= superclass.questions if superclass.respond_to? :questions
|
62
65
|
qs |= (@_questions||=[])
|
@@ -70,14 +73,30 @@ module Interrogative
|
|
70
73
|
module InstanceMethods
|
71
74
|
include BaseMethods
|
72
75
|
|
76
|
+
# Get the array of all noted questions for this instance and its class
|
77
|
+
# (and all of its superclasses), bound to this instance.
|
78
|
+
#
|
79
|
+
# All questions will be bound to the instance on which `questions`
|
80
|
+
# is called, so their `instance_block`s, if provided, will be evaluated
|
81
|
+
# in its context.
|
82
|
+
#
|
83
|
+
# @return [Array<Question>]
|
84
|
+
# @see ClassMethods#questions
|
85
|
+
# @see Question#options
|
73
86
|
def questions
|
74
87
|
qs = []
|
75
|
-
qs |= self.class.questions
|
88
|
+
qs |= self.class.questions if self.class.respond_to? :questions
|
76
89
|
qs |= (@_questions||=[])
|
77
|
-
qs
|
90
|
+
qs.map{|q| q.for_instance(self) }
|
78
91
|
end
|
79
92
|
end
|
80
93
|
|
94
|
+
# Gives the class `base` Interrogative's class-level methods
|
95
|
+
# and gives instances of `base` Interrogative's instance-level methods.
|
96
|
+
#
|
97
|
+
# Called when `Interrogative` is included.
|
98
|
+
#
|
99
|
+
# @param [Class] base the class in which `Interrogative` has been included.
|
81
100
|
def self.included(base)
|
82
101
|
base.extend(Interrogative::ClassMethods)
|
83
102
|
base.class_eval do
|
@@ -85,4 +104,3 @@ module Interrogative
|
|
85
104
|
end
|
86
105
|
end
|
87
106
|
end
|
88
|
-
|
@@ -9,7 +9,7 @@ module Interrogative
|
|
9
9
|
# betraying the fact that it's meant to transform into an HTML form
|
10
10
|
# element.
|
11
11
|
class Question
|
12
|
-
attr_accessor :name, :text, :attrs
|
12
|
+
attr_accessor :name, :text, :attrs, :instance
|
13
13
|
|
14
14
|
# @see Interrogative#question
|
15
15
|
def initialize(name, text, owner=nil, attrs={}, &instance_block)
|
@@ -17,9 +17,18 @@ module Interrogative
|
|
17
17
|
@text = text or raise ArgumentError, "A question must have a label."
|
18
18
|
@owner = owner
|
19
19
|
@attrs = attrs
|
20
|
+
@instance = nil
|
20
21
|
@instance_block = instance_block
|
21
22
|
end
|
22
23
|
|
24
|
+
# Returns a copy of this question that is bound to some object.
|
25
|
+
#
|
26
|
+
# This object will be used as the instance on which the `instance_block`,
|
27
|
+
# if provided, is `instance_eval`ed.
|
28
|
+
def for_instance(instance)
|
29
|
+
self.clone.tap{|q| q.instance = instance }
|
30
|
+
end
|
31
|
+
|
23
32
|
# Possible answers for the question.
|
24
33
|
#
|
25
34
|
# If a block was passed to the initializer, then the result of
|
@@ -35,10 +44,10 @@ module Interrogative
|
|
35
44
|
# In the case of a Hash, the format should be `{ text => value }`.
|
36
45
|
#
|
37
46
|
# @return [Array, Hash] the possible answers for the question.
|
38
|
-
def options
|
47
|
+
def options
|
39
48
|
if (@instance_block)
|
40
|
-
if not instance.nil?
|
41
|
-
return instance.instance_eval &@instance_block
|
49
|
+
if not @instance.nil?
|
50
|
+
return @instance.instance_eval &@instance_block
|
42
51
|
else
|
43
52
|
return @instance_block.call
|
44
53
|
end
|
@@ -52,26 +61,19 @@ module Interrogative
|
|
52
61
|
end
|
53
62
|
end
|
54
63
|
|
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
|
-
|
62
64
|
# Returns a hash representation of the question.
|
63
65
|
#
|
64
66
|
# Attributes are merged into the top level, along with `:text` and
|
65
67
|
# `:name`. Possible options are nested under `:options`.
|
66
68
|
#
|
67
69
|
# @return [Hash]
|
68
|
-
def
|
70
|
+
def to_hash
|
69
71
|
h = @attrs.merge({
|
70
72
|
:text => text,
|
71
73
|
:name => name,
|
72
74
|
})
|
73
75
|
|
74
|
-
o = options
|
76
|
+
o = options
|
75
77
|
h[:options] = o if not o.nil?
|
76
78
|
return h
|
77
79
|
end
|
@@ -80,14 +82,7 @@ module Interrogative
|
|
80
82
|
# representation.
|
81
83
|
#
|
82
84
|
# @return [String]
|
83
|
-
# @see #
|
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
|
85
|
+
# @see #to_hash
|
91
86
|
def to_json(opts={})
|
92
87
|
self.to_hash.to_json(opts)
|
93
88
|
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.
|
4
|
+
version: 0.4.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: &
|
16
|
+
requirement: &70102326682260 !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: *
|
24
|
+
version_requirements: *70102326682260
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: riot
|
27
|
-
requirement: &
|
27
|
+
requirement: &70102326681220 !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: *
|
35
|
+
version_requirements: *70102326681220
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: yard
|
38
|
-
requirement: &
|
38
|
+
requirement: &70102326680580 !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: *
|
46
|
+
version_requirements: *70102326680580
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rdoc
|
49
|
-
requirement: &
|
49
|
+
requirement: &70102326679560 !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: *
|
57
|
+
version_requirements: *70102326679560
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: bundler
|
60
|
-
requirement: &
|
60
|
+
requirement: &70102326695340 !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: *
|
68
|
+
version_requirements: *70102326695340
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: jeweler
|
71
|
-
requirement: &
|
71
|
+
requirement: &70102326694720 !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: *
|
79
|
+
version_requirements: *70102326694720
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: redcarpet
|
82
|
-
requirement: &
|
82
|
+
requirement: &70102326693660 !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: *
|
90
|
+
version_requirements: *70102326693660
|
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: -
|
128
|
+
hash: -1066373110683406095
|
129
129
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
130
|
none: false
|
131
131
|
requirements:
|