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
Public Class Methods
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
Shortcut to get an instance of Input
model adapter.
# File lib/haveapi/model_adapter.rb, line 36 def input(*) self::Input.new(*) end
Shortcut to Input::clean
.
# File lib/haveapi/model_adapter.rb, line 31 def input_clean(*) self::Input.clean(*) end
Override this method to load validators from ‘model` to `params`.
# File lib/haveapi/model_adapter.rb, line 47 def load_validators(model, params); end
Shortcut to get an instance of Output
model adapter.
# File lib/haveapi/model_adapter.rb, line 41 def output(*) self::Output.new(*) end
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
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