module AsJsonRepresentations

Constants

QUERY_METHODS
VERSION

Public Class Methods

included(base) click to toggle source
# File lib/as_json_representations.rb, line 57
def self.included(base)
  base.extend ClassMethods

  base.class_eval do
    eval %{
      def as_json(options={})
        if !options[:representation] && defined?(super)
          super(options)
        else
          #{base}.render_representation(self, options.dup)
        end
      end
    }

    def representation(name, options={})
      as_json(options.merge(representation: name))
    end

    def self.included(base)
      if base.class == Module
        AsJsonRepresentations.send(:included, base)
        base.instance_variable_set :@parent_entity, self
      else
        context = self
        base.define_singleton_method(:representations) { context.representations }
      end
    end
  end
end

Public Instance Methods

representation(name, options={}) click to toggle source
# File lib/as_json_representations.rb, line 71
def representation(name, options={})
  as_json(options.merge(representation: name))
end