Class: Sinatra::Hat::Maker

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Actions

#create!, #destroy!, #edit!, #generate_actions!, #index!, #new!, #show!, #update!

Methods included from Responses

#serialized, #templated

Constructor Details

#initialize(model) ⇒ Maker

Returns a new instance of Maker.



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

def initialize(model)
  @model = model
  with(options)
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



5
6
7
# File 'lib/sinatras-hat/maker.rb', line 5

def context
  @context
end

#modelObject (readonly)

Returns the value of attribute model.



5
6
7
# File 'lib/sinatras-hat/maker.rb', line 5

def model
  @model
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/sinatras-hat/maker.rb', line 5

def options
  @options
end

#parentObject

Returns the value of attribute parent.



4
5
6
# File 'lib/sinatras-hat/maker.rb', line 4

def parent
  @parent
end

Instance Method Details

#authenticator(&block) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/sinatras-hat/maker.rb', line 79

def authenticator(&block)
  if block_given?
    @authenticator = block
  else
    @authenticator ||= proc { |username, password|
      credentials[:username] == username and credentials[:password] == password
    }
  end
end

#call(method, params) ⇒ Object



177
178
179
# File 'lib/sinatras-hat/maker.rb', line 177

def call(method, params)
  proxy(params).instance_exec(params, &send(method))
end

#children(*args) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/sinatras-hat/maker.rb', line 70

def children(*args)
  result = get_or_set_option(:children, args) do
    self.children = args
    self.children.uniq!
  end
  
  [result].flatten
end

#create(&block) ⇒ Object



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

def create(&block)
  if block_given?
    @create = block
  else
    @create ||= proc do |model, params|
      result = model.new
      result.attributes = params[model_name]
      result.save ? result : nil
    end
  end
end

#define(context, opts = {}, &block) ⇒ Object



14
15
16
17
18
19
# File 'lib/sinatras-hat/maker.rb', line 14

def define(context, opts={}, &block)
  @context = context
  @options.merge!(opts)
  instance_eval(&block) if block_given?
  generate_actions!
end

#destroy(&block) ⇒ Object



128
129
130
131
132
133
134
135
136
137
# File 'lib/sinatras-hat/maker.rb', line 128

def destroy(&block)
  if block_given?
    @destroy = block
  else
    @destroy ||= proc do |record, params|
      record.destroy
      :destroyed
    end
  end
end

#finder(&block) ⇒ Object



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

def finder(&block)
  if block_given?
    @finder = block
  else
    @finder ||= proc { |params| all }
  end
end

#map(name, path, opts = {}, &block) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/sinatras-hat/maker.rb', line 139

def map(name, path, opts={}, &block)
  opts[:verb] ||= :get
  klass = self
  actions[name] = Action.new(self, name, block)

  context.send(opts[:verb], resource_path(path)) do
    begin
      klass.templated(self, name, opts)
    rescue Errno::ENOENT => e
      klass.rescue_template_error(e)
    end
  end
  
  context.send(opts[:verb], "#{resource_path(path)}.:format") do
    begin
      klass.serialized(self, name, opts)
    rescue UnsupportedFormat => e
      klass.rescue_format_error(e)
    end
  end
end

#model_idObject



189
190
191
# File 'lib/sinatras-hat/maker.rb', line 189

def model_id
  "#{model_name}_id".to_sym
end

#model_nameObject



193
194
195
# File 'lib/sinatras-hat/maker.rb', line 193

def model_name
  model.name.downcase
end

#mount(klass, opts = {}, &block) ⇒ Object



21
22
23
24
25
26
# File 'lib/sinatras-hat/maker.rb', line 21

def mount(klass, opts={}, &block)
  child = Maker.new(klass)
  child.parent = self
  child.define(context, opts, &block)
  child
end

#only(*args) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sinatras-hat/maker.rb', line 56

def only(*args)
  result = get_or_set_option(:only, args) do
    self.only = args
    self.only.uniq!
  end
  
  result = Array(result)
  
  result.move_to_back(:index)
  result.move_to_front(:new, :edit)
  
  result
end

#parentsObject



41
42
43
# File 'lib/sinatras-hat/maker.rb', line 41

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

#protect(*args) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/sinatras-hat/maker.rb', line 45

def protect(*args)
  opts = args.extract_options!
  credentials.update(opts)
  actions = get_or_set_option(:protect, args) do
    self.protect += args
    self.protect.uniq!
  end
  
  [actions].flatten
end

#proxy(params = {}) ⇒ Object



181
182
183
184
185
186
187
# File 'lib/sinatras-hat/maker.rb', line 181

def proxy(params={})
  return model if parent.nil?
  fake_params = params.dup
  fake_params.merge!("id" => params[parent.model_id])
  fake_params.make_indifferent!
  parent.call(:record, fake_params).try(prefix) || model
end

#record(&block) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/sinatras-hat/maker.rb', line 97

def record(&block)
  if block_given?
    @record = block
  else
    @record ||= proc { |params| first(:id => params[:id]) }
  end
end

#rescue_format_error(e) ⇒ Object



161
162
163
164
165
166
167
168
# File 'lib/sinatras-hat/maker.rb', line 161

def rescue_format_error(e)
  throw :halt, [
    406, [
      "The `#{e.format}` format is not supported.\n",
      "Valid Formats: #{accepts.keys.join(', ')}\n",
    ].join("\n")
  ]
end

#rescue_template_error(e) ⇒ Object

TODO Create a real template for this error.



171
172
173
174
175
# File 'lib/sinatras-hat/maker.rb', line 171

def rescue_template_error(e)
  msg = "<pre>There was a problem with your view template:\n\n  "
  msg << e.message
  msg << "\n</pre>"
end

#resource_path(suffix) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/sinatras-hat/maker.rb', line 28

def resource_path(suffix)
  resources = parents + [self]
  path = resources.inject("") do |memo, maker|
    memo += eql?(maker) ?
      "/#{maker.prefix}" :
      "/#{maker.prefix}/:#{maker.model.name}_id"
  end
  (path + suffix).tap do |s|
    s.downcase!
    s.gsub!(%r(/$), '')
  end
end

#update(&block) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# File 'lib/sinatras-hat/maker.rb', line 117

def update(&block)
  if block_given?
    @update = block
  else
    @update ||= proc do |record, params|
      record.attributes = params[model_name]
      record.save ? record : false
    end
  end
end