famili 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
data/lib/famili/child.rb CHANGED
@@ -36,7 +36,11 @@ module Famili
36
36
  @unresolved_property_names.delete(name)
37
37
  undefine_property_stub(name)
38
38
  attribute_value = @attributes[name]
39
- attribute_value = @model.instance_exec(&attribute_value) if attribute_value.is_a?(::Proc)
39
+ if attribute_value.is_a?(::Proc)
40
+ attribute_value = @model.instance_exec(&attribute_value)
41
+ elsif attribute_value.is_a?(::Famili::Father)
42
+ attribute_value = attribute_value.create
43
+ end
40
44
  @model.send("#{name}=", attribute_value)
41
45
  end
42
46
 
data/lib/famili/father.rb CHANGED
@@ -6,7 +6,7 @@ module Famili
6
6
  @mother = mother
7
7
  @attributes = attributes
8
8
  end
9
-
9
+
10
10
  def build_hash(opts = {})
11
11
  attributes = build(opts).attributes.symbolize_keys
12
12
  attributes.delete(:updated_at)
@@ -15,7 +15,7 @@ module Famili
15
15
  end
16
16
 
17
17
  def build(opts = {})
18
- attributes = merge(opts)
18
+ attributes = merge(opts)
19
19
  model = Famili::Child.new(@mother, attributes).born
20
20
  yield model if block_given?
21
21
  @mother.before_save(model)
@@ -29,16 +29,24 @@ module Famili
29
29
  model
30
30
  end
31
31
 
32
- def build_brothers(num, opts = {}, &block)
32
+ def produce_brothers(num, opts={}, init_block, &block)
33
33
  brothers = []
34
- num.times { brothers << build(opts, &block) }
34
+ if init_block && init_block.arity == 2
35
+ num.times { |i| brothers << block.call(opts) { |o| init_block.call(o, i) } }
36
+ else
37
+ num.times { brothers << block.call(opts, &init_block) }
38
+ end
35
39
  brothers
36
40
  end
37
41
 
42
+ private_methods :produce_brothers
43
+
44
+ def build_brothers(num, opts = {}, &block)
45
+ produce_brothers(num, opts, block) { |brother_opts, &init_block| build(brother_opts, &init_block) }
46
+ end
47
+
38
48
  def create_brothers(num, opts = {}, &block)
39
- brothers = []
40
- num.times { brothers << create(opts, &block) }
41
- brothers
49
+ produce_brothers(num, opts, block) { |brother_opts, &init_block| create(brother_opts, &init_block) }
42
50
  end
43
51
 
44
52
  def scoped(attributes = {})
data/lib/famili/mother.rb CHANGED
@@ -18,10 +18,12 @@ module Famili
18
18
  end
19
19
 
20
20
  class << self
21
- alias :class_name :name
21
+ alias_method :class_name, :name
22
+
23
+ delegate :build, :create, :build_brothers, :create_brothers, :build_hash, :scoped, to: :new_father
22
24
 
23
25
  def objects_sequence_number
24
- @sequence_number ||=0
26
+ @sequence_number ||= 0
25
27
  @sequence_number += 1
26
28
  end
27
29
 
@@ -48,59 +50,39 @@ module Famili
48
50
  end
49
51
 
50
52
  def attributes
51
- @attributes||=parent_class && parent_class.attributes.clone || {}
52
- @attributes
53
+ @attributes ||= parent_class && parent_class.attributes.clone || {}
53
54
  end
54
55
 
55
- def field(method, &block)
56
+ def field(method, value = nil, &block)
57
+ block = -> { value } if value
56
58
  attributes[method] = block
57
59
  end
58
60
 
59
- def father
60
- @father ||= Famili::Father.new(self.new, attributes)
61
- end
62
-
63
- def create(opts = {})
64
- father.create(opts)
61
+ def has(name, &block)
62
+ pending_tasks.push(Proc.new do
63
+ father = "#{model_class.reflect_on_association(name.to_sym).klass.name}Famili".constantize.new_father
64
+ father = father.scoped(collect_attributes(&block)) if block_given?
65
+ attributes[name] = father
66
+ end)
65
67
  end
66
68
 
67
- def build(opts = {})
68
- father.build(opts)
69
+ def new_father
70
+ invoke_pending_tasks
71
+ Famili::Father.new(self.new, attributes)
69
72
  end
70
73
 
71
- def build_hash(opts = {})
72
- father.build_hash(opts)
73
- end
74
-
75
- def build_brothers(num, opts = {}, &block)
76
- father.build_brothers(num, opts, &block)
77
- end
78
-
79
- def create_brothers(num, opts = {}, &block)
80
- father.create_brothers(num, opts, &block)
81
- end
82
-
83
- def scoped(attributes = {})
84
- father.scoped(attributes)
85
- end
86
-
87
- def scope(name)
88
- saved_attributes = @attributes
89
- @attributes = {}
90
- yield
91
- scopes[name] = @attributes
74
+ def scope(name, &block)
75
+ scopes[name] = collect_attributes(&block)
92
76
  singleton_class.send(:define_method, name) do
93
- father.send(name)
77
+ new_father.send(name)
94
78
  end
95
- ensure
96
- @attributes = saved_attributes
97
79
  end
98
80
 
99
81
  def scopes
100
82
  @scopes ||= parent_class && parent_class.scopes.dup || {}
101
83
  end
102
84
 
103
- def model_class(klass=nil)
85
+ def model_class(klass = nil)
104
86
  if klass
105
87
  @model_class = klass
106
88
  return
@@ -112,6 +94,27 @@ module Famili
112
94
  end
113
95
  end
114
96
  end
97
+
98
+ protected
99
+
100
+ def pending_tasks
101
+ @pending_tasks ||= []
102
+ end
103
+
104
+ def invoke_pending_tasks
105
+ parent_class.invoke_pending_tasks if parent_class
106
+ if @pending_tasks
107
+ @pending_tasks.each(&:call)
108
+ end
109
+ end
110
+
111
+ def collect_attributes
112
+ saved_attributes, @attributes = @attributes, {}
113
+ yield
114
+ @attributes
115
+ ensure
116
+ @attributes = saved_attributes
117
+ end
115
118
  end
116
119
  end
117
120
  end
@@ -1,5 +1,5 @@
1
1
  module Famili
2
2
  module Version
3
- STRING = '0.1.7'
3
+ STRING = '0.1.8'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: famili
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2012-04-16 00:00:00.000000000Z
13
+ date: 2012-06-28 00:00:00.000000000 Z
14
14
  dependencies: []
15
15
  description: Yet another object mother pattern implementation.
16
16
  email:
@@ -38,7 +38,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
38
38
  version: '0'
39
39
  segments:
40
40
  - 0
41
- hash: -148278919315159194
41
+ hash: -3995658946911266953
42
42
  required_rubygems_version: !ruby/object:Gem::Requirement
43
43
  none: false
44
44
  requirements:
@@ -47,11 +47,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
47
47
  version: '0'
48
48
  segments:
49
49
  - 0
50
- hash: -148278919315159194
50
+ hash: -3995658946911266953
51
51
  requirements: []
52
52
  rubyforge_project:
53
53
  rubygems_version: 1.8.10
54
54
  signing_key:
55
55
  specification_version: 3
56
- summary: famili-0.1.7
56
+ summary: famili-0.1.8
57
57
  test_files: []