Sha256: 6af1ef1c4143ed2b49bafae7c42bd9c47bbe0ae4c9c50dbd6f87e21f8f53e95a

Contents?: true

Size: 1.45 KB

Versions: 4

Compression:

Stored size: 1.45 KB

Contents

module RoarExtensions::Representer
  def root_element(element)
    self.representation_wrap = element.to_s
  end

  def always_include_attributes
    @always_include_attributes ||= []
  end

  # In the real API, :from and name are backwards?
  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

  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

  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
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
roar-extensions-0.1.0 lib/roar_extensions/representer.rb
roar-extensions-0.0.4 lib/roar_extensions/representer.rb
roar-extensions-0.0.3 lib/roar_extensions/representer.rb
roar-extensions-0.0.2 lib/roar_extensions/representer.rb