representable icon indicating copy to clipboard operation
representable copied to clipboard

Default options

Open Nerian opened this issue 12 years ago • 6 comments

It would be awesome if we could have a clean way to specify default options for all properties in a single line.

Example:

module BandRepresenter
  include Roar::Representer::JSON
  include Roar::Representer::Feature::Hypermedia

  # This is how I do it right now
  def self.property(name, options={})
    super(name, options.merge!(render_nil: true))
  end

  # This is how I would like to do it.
  default_options render_nil: true

  property :id
  property :whatever1
  property :whatever2
  property :whatever3
  property :whatever4
  property :whatever5
end

Thanks

Nerian avatar Feb 20 '13 13:02 Nerian

+1

jsmpereira avatar Feb 13 '14 10:02 jsmpereira

+1, would love this feature. We are having a fun issue:

  1. using property to create a json hash
  2. reading back the json hash from a search engine (elasticsearch)
  3. using a DelegateClass to wrap that hash..it throws errors because some methods are not defined because they got represented as nil and therefore didn't make it into the representation at all

skwp avatar Jan 23 '15 16:01 skwp

Here's the solution we used to monkeypatch Roar to define this globally https://gist.github.com/skwp/5afe7b2a238260f301ac

skwp avatar Jan 23 '15 17:01 skwp

Ok, I can see how this is helpful. The defaults will be copied to inline representers, right?

apotonick avatar Jan 23 '15 23:01 apotonick

It would be even greater (I would say it is a must) to be able to configure defaults in a global way (for all representers). I'm thinking in render_nil option, which usually is an overall API decision, so it would be also tiring to have to declare it in each representer.

waiting-for-dev avatar May 19 '15 16:05 waiting-for-dev

@waiting-for-dev Hi, did the global defaults feature get implemented?

richkettle avatar Dec 07 '16 10:12 richkettle