class ArtirixDataModels::GatewayResponseAdaptors::ModelAdaptor

Attributes

object_creator[R]

Public Class Methods

collection(object_class_or_factory, from = 0, size = nil, dao_registry_loader = nil) click to toggle source
# File lib/artirix_data_models/gateway_response_adaptors/model_adaptor.rb, line 55
def self.collection(object_class_or_factory, from = 0, size = nil, dao_registry_loader = nil)
  size ||= ArtirixDataModels.configuration.try(:search_page_size).try(:default) || 10
  new ->(data_collection) {
    ArtirixDataModels::EsCollection.new object_class_or_factory,
                                        response:            data_collection,
                                        from:                from,
                                        size:                size,
                                        dao_registry_loader: dao_registry_loader
  }
end
identity() click to toggle source
# File lib/artirix_data_models/gateway_response_adaptors/model_adaptor.rb, line 43
def self.identity
  new ->(data_hash) { data_hash }
end
single(model_class) click to toggle source
# File lib/artirix_data_models/gateway_response_adaptors/model_adaptor.rb, line 47
def self.single(model_class)
  new ->(data_hash) { model_class.new data_hash }
end
some(model_class) click to toggle source
# File lib/artirix_data_models/gateway_response_adaptors/model_adaptor.rb, line 51
def self.some(model_class)
  new ->(data_list) { Array(data_list).map { |data_hash| model_class.new data_hash } }
end
with_block(&block) click to toggle source
# File lib/artirix_data_models/gateway_response_adaptors/model_adaptor.rb, line 66
def self.with_block(&block)
  new block
end
with_callable(callable) click to toggle source
# File lib/artirix_data_models/gateway_response_adaptors/model_adaptor.rb, line 70
def self.with_callable(callable)
  new callable
end

Private Class Methods

new(object_creator) click to toggle source

The adaptor will create an ‘object_creator` callable object. That callable object will be called with a `data_hash` as only argument. The purpose of that callable object is to create an object with the given data.

3 options:

1) object_class_or_creator with the Class of the object to be initiated

sma = ModelAdaptor.single(MyModel)
res = sma.call({a: 1})
res.class           # => MyModel
res                 # => <MyModel a=1>

2) block with 1 argument

sma = ModelAdaptor.with_block do |data|
  { something: data }
end

res = sma.call({a: 1})
res.class           # => Hash
res                 # => {something: {a: 1}}

3) callable object (respond to ‘call`), like a lambda

sma = ModelAdaptor.with_callable( ->(data) { { something: data } })

res = sma.call({a: 1})
res.class           # => Hash
res                 # => {something: {a: 1}}
# File lib/artirix_data_models/gateway_response_adaptors/model_adaptor.rb, line 37
def initialize(object_creator)
  @object_creator = object_creator
end

Public Instance Methods

call(data_hash) click to toggle source
# File lib/artirix_data_models/gateway_response_adaptors/model_adaptor.rb, line 74
def call(data_hash)
  object_creator.call data_hash
end