Module: RoarExtensions::Representer
- Defined in:
- lib/roar_extensions/representer.rb
Instance Method Summary collapse
- #always_include_attributes ⇒ Object
- #collection(name, options = {}) ⇒ Object
- #link(name, &block) ⇒ Object
-
#property(name, options = {}) ⇒ Object
In the real API, :from and name are backwards?.
- #root_element(element) ⇒ Object
Instance Method Details
#always_include_attributes ⇒ Object
6 7 8 |
# File 'lib/roar_extensions/representer.rb', line 6 def always_include_attributes @always_include_attributes ||= [] end |
#collection(name, options = {}) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/roar_extensions/representer.rb', line 34 def collection(name, = {}) if create_object = .delete(:as) define_method("#{name}_presented") do send(name).map do |element| element && create_object.new(element).as_json end end [:from] = "#{name}_presented" end super name, end |
#link(name, &block) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/roar_extensions/representer.rb', line 47 def link(name, &block) development = (defined?(Rails) && Rails.env.development?) || ENV['RAILS_ENV'] == 'development' || ENV['ENVIRONMENT'] == 'development' if development super(name) do if path = instance_exec(&block) "#{RoarExtensions.base_url}#{path}" end end else super end end |
#property(name, options = {}) ⇒ Object
In the real API, :from and name are backwards?
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/roar_extensions/representer.rb', line 11 def property(name, = {}) .merge!(:render_nil => true) if [:always_include] always_include_attributes << name end if create_object = .delete(:as) define_method("#{name}_presented") do attr_value = send(name) attr_value && create_object.new(attr_value).as_json end [:from] = "#{name}_presented" end if from = .delete(:from) super(from, .merge(:from => name)) else super(name, ) end end |
#root_element(element) ⇒ Object
2 3 4 |
# File 'lib/roar_extensions/representer.rb', line 2 def root_element(element) self.representation_wrap = element.to_s end |