Class: ArtirixDataModels::GatewayResponseAdaptors::ModelAdaptor

Inherits:
Object
  • Object
show all
Defined in:
lib/artirix_data_models/gateway_response_adaptors/model_adaptor.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object_creator) ⇒ ModelAdaptor

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}}


37
38
39
# File 'lib/artirix_data_models/gateway_response_adaptors/model_adaptor.rb', line 37

def initialize(object_creator)
  @object_creator = object_creator
end

Instance Attribute Details

#object_creatorObject (readonly)

Returns the value of attribute object_creator.



3
4
5
# File 'lib/artirix_data_models/gateway_response_adaptors/model_adaptor.rb', line 3

def object_creator
  @object_creator
end

Class Method Details

.collection(object_class_or_factory, from = 0, size = nil, dao_registry_loader = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# 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

.identityObject



43
44
45
# 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) ⇒ Object



47
48
49
# 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) ⇒ Object



51
52
53
# 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) ⇒ Object



66
67
68
# File 'lib/artirix_data_models/gateway_response_adaptors/model_adaptor.rb', line 66

def self.with_block(&block)
  new block
end

.with_callable(callable) ⇒ Object



70
71
72
# File 'lib/artirix_data_models/gateway_response_adaptors/model_adaptor.rb', line 70

def self.with_callable(callable)
  new callable
end

Instance Method Details

#call(data_hash) ⇒ Object



74
75
76
# File 'lib/artirix_data_models/gateway_response_adaptors/model_adaptor.rb', line 74

def call(data_hash)
  object_creator.call data_hash
end