Class: Famili::Father
- Inherits:
-
Object
show all
- Defined in:
- lib/famili/father.rb
Instance Method Summary
collapse
-
#build(opts = {}) {|model| ... } ⇒ Object
-
#build_brothers(num, opts = {}, &block) ⇒ Object
-
#build_hash(opts = {}) ⇒ Object
-
#create(opts = {}, &block) ⇒ Object
-
#create_brothers(num, opts = {}, &block) ⇒ Object
-
#initialize(mother, attributes) ⇒ Father
constructor
A new instance of Father.
-
#method_missing(name, *args) ⇒ Object
-
#produce_brothers(num, opts = {}, init_block, &block) ⇒ Object
-
#scoped(attributes = {}) ⇒ Object
Constructor Details
#initialize(mother, attributes) ⇒ Father
Returns a new instance of Father.
5
6
7
8
|
# File 'lib/famili/father.rb', line 5
def initialize(mother, attributes)
@mother = mother
@attributes = attributes
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
56
57
58
59
60
61
62
|
# File 'lib/famili/father.rb', line 56
def method_missing(name, *args)
if scope_attributes = @mother.class.scopes[name]
scoped(scope_attributes)
else
super(name, *args)
end
end
|
Instance Method Details
#build(opts = {}) {|model| ... } ⇒ Object
17
18
19
20
21
22
23
|
# File 'lib/famili/father.rb', line 17
def build(opts = {})
attributes = merge(opts)
model = Famili::Child.new(@mother, attributes).born
yield model if block_given?
@mother.before_save(model)
model
end
|
#build_brothers(num, opts = {}, &block) ⇒ Object
44
45
46
|
# File 'lib/famili/father.rb', line 44
def build_brothers(num, opts = {}, &block)
produce_brothers(num, opts, block) { |brother_opts, &init_block| build(brother_opts, &init_block) }
end
|
#build_hash(opts = {}) ⇒ Object
10
11
12
13
14
15
|
# File 'lib/famili/father.rb', line 10
def build_hash(opts = {})
attributes = build(opts).attributes.symbolize_keys
attributes.delete(:updated_at)
attributes.delete(:created_at)
attributes
end
|
#create(opts = {}, &block) ⇒ Object
25
26
27
28
29
30
|
# File 'lib/famili/father.rb', line 25
def create(opts = {}, &block)
model = build(opts, &block)
model.save!
@mother.after_create(model)
model
end
|
#create_brothers(num, opts = {}, &block) ⇒ Object
48
49
50
|
# File 'lib/famili/father.rb', line 48
def create_brothers(num, opts = {}, &block)
produce_brothers(num, opts, block) { |brother_opts, &init_block| create(brother_opts, &init_block) }
end
|
#produce_brothers(num, opts = {}, init_block, &block) ⇒ Object
32
33
34
35
36
37
38
39
40
|
# File 'lib/famili/father.rb', line 32
def produce_brothers(num, opts={}, init_block, &block)
brothers = []
if init_block && init_block.arity == 2
num.times { |i| brothers << block.call(opts) { |o| init_block.call(o, i) } }
else
num.times { brothers << block.call(opts, &init_block) }
end
brothers
end
|
#scoped(attributes = {}) ⇒ Object
52
53
54
|
# File 'lib/famili/father.rb', line 52
def scoped(attributes = {})
Famili::Father.new(@mother, merge(attributes))
end
|