Skip to content

Commit

Permalink
Allow merge to have block
Browse files Browse the repository at this point in the history
  • Loading branch information
JensMeire committed Apr 19, 2023
1 parent 9a3e306 commit 47ee77d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/cache_crispies/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ def self.serialize(
end
private_class_method :serialize

def self.merge(attribute = nil, with: nil)
serialize(nil, from: attribute, with: with)
def self.merge(attribute = nil, with: nil, &block)
serialize(nil, from: attribute, with: with, &block)
end
private_class_method :merge
end
Expand Down
25 changes: 24 additions & 1 deletion spec/cache_crispies/hash_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,19 @@ def small_print
end
end

class AddressSerializer < CacheCrispies::Base
serialize :full_address do |model, _options|
"#{model.street}, #{model.city}, #{model.state} #{model.zip}"
end
end

class CerealSerializerForHashBuilder < CacheCrispies::Base
serialize :uid, from: :id, to: String
serialize :name, :company
merge :itself, with: MarketingBsSerializer
merge :itself, with: AddressSerializer do |model, _options|
model.address
end

nest_in :about do
nest_in :nutritional_information do
Expand Down Expand Up @@ -66,6 +75,14 @@ def certification
OpenStruct.new(name: 'Lactose')
]
}
let(:address) {
OpenStruct.new(
street: '123 Fake St',
city: 'San Francisco',
state: 'CA',
zip: '94103'
)
}
let(:model) {
OpenStruct.new(
id: 42,
Expand All @@ -75,7 +92,8 @@ def certification
organic: organic,
tagline: "Part of a balanced breakfast",
ingredients: ingredients,
allergies: allergies
allergies: allergies,
address: address
)
}
let(:options) { { footnote_marker: '*' } }
Expand All @@ -88,6 +106,7 @@ def certification
uid: '42',
name: 'Lucky Charms',
company: 'General Mills',
full_address: '123 Fake St, San Francisco, CA 94103',
tagline: 'Part of a balanced breakfast*',
small_print: "*this doesn't mean jack-squat",
about: {
Expand All @@ -110,6 +129,7 @@ def certification
uid: '42',
name: 'Lucky Charms',
company: 'General Mills',
full_address: '123 Fake St, San Francisco, CA 94103',
tagline: 'Part of a balanced breakfast†',
small_print: "†this doesn't mean jack-squat",
about: {
Expand All @@ -135,6 +155,7 @@ def certification
uid: '42',
name: 'Lucky Charms',
company: 'General Mills',
full_address: '123 Fake St, San Francisco, CA 94103',
tagline: 'Part of a balanced breakfast†',
small_print: "†this doesn't mean jack-squat",
about: {
Expand Down Expand Up @@ -163,6 +184,7 @@ def certification
uid: '42',
name: 'Lucky Charms',
company: 'General Mills',
full_address: '123 Fake St, San Francisco, CA 94103',
tagline: 'Part of a balanced breakfast*',
small_print: "*this doesn't mean jack-squat",
about: {
Expand Down Expand Up @@ -190,6 +212,7 @@ def certification
uid: '42',
name: 'Lucky Charms',
company: 'General Mills',
full_address: '123 Fake St, San Francisco, CA 94103',
tagline: 'Part of a balanced breakfast*',
small_print: "*this doesn't mean jack-squat",
about: {
Expand Down

0 comments on commit 47ee77d

Please sign in to comment.