Class: Bricks::Builder

Inherits:
Object
  • Object
show all
Includes:
DSL
Defined in:
lib/bricks/builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSL

#build, #build!, #build?, #builder, #create, #create!, #create?

Constructor Details

#initialize(klass, attrs = nil, traits = nil, save = false, search = false, &block) ⇒ Builder

Returns a new instance of Builder.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/bricks/builder.rb', line 36

def initialize(
    klass,
    attrs  = nil,
    traits = nil,
    save   = false,
    search = false,
    &block)
  @class  = klass
  @attrs  = attrs ? deep_copy(attrs) : []
  @traits = traits ? Module.new { include traits } : Module.new
  @save   = save
  @search = search
  @block  = block

  extend @traits
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/bricks/builder.rb', line 76

def method_missing(name, *args, &block)
  attr   = (return_object = name.to_s =~ /[!?]$/) ? name.to_s.chop : name
  result = if respond_to?(attr)
             send(attr, *args)
           elsif settable?(attr)
             set attr, *args, &block
           else
             raise Bricks::NoAttributeOrTrait, "Can't find `#{name}'."
           end

  if return_object
    opts          = {:parent => @parent}
    opts[:search] = name.to_s =~ /\?$/ || @search

    generate opts
  else
    result
  end
end

Class Method Details

.adapterObject



8
9
10
# File 'lib/bricks/builder.rb', line 8

def self.adapter
  @@adapter
end

.adapter=(adapter) ⇒ Object



12
13
14
# File 'lib/bricks/builder.rb', line 12

def self.adapter=(adapter)
  @@adapter = adapter
end

.instancesObject



16
17
18
# File 'lib/bricks/builder.rb', line 16

def self.instances
  @@instances ||= {}
end

Instance Method Details

#derive(args = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/bricks/builder.rb', line 26

def derive(args = {})
  build_attrs

  klass  = args[:class] || @class
  save   = args.has_key?(:save) ? args[:save] : @save
  search = args.has_key?(:search) ? args[:search] : @search

  Builder.new(klass, @attrs, @traits, save, search)
end

#generate(opts = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/bricks/builder.rb', line 53

def generate(opts = {})
  parent = opts[:parent]
  search = opts.has_key?(:search) ? opts[:search] : @search
  obj    = initialize_object(parent)

  obj  = adapter.find(@class, Hash[*@attrs.flatten]) || obj if search
  save_object(obj)                                          if @save

  obj
end

#trait(name, &block) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/bricks/builder.rb', line 64

def trait(name, &block)
  @traits.class_eval do
    define_method "__#{name}", &block

    define_method name do |*args|
      send "__#{name}", *args

      self
    end
  end
end

#~@Object



20
21
22
23
24
# File 'lib/bricks/builder.rb', line 20

def ~@()
  @search = true

  self
end