Class: Bricks::Builder

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

Defined Under Namespace

Classes: Proxy

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.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/bricks/builder.rb', line 45

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



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/bricks/builder.rb', line 87

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}' on builder for #{@class}."
           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

#after(hook, &block) ⇒ Object



26
27
28
# File 'lib/bricks/builder.rb', line 26

def after(hook, &block)
  define_hook :after, hook, &block
end

#before(hook, &block) ⇒ Object



30
31
32
# File 'lib/bricks/builder.rb', line 30

def before(hook, &block)
  define_hook :before, hook, &block
end

#derive(args = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/bricks/builder.rb', line 34

def derive(args = {})
  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, &@block).tap { |b|
    b.send :build_attrs
    b.run_hook :after, :clone if ! args[:class]
  }
end

#generate(opts = {}) ⇒ Object



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

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

  run_hook :before, :save if @save

  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



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/bricks/builder.rb', line 75

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