Class: Sinatra::Hat::Maker

Inherits:
Object show all
Includes:
Authorization::Helpers, Actions, Extendor
Defined in:
lib/sinatras-hat/maker.rb

Overview

This is where it all comes together

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Actions

included

Methods included from Authorization::Helpers

#authorized?, #protect!

Methods included from Extendor

#mount

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
  options.merge!(overrides)
  with(options)
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



8
9
10
# File 'lib/sinatras-hat/maker.rb', line 8

def app
  @app
end

#klassObject (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, options={}, &block)
  verb = options[:verb] || :get
  Router.cache << [verb, name, path]
  actions[name] = { :path => path, :verb => verb, :fn => block }
end

.actionsObject



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

Yields:



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?
    options[:authenticator] = block
  else
    options[: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?
    options[:finder] = block
  else
    options[: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

#inspectObject



117
118
119
# File 'lib/sinatras-hat/maker.rb', line 117

def inspect
  "maker: #{klass}"
end

#loggerObject

TODO Hook this into Rack::CommonLogger



134
135
136
# File 'lib/sinatras-hat/maker.rb', line 134

def logger
  @logger ||= Logger.new(self)
end

#modelObject



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?
    options[:only] ||= Set.new(options[:only])
  else
    Set.new(options[:only] = actions)
  end
end

#optionsObject



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/sinatras-hat/maker.rb', line 104

def options
  @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

#parentsObject



96
97
98
# File 'lib/sinatras-hat/maker.rb', line 96

def parents
  @parents ||= parent ? Array(parent) + parent.parents : []
end

#prefixObject



92
93
94
# File 'lib/sinatras-hat/maker.rb', line 92

def prefix
  options[: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.extract_options!)
  
  if actions.empty?
    options[:protect] ||= Set.new([])
  else
    actions == [:all] ? 
      Set.new(options[:protect] = only) :
      Set.new(options[: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?
    options[:record] = block
  else
    options[: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

#responderObject



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