Class: Sinatra::Hat::Model

Inherits:
Object show all
Defined in:
lib/sinatras-hat/model.rb

Overview

A wrapper around the model class that we’re mounting

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#makerObject (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!
  options[: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!
  options[: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)
  options[:record].call(proxy(params), params)
end

#foreign_keyObject



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

#pluralObject



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

def plural
  klass.name.snake_case.plural
end

#singularObject



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