Module: RoarExtensions::Representer

Defined in:
lib/roar_extensions/representer.rb

Instance Method Summary collapse

Instance Method Details

#always_include_attributesObject



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, options = {})
  if create_object = options.delete(:as)
    define_method("#{name}_presented") do
      send(name).map do |element|
        element && create_object.new(element).as_json
      end
    end
    options[:from] = "#{name}_presented"
  end

  super name, options
end


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, options = {})
  options.merge!(:render_nil => true)

  if options[:always_include]
    always_include_attributes << name
  end

  if create_object = options.delete(:as)
    define_method("#{name}_presented") do
      attr_value = send(name)
      attr_value && create_object.new(attr_value).as_json
    end

    options[:from] = "#{name}_presented"
  end

  if from = options.delete(:from)
    super(from, options.merge(:from => name))
  else
    super(name, options)
  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