class HaveAPI::ModelAdapter

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.

Attributes

adapters[RW]

Public Class Methods

for(layout, obj) click to toggle source

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

# 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
input(*) click to toggle source

Shortcut to get an instance of Input model adapter.

# File lib/haveapi/model_adapter.rb, line 36
def input(*)
  self::Input.new(*)
end
input_clean(*) click to toggle source

Shortcut to Input::clean.

# File lib/haveapi/model_adapter.rb, line 31
def input_clean(*)
  self::Input.clean(*)
end
load_validators(model, params) click to toggle source

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

# File lib/haveapi/model_adapter.rb, line 47
def load_validators(model, params); end
output(*) click to toggle source

Shortcut to get an instance of Output model adapter.

# File lib/haveapi/model_adapter.rb, line 41
def output(*)
  self::Output.new(*)
end
register() click to toggle source

Every model adapter must register itself using this method.

# File lib/haveapi/model_adapter.rb, line 15
def register
  ModelAdapter.adapters ||= []
  ModelAdapter.adapters << Kernel.const_get(to_s)
end
used_by(direction, action) click to toggle source

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`.

# 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