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
Loads all records using the maker’s :finder option.
-
#find(params) ⇒ Object
Loads one record using the maker’s :record option.
-
#find_last_modified(records) ⇒ Object
Returns the last modified record from the array of records passed in.
-
#find_owner(params) ⇒ Object
Finds the owner record of a nested resource.
-
#foreign_key ⇒ Object
Returns the foreign_key to be used for this model.
-
#initialize(maker) ⇒ Model
constructor
A new instance of Model.
-
#new(params = {}) ⇒ Object
Returns a new instance of the mounted model.
-
#plural ⇒ Object
Returns the pluralized name for the model.
-
#singular ⇒ Object
Returns the singularized name for the model.
-
#update(params) ⇒ Object
Updates a record with the given params.
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
Loads all records using the maker’s :finder option.
14 15 16 17 |
# File 'lib/sinatras-hat/model.rb', line 14 def all(params) params.make_indifferent! [:finder].call(proxy(params), params) end |
#find(params) ⇒ Object
Loads one record using the maker’s :record option.
20 21 22 23 |
# File 'lib/sinatras-hat/model.rb', line 20 def find(params) params.make_indifferent! [:record].call(proxy(params), params) end |
#find_last_modified(records) ⇒ Object
Returns the last modified record from the array of records passed in. It’s thorougly inefficient, since it requires all of the cacheable data to be loaded anyway.
64 65 66 67 68 69 70 |
# File 'lib/sinatras-hat/model.rb', line 64 def find_last_modified(records) if records.all? { |r| r.respond_to?(:updated_at) } records.sort_by { |r| r.updated_at }.last else records.last end end |
#find_owner(params) ⇒ Object
Finds the owner record of a nested resource.
26 27 28 29 |
# File 'lib/sinatras-hat/model.rb', line 26 def find_owner(params) params = parent_params(params) [:record].call(proxy(params), params) end |
#foreign_key ⇒ Object
Returns the foreign_key to be used for this model.
57 58 59 |
# File 'lib/sinatras-hat/model.rb', line 57 def foreign_key "#{singular}_id".to_sym end |
#new(params = {}) ⇒ Object
Returns a new instance of the mounted model.
41 42 43 44 |
# File 'lib/sinatras-hat/model.rb', line 41 def new(params={}) params.nest! proxy(params).new(params[singular] || { }) end |
#plural ⇒ Object
Returns the pluralized name for the model.
47 48 49 |
# File 'lib/sinatras-hat/model.rb', line 47 def plural klass.name.snake_case.plural end |
#singular ⇒ Object
Returns the singularized name for the model.
52 53 54 |
# File 'lib/sinatras-hat/model.rb', line 52 def singular klass.name.snake_case.singular end |
#update(params) ⇒ Object
Updates a record with the given params.
32 33 34 35 36 37 38 |
# File 'lib/sinatras-hat/model.rb', line 32 def update(params) if record = find(params) params.nest! record.attributes = (params[singular] || { }) record end end |