Class: Sinatra::Hat::Maker
- Includes:
- Authorization::Helpers, Actions, Extendor
- Defined in:
- lib/sinatras-hat/maker.rb
Overview
This is where it all comes together
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
Class Method Summary collapse
-
.action(name, path, options = {}, &block) ⇒ Object
enables the douche-y DSL you see in actions.rb.
- .actions ⇒ Object
Instance Method Summary collapse
- #after(action) {|HashMutator.new(responder.defaults[action])| ... } ⇒ Object
- #authenticator(&block) ⇒ Object
- #finder(&block) ⇒ Object
- #generate_routes! ⇒ Object
- #handle(action, request) ⇒ Object
-
#initialize(klass, overrides = {}) ⇒ Maker
constructor
.
- #inspect ⇒ Object
-
#logger ⇒ Object
TODO Hook this into Rack::CommonLogger.
- #model ⇒ Object
- #only(*actions) ⇒ Object
- #options ⇒ Object
- #parents ⇒ Object
- #prefix ⇒ Object
- #protect(*actions) ⇒ Object
- #record(&block) ⇒ Object
- #resource_path(*args) ⇒ Object
- #responder ⇒ Object
- #setup(app) ⇒ Object
Methods included from Actions
Methods included from Authorization::Helpers
Methods included from Extendor
Constructor Details
#initialize(klass, overrides = {}) ⇒ Maker
25 26 27 28 29 |
# File 'lib/sinatras-hat/maker.rb', line 25 def initialize(klass, overrides={}) @klass = klass .merge!(overrides) with() end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
8 9 10 |
# File 'lib/sinatras-hat/maker.rb', line 8 def app @app end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
8 9 10 |
# File 'lib/sinatras-hat/maker.rb', line 8 def klass @klass end |
Class Method Details
.action(name, path, options = {}, &block) ⇒ Object
enables the douche-y DSL you see in actions.rb
15 16 17 18 19 |
# File 'lib/sinatras-hat/maker.rb', line 15 def self.action(name, path, ={}, &block) verb = [:verb] || :get Router.cache << [verb, name, path] actions[name] = { :path => path, :verb => verb, :fn => block } end |
.actions ⇒ Object
10 11 12 |
# File 'lib/sinatras-hat/maker.rb', line 10 def self.actions @actions ||= { } end |
Instance Method Details
#after(action) {|HashMutator.new(responder.defaults[action])| ... } ⇒ Object
44 45 46 |
# File 'lib/sinatras-hat/maker.rb', line 44 def after(action) yield HashMutator.new(responder.defaults[action]) end |
#authenticator(&block) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/sinatras-hat/maker.rb', line 64 def authenticator(&block) if block_given? [:authenticator] = block else [:authenticator] end end |
#finder(&block) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/sinatras-hat/maker.rb', line 48 def finder(&block) if block_given? [:finder] = block else [:finder] end end |
#generate_routes! ⇒ Object
121 122 123 |
# File 'lib/sinatras-hat/maker.rb', line 121 def generate_routes! Router.new(self).generate(@app) end |
#handle(action, request) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/sinatras-hat/maker.rb', line 35 def handle(action, request) request.error(404) unless only.include?(action) protect!(request) if protect.include?(action) log_with_benchmark(request, action) do instance_exec(request, &self.class.actions[action][:fn]) end end |
#inspect ⇒ Object
117 118 119 |
# File 'lib/sinatras-hat/maker.rb', line 117 def inspect "maker: #{klass}" end |
#logger ⇒ Object
TODO Hook this into Rack::CommonLogger
134 135 136 |
# File 'lib/sinatras-hat/maker.rb', line 134 def logger @logger ||= Logger.new(self) end |
#model ⇒ Object
129 130 131 |
# File 'lib/sinatras-hat/maker.rb', line 129 def model @model ||= Model.new(self) end |
#only(*actions) ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/sinatras-hat/maker.rb', line 72 def only(*actions) if actions.empty? [:only] ||= Set.new([:only]) else Set.new([:only] = actions) end end |
#options ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/sinatras-hat/maker.rb', line 104 def @options ||= { :only => Set.new(Maker.actions.keys), :parent => nil, :finder => proc { |model, params| model.all }, :record => proc { |model, params| model.find_by_id(params[:id]) }, :protect => [ ], :formats => { }, :credentials => { :username => 'username', :password => 'password', :realm => "The App" }, :authenticator => proc { |username, password| [username, password] == [:username, :password].map(&credentials.method(:[])) } } end |
#parents ⇒ Object
96 97 98 |
# File 'lib/sinatras-hat/maker.rb', line 96 def parents @parents ||= parent ? Array(parent) + parent.parents : [] end |
#prefix ⇒ Object
92 93 94 |
# File 'lib/sinatras-hat/maker.rb', line 92 def prefix [:prefix] ||= model.plural end |
#protect(*actions) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/sinatras-hat/maker.rb', line 80 def protect(*actions) credentials.merge!(actions.) if actions.empty? [:protect] ||= Set.new([]) else actions == [:all] ? Set.new([:protect] = only) : Set.new([:protect] = actions) end end |
#record(&block) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/sinatras-hat/maker.rb', line 56 def record(&block) if block_given? [:record] = block else [:record] end end |
#resource_path(*args) ⇒ Object
100 101 102 |
# File 'lib/sinatras-hat/maker.rb', line 100 def resource_path(*args) resource.path(*args) end |
#responder ⇒ Object
125 126 127 |
# File 'lib/sinatras-hat/maker.rb', line 125 def responder @responder ||= Responder.new(self) end |
#setup(app) ⇒ Object
31 32 33 |
# File 'lib/sinatras-hat/maker.rb', line 31 def setup(app) @app = app end |