Permalink
Browse files

Cleaned up duplicated option setter/getter logic.

  • Loading branch information...
1 parent 0011802 commit 1827fe3cbe51a06f2877737ea6e5e4145762ef9b @nakajima committed Feb 5, 2009
Showing with 38 additions and 62 deletions.
  1. +35 −55 lib/sinatras-hat/maker.rb
  2. +3 −7 spec/maker_spec.rb
View
@@ -17,10 +17,44 @@ def self.action(name, path, options={}, &block)
actions[name] = { :path => path, :verb => verb, :fn => block }
end
+ def self.option_setter(name, options={})
+ setter = options[:collection] ? 'Set.new(args)' : 'args.first'
+ class_eval(<<-END, __FILE__, __LINE__)
+ def #{name}(*args, &block)
+ return options[#{name.inspect}] = block if block_given?
+ return options[#{name.inspect}] = #{setter} unless args.empty?
+ return options[#{name.inspect}]
+ end
+ END
+ end
+
include Sinatra::Hat::Actions
# ======================================================
+ # The finder block is used when loading all records for the index
+ # action. It gets passed the model proxy and the request params hash.
+ option_setter :finder
+
+ # The finder block is used when loading a single record, which
+ # is the case for most actions. It gets passed the model proxy
+ # and the request params hash.
+ option_setter :record
+
+ # The authenticator block gets called before protected actions. It
+ # gets passed the basic auth username and password.
+ option_setter :authenticator
+
+ # A list of actions that get generated by this maker instance. By
+ # default it's all of the actions specified in actions.rb
+ option_setter :only, :collection => true
+
+ # A way to determine a record's representation in the database
+ option_setter :to_param
+
+ # The path prefix to use for generating groutes.
+ option_setter :prefix
+
def initialize(klass, overrides={})
@klass = klass
options.merge!(overrides)
@@ -51,56 +85,6 @@ def after(action)
yield HashMutator.new(responder.defaults[action])
end
- # The finder block is used when loading all records for the index
- # action. It gets passed the model proxy and the request params hash.
- def finder(&block)
- if block_given?
- options[:finder] = block
- else
- options[:finder]
- end
- end
-
- # The finder block is used when loading a single record, which
- # is the case for most actions. It gets passed the model proxy
- # and the request params hash.
- def record(&block)
- if block_given?
- options[:record] = block
- else
- options[:record]
- end
- end
-
- # The authenticator block gets called before protected actions. It
- # gets passed the basic auth username and password.
- def authenticator(&block)
- if block_given?
- options[:authenticator] = block
- else
- options[:authenticator]
- end
- end
-
- # A list of actions that get generated by this maker instance. By
- # default it's all of the actions specified in actions.rb
- def only(*actions)
- if actions.empty?
- options[:only] ||= Set.new(options[:only])
- else
- Set.new(options[:only] = actions)
- end
- end
-
- # A way to determine a record's representation in the database
- def to_param(name=nil)
- if name
- options[:to_param] = name
- else
- options[:to_param]
- end
- end
-
# A list of actions to protect via basic auth. Protected actions
# will have the authenticator block called before they are handled.
def protect(*actions)
@@ -115,11 +99,6 @@ def protect(*actions)
end
end
- # The path prefix to use for routes and such.
- def prefix(name = nil)
- options[:prefix] ||= (name || model.plural)
- end
-
# An array of parent Maker instances under which this instance
# was nested.
def parents
@@ -137,6 +116,7 @@ def options
@options ||= {
:only => Set.new(Maker.actions.keys),
:parent => nil,
+ :prefix => model.plural,
:finder => proc { |model, params| model.all },
:record => proc { |model, params| model.send("find_by_#{to_param}", params[:id]) },
:protect => [ ],
View
@@ -133,7 +133,9 @@ class Comment; end
it "has methodized setter" do
maker.only :index, :show
- maker.only.should == [:index, :show]
+ maker.only.should have(2).entries
+ maker.only.should include(:index)
+ maker.only.should include(:show)
end
end
@@ -280,12 +282,6 @@ class Comment; end
maker.prefix.should == 'super/heroes'
maker.prefix.should == maker.options[:prefix]
end
-
- it "memoizes the value" do
- maker.prefix.should == 'articles'
- maker.prefix 'secret/agents'
- maker.prefix.should_not == 'secret/agents'
- end
end
context "when specified as an option" do

0 comments on commit 1827fe3

Please sign in to comment.