blueprinter icon indicating copy to clipboard operation
blueprinter copied to clipboard

Allow pasing :method option to `field`

Open ghost opened this issue 4 years ago • 1 comments

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.

ghost avatar May 12 '20 20:05 ghost

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.

AllPurposeName avatar May 15 '20 22:05 AllPurposeName

Closing this as the original author's account has been deleted.

lessthanjacob avatar Sep 28 '23 14:09 lessthanjacob