famili 0.1.9 → 0.1.9.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/famili/child.rb CHANGED
@@ -38,9 +38,10 @@ module Famili
38
38
  attribute_value = @attributes[name]
39
39
  if attribute_value.is_a?(::Proc)
40
40
  attribute_value = @model.instance_exec(&attribute_value)
41
- elsif attribute_value.is_a?(::Famili::Father)
42
- attribute_value = attribute_value.create
41
+ elsif attribute_value.respond_to?(:call)
42
+ attribute_value = attribute_value.call
43
43
  end
44
+ attribute_value = attribute_value.build if attribute_value.is_a?(::Famili::Father)
44
45
  @model.send("#{name}=", attribute_value)
45
46
  end
46
47
 
@@ -0,0 +1,11 @@
1
+ module Famili
2
+ class LazyValue
3
+ def initialize
4
+ @block = proc
5
+ end
6
+
7
+ def call
8
+ @value ||= @block.call
9
+ end
10
+ end
11
+ end
data/lib/famili/mother.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'date'
2
2
  require 'famili/father'
3
+ require "famili/lazy_value"
3
4
 
4
5
  module Famili
5
6
  class Mother
@@ -37,7 +38,6 @@ module Famili
37
38
 
38
39
  def inherited(child)
39
40
  child.parent_class = self
40
- child.pending_tasks = []
41
41
  end
42
42
 
43
43
  def name(&block)
@@ -59,16 +59,19 @@ module Famili
59
59
  attributes[method] = block
60
60
  end
61
61
 
62
+ def lazy(&block)
63
+ Famili::LazyValue.new(&block)
64
+ end
65
+
62
66
  def has(name, &block)
63
- pending_tasks.push(Proc.new do
67
+ attributes[name] = lazy do
64
68
  father = "#{model_class.reflect_on_association(name.to_sym).klass.name}Famili".constantize.new_father
65
69
  father = father.scoped(collect_attributes(&block)) if block_given?
66
- attributes[name] = father
67
- end)
70
+ father
71
+ end
68
72
  end
69
73
 
70
74
  def new_father
71
- invoke_pending_tasks if pending_tasks
72
75
  Famili::Father.new(self.new, attributes)
73
76
  end
74
77
 
@@ -85,7 +88,7 @@ module Famili
85
88
 
86
89
  def model_class(klass = nil)
87
90
  if klass
88
- @model_class = klass
91
+ self.model_class = klass
89
92
  return
90
93
  end
91
94
 
@@ -96,18 +99,12 @@ module Famili
96
99
  end
97
100
  end
98
101
 
99
- protected
100
-
101
- attr_accessor :pending_tasks
102
-
103
- def invoke_pending_tasks
104
- parent_class.invoke_pending_tasks if parent_class
105
- if @pending_tasks
106
- @pending_tasks.each(&:call)
107
- @pending_tasks = nil
108
- end
102
+ def model_class=(klass)
103
+ @model_class = klass
109
104
  end
110
105
 
106
+ protected
107
+
111
108
  def collect_attributes
112
109
  saved_attributes, @attributes = @attributes, {}
113
110
  yield
@@ -1,5 +1,5 @@
1
1
  module Famili
2
2
  module Version
3
- STRING = '0.1.9'
3
+ STRING = '0.1.9.1'
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.9
4
+ version: 0.1.9.1
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-07-02 00:00:00.000000000 Z
13
+ date: 2012-07-03 00:00:00.000000000 Z
14
14
  dependencies: []
15
15
  description: Yet another object mother pattern implementation.
16
16
  email:
@@ -21,6 +21,7 @@ files:
21
21
  - lib/famili.rb
22
22
  - lib/famili/child.rb
23
23
  - lib/famili/father.rb
24
+ - lib/famili/lazy_value.rb
24
25
  - lib/famili/mother.rb
25
26
  - lib/famili/version.rb
26
27
  homepage: http://github.com/niquola/famili
@@ -38,7 +39,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
38
39
  version: '0'
39
40
  segments:
40
41
  - 0
41
- hash: 2078673055765586338
42
+ hash: -1932633742867940772
42
43
  required_rubygems_version: !ruby/object:Gem::Requirement
43
44
  none: false
44
45
  requirements:
@@ -47,11 +48,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
47
48
  version: '0'
48
49
  segments:
49
50
  - 0
50
- hash: 2078673055765586338
51
+ hash: -1932633742867940772
51
52
  requirements: []
52
53
  rubyforge_project:
53
54
  rubygems_version: 1.8.10
54
55
  signing_key:
55
56
  specification_version: 3
56
- summary: famili-0.1.9
57
+ summary: famili-0.1.9.1
57
58
  test_files: []