Sha256: cd9df72082b054d5af086a4b890197e6b2cb8beca8a06cddb9aa5b2ae5e10254

Contents?: true

Size: 1.35 KB

Versions: 4

Compression:

Stored size: 1.35 KB

Contents

require "pluck_map/attribute"
require "pluck_map/structured_attribute"
require "pluck_map/attributes"
require "pluck_map/relationships"

module PluckMap
  class AttributeBuilder < BasicObject

    def self.build(model:, &block)
      attributes = []
      builder = self.new(attributes, model)
      if block.arity == 1
        block.call(builder)
      else
        builder.instance_eval(&block)
      end
      Attributes.new(attributes, model)
    end

    def initialize(attributes, model)
      @attributes = attributes
      @model = model
    end

    def method_missing(attribute_name, *args, &block)
      options = args.extract_options!
      options[:value] = args.first unless args.empty?
      @attributes.push block.nil? ? Attribute.new(attribute_name, @model, options) :
        StructuredAttribute.new(attribute_name, @model, block, options)
      :attribute_added
    end

    def has_many(name, *args, &block)
      options = args.extract_options!
      options[:scope_block] = args.first unless args.empty?
      @attributes.push Relationships.many(@model, name, block, options)
      :relationship_added
    end

    def has_one(name, *args, &block)
      options = args.extract_options!
      options[:scope_block] = args.first unless args.empty?
      @attributes.push Relationships.one(@model, name, block, options)
      :relationship_added
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

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