famili 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/famili/child.rb +45 -19
  2. data/lib/famili/version.rb +1 -1
  3. metadata +7 -4
data/lib/famili/child.rb CHANGED
@@ -1,36 +1,62 @@
1
1
  module Famili
2
2
  class Child < BasicObject
3
+ attr_reader :mother
4
+
3
5
  def initialize(mother, attributes)
4
6
  @mother = mother
5
7
  @attributes = attributes
6
8
  end
7
9
 
8
- def method_missing(name, *arguments)
9
- if @mother.respond_to?(name)
10
- @mother.send(name, *arguments)
11
- else
12
- evaluate_value(name.to_sym, *arguments)
13
- end
14
- end
15
-
16
10
  def born
17
- @unresolved_keys = @attributes.keys
11
+ @unresolved_property_names = @attributes.keys
18
12
  @model = @mother.class.model_class.new
19
- evaluate_value(@unresolved_keys.first) until @unresolved_keys.empty?
13
+ @meta_class = @model.singleton_class
14
+ @model.instance_variable_set(:@__famili_child__, self)
15
+ define_method_stub(:method_missing) do |name, *args|
16
+ mother = @__famili_child__.mother
17
+ if mother.respond_to?(name)
18
+ mother.send(name, *args)
19
+ else
20
+ super
21
+ end
22
+ end
23
+ @unresolved_property_names.each do |key|
24
+ define_property_stub(key)
25
+ end
26
+ resolve_property(@unresolved_property_names.first) until @unresolved_property_names.empty?
27
+ undefine_method_stub(:method_missing)
20
28
  @model
21
29
  end
22
30
 
23
- private
31
+ def munge(property_name)
32
+ "__famili_child_proxied_#{property_name}"
33
+ end
34
+
35
+ def resolve_property(name)
36
+ @unresolved_property_names.delete(name)
37
+ undefine_property_stub(name)
38
+ attribute_value = @attributes[name]
39
+ attribute_value = @model.instance_exec(&attribute_value) if attribute_value.is_a?(::Proc)
40
+ @model.send("#{name}=", attribute_value)
41
+ end
42
+
43
+ def undefine_method_stub(method_name)
44
+ munged_name = munge(method_name)
45
+ @meta_class.send(:alias_method, method_name, munged_name)
46
+ @meta_class.send(:remove_method, munged_name)
47
+ end
24
48
 
25
- def evaluate_value(name, *arguments)
26
- if @unresolved_keys.include?(name)
27
- @unresolved_keys.delete(name)
28
- attribute_value = @attributes[name]
29
- attribute_value = instance_exec(&attribute_value) if attribute_value.is_a?(::Proc)
30
- @model.send("#{name}=", attribute_value)
31
- else
32
- @model.send(name, *arguments)
49
+ alias :undefine_property_stub :undefine_method_stub
50
+
51
+ def define_property_stub(property_name)
52
+ define_method_stub property_name do
53
+ @__famili_child__.resolve_property(property_name)
33
54
  end
34
55
  end
56
+
57
+ def define_method_stub(method_name, &block)
58
+ @meta_class.send(:alias_method, munge(method_name), method_name) if @model.respond_to?(method_name)
59
+ @meta_class.send(:define_method, method_name, &block)
60
+ end
35
61
  end
36
62
  end
@@ -1,5 +1,5 @@
1
1
  module Famili
2
2
  module Version
3
- STRING = '0.1.3'
3
+ STRING = '0.1.4'
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.3
4
+ version: 0.1.4
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: 2011-10-28 00:00:00.000000000Z
13
+ date: 2011-11-16 00:00:00.000000000Z
14
14
  dependencies: []
15
15
  description: Yet another object mother pattern implementation.
16
16
  email:
@@ -38,17 +38,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
38
38
  version: '0'
39
39
  segments:
40
40
  - 0
41
- hash: 2274387620902998155
41
+ hash: -523760628095168848
42
42
  required_rubygems_version: !ruby/object:Gem::Requirement
43
43
  none: false
44
44
  requirements:
45
45
  - - ! '>='
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
+ segments:
49
+ - 0
50
+ hash: -523760628095168848
48
51
  requirements: []
49
52
  rubyforge_project:
50
53
  rubygems_version: 1.8.10
51
54
  signing_key:
52
55
  specification_version: 3
53
- summary: famili-0.1.3
56
+ summary: famili-0.1.4
54
57
  test_files: []