Sha256: 19953aee8c51a61a8b0ed9a4d6f646932bfd49040449f4087763b1b72fe53473

Contents?: true

Size: 886 Bytes

Versions: 1

Compression:

Stored size: 886 Bytes

Contents

module ActiveModel
  class Exporter
    class << self
      attr_accessor :_attributes

      def inherited(base)
        base._attributes = (_attributes || []).dup
      end

      def attributes(*attrs)
        @_attributes.concat(attrs)

        attrs.each do |attr|
          define_method(attr) do
            object.send(attr)
          end unless method_defined?(attr)
        end
      end

      def exporter_for(resource)
        if resource.respond_to?(:to_ary)
          ArrayExporter
        else
          "#{resource.class.name}Exporter".safe_constantize
        end
      end
    end


    attr_accessor :object, :attributes, :scope

    def initialize(object, options = {})
      @object     = object
      @scope      = options[:scope]
      @attributes = self.class._attributes.dup
    end

    def values
      attributes.map { |attr| send(attr) }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_model_exporters-0.0.2 lib/active_model/exporter.rb