blueprinter
blueprinter copied to clipboard
Allow pasing :method option to `field`
This change allows us to write our blueprints in terms of the JSON structure that we expect:
Before:
class MyBlueprint < Blueprint::Base
identifier :id
field :name
field :item_number, name: :position
end
After:
class MyBlueprint < Blueprint::Base
identifier :id
field :name
field :position, method: :item_number
end
Both blueprints would output
{
id: 1,
name: "A name",
position: 123
}
I think the latter method seems preferable and more readable as it reflects what keys are supposed to show up in the JSON result.
This change should not break existing blueprint structures; it just allows more flexibility for those who prefer to write their blueprints the way that they expect the JSON object should appear.
This is much more intuitive for me. Another point for it is when you switch a name field around to a block defined field it is easier.
# Replacing
# field :item_number, name: :position
field :position do |my_bloop|
my_bloop.try(:item_number) || my_bloop[:item_number]
end
# Replacing
# field :position, method: :item_number
field :position do |my_bloop|
my_bloop.try(:item_number) || my_bloop[:item_number]
end
That one always gets me.
Closing this as the original author's account has been deleted.