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
# 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 { |adapter| adapter.handle?(layout, obj) }
  adapter || ModelAdapters::Hash
end

.input(*args) ⇒ Object

Shortcut to get an instance of Input model adapter.



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

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

.input_clean(*args) ⇒ Object

Shortcut to Input::clean.



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

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

.load_validators(model, params) ⇒ Object

Override this method to load validators from model to params.



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

def load_validators(model, params)

end

.output(*args) ⇒ Object

Shortcut to get an instance of Output model adapter.



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

def output(*args)
  self::Output.new(*args)
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(self.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.



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

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