Class: HaveAPI::ModelAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/haveapi/model_adapter.rb

Overview

Model adapters are used to automate handling of action input/output.

Adapters are chosen based on the ‘model` set on a HaveAPI::Resource. If no `model` is specified, ModelAdapters::Hash is used as a default adapter.

All model adapters are based on this class.

Defined Under Namespace

Classes: Input, Output

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.adaptersObject

Returns the value of attribute adapters.



12
13
14
# File 'lib/haveapi/model_adapter.rb', line 12

def adapters
  @adapters
end

Class Method Details

.for(layout, obj) ⇒ Object

Returns an adapter suitable for ‘layout` and `obj`. Adapters are iterated over and the first to return true to handle?() is returned.



23
24
25
26
27
28
# File 'lib/haveapi/model_adapter.rb', line 23

def for(layout, obj)
  return ModelAdapters::Hash if !obj || %i[hash hash_list].include?(layout)

  adapter = @adapters.detect { |a| a.handle?(layout, obj) }
  adapter || ModelAdapters::Hash
end

.inputObject

Shortcut to get an instance of Input model adapter.



36
37
38
# File 'lib/haveapi/model_adapter.rb', line 36

def input(*)
  self::Input.new(*)
end

.input_cleanObject

Shortcut to Input::clean.



31
32
33
# File 'lib/haveapi/model_adapter.rb', line 31

def input_clean(*)
  self::Input.clean(*)
end

.load_validators(model, params) ⇒ Object

Override this method to load validators from ‘model` to `params`.



47
# File 'lib/haveapi/model_adapter.rb', line 47

def load_validators(model, params); end

.outputObject

Shortcut to get an instance of Output model adapter.



41
42
43
# File 'lib/haveapi/model_adapter.rb', line 41

def output(*)
  self::Output.new(*)
end

.registerObject

Every model adapter must register itself using this method.



15
16
17
18
# File 'lib/haveapi/model_adapter.rb', line 15

def register
  ModelAdapter.adapters ||= []
  ModelAdapter.adapters << Kernel.const_get(to_s)
end

.used_by(direction, action) ⇒ Object

Called when mounting the API. Model adapters may use this method to add custom meta parameters to ‘action`. `direction` is one of `:input` and `:output`.



52
53
54
55
56
57
58
59
# File 'lib/haveapi/model_adapter.rb', line 52

def used_by(direction, action)
  case direction
  when :input
    self::Input.used_by(action)
  when :output
    self::Output.used_by(action)
  end
end