Class: PluckMap::AttributeBuilder

Inherits:
BasicObject
Defined in:
lib/pluck_map/attribute_builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes, model) ⇒ AttributeBuilder

Returns a new instance of AttributeBuilder.



20
21
22
23
# File 'lib/pluck_map/attribute_builder.rb', line 20

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(attribute_name, *args, &block) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/pluck_map/attribute_builder.rb', line 25

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

Class Method Details

.build(model:, &block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/pluck_map/attribute_builder.rb', line 9

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

Instance Method Details

#has_many(name, *args, &block) ⇒ Object



33
34
35
36
37
38
# File 'lib/pluck_map/attribute_builder.rb', line 33

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

#has_one(name, *args, &block) ⇒ Object



40
41
42
43
44
45
# File 'lib/pluck_map/attribute_builder.rb', line 40

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