Sha256: 0f2a9751d6cd5101a9510d16135d06ce63ca3b0227ec9ba7cc3ac3a3fe388e5c

Contents?: true

Size: 777 Bytes

Versions: 4

Compression:

Stored size: 777 Bytes

Contents

require "pluck_map/attribute"

module PluckMap
  class StructuredAttribute < Attribute
    attr_reader :attributes

    def initialize(attribute_name, model, block, options={})
      @attributes = AttributeBuilder.build(model: model, &block)
      options = options.slice(:as).merge(select: build_select, map: build_map)
      super(attribute_name, model, options)
    end

    def will_map?
      attributes.any?(&:will_map?)
    end

    def nested?
      true
    end

  protected

    def build_select
      attributes.selects
    end

    def build_map
      lambda do |*values|
        return nil if values.none?
        attributes.each_with_object({}) do |attribute, hash|
          hash[attribute.name] = attribute.exec(values)
        end
      end
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
pluck_map-2.0.0 lib/pluck_map/structured_attribute.rb
pluck_map-2.0.0.rc1 lib/pluck_map/structured_attribute.rb
pluck_map-1.0.0 lib/pluck_map/structured_attribute.rb
pluck_map-1.0.0.rc2 lib/pluck_map/structured_attribute.rb