Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
  • 3 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
Commits on May 10, 2012
@alloy-d Don't pass instances around willy-nilly.
Instead, we'll bind questions retrieved for an instance
*to* that instance, so the old to_hash/to_json API works
as expected and as is sensible.
8ace546
@alloy-d Version bump to 0.4.0 da488f7
@alloy-d Regenerate gemspec for version 0.4.0 6921bdf
Showing with 47 additions and 34 deletions.
  1. +1 −1 VERSION
  2. +1 −1 interrogative.gemspec
  3. +29 −11 lib/interrogative.rb
  4. +16 −21 lib/interrogative/question.rb
View
2 VERSION
@@ -1 +1 @@
-0.3.0
+0.4.0
View
2 interrogative.gemspec
@@ -5,7 +5,7 @@
Gem::Specification.new do |s|
s.name = "interrogative"
- s.version = "0.3.0"
+ s.version = "0.4.0"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Adam Lloyd"]
View
40 lib/interrogative.rb
@@ -17,32 +17,35 @@ def when_questioned(&postprocessor)
#
# Useful when you need to postprocess questions at a higher level.
#
- # @param [Question] the Question.
+ # @param [Question] question the `Question`.
+ # @return [Question] the `Question`.
def postprocess_question(question)
unless @_question_postprocessors.nil?
@_question_postprocessors.each do |postprocessor|
postprocessor.call(question)
end
end
+ question
end
# Give a new question.
#
- # @param [Symbol, String] name the name (think <input name=...>) of
+ # @param [Symbol, String] name the name (think \<input name=...>) of
# the question.
- # @param [String] label the text of the question (think <label>).
+ # @param [String] label the text of the question (think \<label>).
# @param [Hash] attrs additional attributes for the question.
# @option attrs [Boolean] :long whether the question has a long answer
- # (think <textarea> vs <input>).
+ # (think \<textarea> vs \<input>).
# @option attrs [Boolean] :multiple whether the question could have
# multiple answers.
- # @return [Question] the new Question.
+ # @param [Proc] instance_block a block returning option values.
+ # @return [Question] the new `Question`.
+ # @see Question#options
def question(name, text, attrs={}, &instance_block)
q = Question.new(name, text, self, attrs, &instance_block)
(@_questions||=[]) << q
postprocess_question(q)
-
return q
end
end
@@ -53,10 +56,10 @@ def question(name, text, attrs={}, &instance_block)
module ClassMethods
include BaseMethods
- # Get the array of all noted questions.
+ # Get the array of all noted questions for this class and its superclasses.
#
# @return [Array<Question>] array of all noted questions.
- def questions(instance=nil)
+ def questions
qs = []
qs |= superclass.questions if superclass.respond_to? :questions
qs |= (@_questions||=[])
@@ -70,14 +73,30 @@ def questions(instance=nil)
module InstanceMethods
include BaseMethods
+ # Get the array of all noted questions for this instance and its class
+ # (and all of its superclasses), bound to this instance.
+ #
+ # All questions will be bound to the instance on which `questions`
+ # is called, so their `instance_block`s, if provided, will be evaluated
+ # in its context.
+ #
+ # @return [Array<Question>]
+ # @see ClassMethods#questions
+ # @see Question#options
def questions
qs = []
- qs |= self.class.questions(self) if self.class.respond_to? :questions
+ qs |= self.class.questions if self.class.respond_to? :questions
qs |= (@_questions||=[])
- qs
+ qs.map{|q| q.for_instance(self) }
end
end
+ # Gives the class `base` Interrogative's class-level methods
+ # and gives instances of `base` Interrogative's instance-level methods.
+ #
+ # Called when `Interrogative` is included.
+ #
+ # @param [Class] base the class in which `Interrogative` has been included.
def self.included(base)
base.extend(Interrogative::ClassMethods)
base.class_eval do
@@ -85,4 +104,3 @@ def self.included(base)
end
end
end
-
View
37 lib/interrogative/question.rb
@@ -9,7 +9,7 @@ module Interrogative
# betraying the fact that it's meant to transform into an HTML form
# element.
class Question
- attr_accessor :name, :text, :attrs
+ attr_accessor :name, :text, :attrs, :instance
# @see Interrogative#question
def initialize(name, text, owner=nil, attrs={}, &instance_block)
@@ -17,9 +17,18 @@ def initialize(name, text, owner=nil, attrs={}, &instance_block)
@text = text or raise ArgumentError, "A question must have a label."
@owner = owner
@attrs = attrs
+ @instance = nil
@instance_block = instance_block
end
+ # 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.
+ def for_instance(instance)
+ self.clone.tap{|q| q.instance = instance }
+ end
+
# Possible answers for the question.
#
# If a block was passed to the initializer, then the result of
@@ -35,10 +44,10 @@ def initialize(name, text, owner=nil, attrs={}, &instance_block)
# In the case of a Hash, the format should be `{ text => value }`.
#
# @return [Array, Hash] the possible answers for the question.
- def options(instance=nil)
+ def options
if (@instance_block)
- if not instance.nil?
- return instance.instance_eval &@instance_block
+ if not @instance.nil?
+ return @instance.instance_eval &@instance_block
else
return @instance_block.call
end
@@ -52,26 +61,19 @@ def options(instance=nil)
end
end
- # Equivalent to calling `hash_for_instance` without an instance.
- #
- # @see #hash_for_instance
- def to_hash
- hash_for_instance(nil)
- end
-
# 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`.
#
# @return [Hash]
- def hash_for_instance(instance=nil)
+ def to_hash
h = @attrs.merge({
:text => text,
:name => name,
})
- o = options(instance)
+ o = options
h[:options] = o if not o.nil?
return h
end
@@ -80,14 +82,7 @@ def hash_for_instance(instance=nil)
# representation.
#
# @return [String]
- # @see #hash_for_instance
- def json_for_instance(instance=nil, opts={})
- self.hash_for_instance(instance).to_json(opts)
- end
-
- # Equivalent to calling `json_for_instance` without an instance.
- #
- # @see #json_for_instance
+ # @see #to_hash
def to_json(opts={})
self.to_hash.to_json(opts)
end

No commit comments for this range

Something went wrong with that request. Please try again.