Class: ArtirixDataModels::GatewayResponseAdaptors::ModelAdaptor
- Inherits:
-
Object
- Object
- ArtirixDataModels::GatewayResponseAdaptors::ModelAdaptor
- Defined in:
- lib/artirix_data_models/gateway_response_adaptors/model_adaptor.rb
Instance Attribute Summary collapse
-
#object_creator ⇒ Object
readonly
Returns the value of attribute object_creator.
Class Method Summary collapse
- .collection(object_class_or_factory, from = 0, size = nil) ⇒ Object
- .identity ⇒ Object
- .single(model_class) ⇒ Object
- .some(model_class) ⇒ Object
- .with_block(&block) ⇒ Object
- .with_callable(callable) ⇒ Object
Instance Method Summary collapse
- #call(data_hash) ⇒ Object
-
#initialize(object_creator) ⇒ ModelAdaptor
constructor
The adaptor will create an ‘object_creator` callable object.
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_creator ⇒ Object (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) ⇒ Object
55 56 57 58 |
# File 'lib/artirix_data_models/gateway_response_adaptors/model_adaptor.rb', line 55 def self.collection(object_class_or_factory, from = 0, size = 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 } end |
.identity ⇒ Object
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
60 61 62 |
# File 'lib/artirix_data_models/gateway_response_adaptors/model_adaptor.rb', line 60 def self.with_block(&block) new block end |
.with_callable(callable) ⇒ Object
64 65 66 |
# File 'lib/artirix_data_models/gateway_response_adaptors/model_adaptor.rb', line 64 def self.with_callable(callable) new callable end |
Instance Method Details
#call(data_hash) ⇒ Object
68 69 70 |
# File 'lib/artirix_data_models/gateway_response_adaptors/model_adaptor.rb', line 68 def call(data_hash) object_creator.call data_hash end |