Class: PubSubModelSync::PayloadBuilder
- Defined in:
- lib/pub_sub_model_sync/payload_builder.rb
Instance Attribute Summary collapse
-
#action ⇒ Object
Returns the value of attribute action.
-
#as_klass ⇒ Object
Returns the value of attribute as_klass.
-
#data ⇒ Object
Returns the value of attribute data.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#mapping ⇒ Object
Returns the value of attribute mapping.
-
#model ⇒ Object
Returns the value of attribute model.
Class Method Summary collapse
- .ordering_key_for(model) ⇒ Object
-
.parse_mapping_for(model, mapping) ⇒ Hash
Sample: parse_mapping_for(my_model, %w[id name:full_name]) ==> { id: 10, full_name: ‘model.name value’ }.
Instance Method Summary collapse
- #call ⇒ Payload
-
#initialize(model, action, settings = {}) ⇒ PayloadBuilder
constructor
A new instance of PayloadBuilder.
Methods inherited from Base
Constructor Details
#initialize(model, action, settings = {}) ⇒ PayloadBuilder
Returns a new instance of PayloadBuilder.
10 11 12 13 14 15 16 17 |
# File 'lib/pub_sub_model_sync/payload_builder.rb', line 10 def initialize(model, action, settings = {}) @model = model @action = action @data = settings[:data] || {} @mapping = settings[:mapping] || [] @headers = settings[:headers] || {} @as_klass = settings[:as_klass] || model.class.name end |
Instance Attribute Details
#action ⇒ Object
Returns the value of attribute action.
5 6 7 |
# File 'lib/pub_sub_model_sync/payload_builder.rb', line 5 def action @action end |
#as_klass ⇒ Object
Returns the value of attribute as_klass.
5 6 7 |
# File 'lib/pub_sub_model_sync/payload_builder.rb', line 5 def as_klass @as_klass end |
#data ⇒ Object
Returns the value of attribute data.
5 6 7 |
# File 'lib/pub_sub_model_sync/payload_builder.rb', line 5 def data @data end |
#headers ⇒ Object
Returns the value of attribute headers.
5 6 7 |
# File 'lib/pub_sub_model_sync/payload_builder.rb', line 5 def headers @headers end |
#mapping ⇒ Object
Returns the value of attribute mapping.
5 6 7 |
# File 'lib/pub_sub_model_sync/payload_builder.rb', line 5 def mapping @mapping end |
#model ⇒ Object
Returns the value of attribute model.
5 6 7 |
# File 'lib/pub_sub_model_sync/payload_builder.rb', line 5 def model @model end |
Class Method Details
.ordering_key_for(model) ⇒ Object
26 27 28 |
# File 'lib/pub_sub_model_sync/payload_builder.rb', line 26 def self.ordering_key_for(model) [model.class.name, model.id || SecureRandom.uuid].join('/') end |
.parse_mapping_for(model, mapping) ⇒ Hash
Sample: parse_mapping_for(my_model, %w[id name:full_name])
==> { id: 10, full_name: 'model.name value' }
35 36 37 38 39 40 |
# File 'lib/pub_sub_model_sync/payload_builder.rb', line 35 def self.parse_mapping_for(model, mapping) mapping.map do |prop| source, target = prop.to_s.split(':') [target || source, model.send(source.to_sym)] end.to_h.symbolize_keys end |
Instance Method Details
#call ⇒ Payload
20 21 22 23 24 |
# File 'lib/pub_sub_model_sync/payload_builder.rb', line 20 def call values = compute_value(data) values = self.class.parse_mapping_for(model, mapping).merge(values) PubSubModelSync::Payload.new(values, settings_data, headers_data) end |