Sha256: 8c52fe9ebf6e1fcb8e31c037ed46a48200079cff8e24b1fe8634f130d68c6600

Contents?: true

Size: 992 Bytes

Versions: 4

Compression:

Stored size: 992 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, :scope

    def initialize(object, options = {})
      @object = object
      @scope  = options[:scope]
    end

    def values
      attrs = filter(attributes)
      attributes.map { |attr| send(attr) if attrs.include?(attr) }
    end

    private

    def filter(attrs)
      attrs
    end

    def attributes
      self.class._attributes.dup
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
active_model_exporters-0.3.1 lib/active_model/exporter.rb
active_model_exporters-0.3.0 lib/active_model/exporter.rb
active_model_exporters-0.2.0 lib/active_model/exporter.rb
active_model_exporters-0.1.0 lib/active_model/exporter.rb