Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow merge to have block #71

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment on lines +37 to +39
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain the advantage of doing this over just using the attribute directly as described below?

merge :address, with: AddressSerializer


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