Class: Sinatra::Hat::Model
Overview
A wrapper around the model class that we’re mounting
Instance Attribute Summary collapse
-
#maker ⇒ Object
readonly
Returns the value of attribute maker.
Instance Method Summary collapse
- #all(params) ⇒ Object
- #find(params) ⇒ Object
- #find_owner(params) ⇒ Object
- #foreign_key ⇒ Object
-
#initialize(maker) ⇒ Model
constructor
A new instance of Model.
- #new(params = {}) ⇒ Object
- #plural ⇒ Object
- #singular ⇒ Object
- #update(params) ⇒ Object
Constructor Details
#initialize(maker) ⇒ Model
Returns a new instance of Model.
9 10 11 |
# File 'lib/sinatras-hat/model.rb', line 9 def initialize(maker) @maker = maker end |
Instance Attribute Details
#maker ⇒ Object (readonly)
Returns the value of attribute maker.
5 6 7 |
# File 'lib/sinatras-hat/model.rb', line 5 def maker @maker end |
Instance Method Details
#all(params) ⇒ Object
13 14 15 16 |
# File 'lib/sinatras-hat/model.rb', line 13 def all(params) params.make_indifferent! [:finder].call(proxy(params), params) end |
#find(params) ⇒ Object
18 19 20 21 |
# File 'lib/sinatras-hat/model.rb', line 18 def find(params) params.make_indifferent! [:record].call(proxy(params), params) end |
#find_owner(params) ⇒ Object
23 24 25 26 |
# File 'lib/sinatras-hat/model.rb', line 23 def find_owner(params) params = parent_params(params) [:record].call(proxy(params), params) end |
#foreign_key ⇒ Object
49 50 51 |
# File 'lib/sinatras-hat/model.rb', line 49 def foreign_key "#{singular}_id".to_sym end |
#new(params = {}) ⇒ Object
36 37 38 39 |
# File 'lib/sinatras-hat/model.rb', line 36 def new(params={}) params.nest! proxy(params).new(params[singular] || { }) end |
#plural ⇒ Object
41 42 43 |
# File 'lib/sinatras-hat/model.rb', line 41 def plural klass.name.snake_case.plural end |
#singular ⇒ Object
45 46 47 |
# File 'lib/sinatras-hat/model.rb', line 45 def singular klass.name.snake_case.singular end |
#update(params) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/sinatras-hat/model.rb', line 28 def update(params) if record = find(params) params.nest! record.attributes = (params[singular] || { }) record end end |