famili 1.1.2 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/famili/attributes.rb +27 -0
- data/lib/famili/child.rb +7 -14
- data/lib/famili/delegate.rb +15 -0
- data/lib/famili/grand_mother.rb +14 -0
- data/lib/famili/mother.rb +0 -4
- data/lib/famili/version.rb +1 -1
- metadata +7 -5
@@ -0,0 +1,27 @@
|
|
1
|
+
module Famili
|
2
|
+
class Attributes
|
3
|
+
attr :unresolved_names
|
4
|
+
|
5
|
+
def initialize(attributes)
|
6
|
+
@attributes = attributes
|
7
|
+
@unresolved_names = attributes.keys
|
8
|
+
end
|
9
|
+
|
10
|
+
def [](name)
|
11
|
+
(@cached_attributes ||= {})[name] ||= resolve(nil, name)
|
12
|
+
end
|
13
|
+
|
14
|
+
def resolve(model, name)
|
15
|
+
if unresolved_names.delete(name)
|
16
|
+
attribute_value = @attributes[name]
|
17
|
+
if attribute_value.is_a?(::Proc)
|
18
|
+
attribute_value = model.instance_exec(&attribute_value)
|
19
|
+
elsif attribute_value.respond_to?(:call)
|
20
|
+
attribute_value = attribute_value.call
|
21
|
+
end
|
22
|
+
attribute_value = attribute_value.build if attribute_value.is_a?(::Famili::Father)
|
23
|
+
attribute_value
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/famili/child.rb
CHANGED
@@ -1,15 +1,16 @@
|
|
1
|
+
require 'famili/attributes'
|
2
|
+
|
1
3
|
module Famili
|
2
4
|
class Child < BasicObject
|
3
5
|
attr_reader :mother
|
4
6
|
|
5
7
|
def initialize(mother, attributes)
|
6
8
|
@mother = mother
|
7
|
-
@attributes = attributes
|
9
|
+
@attributes = Attributes.new(attributes)
|
8
10
|
end
|
9
11
|
|
10
12
|
def born
|
11
|
-
@
|
12
|
-
@model = @mother.class.model_class.new
|
13
|
+
@model = @mother.instantiate(@attributes)
|
13
14
|
@meta_class = @model.singleton_class
|
14
15
|
@model.instance_variable_set(:@__famili_child__, self)
|
15
16
|
define_method_stub(:method_missing) do |name, *args|
|
@@ -20,10 +21,10 @@ module Famili
|
|
20
21
|
send(@__famili_child__.munge(:method_missing), name, *args)
|
21
22
|
end
|
22
23
|
end
|
23
|
-
@
|
24
|
+
@attributes.unresolved_names.each do |key|
|
24
25
|
define_property_stub(key)
|
25
26
|
end
|
26
|
-
resolve_property(@
|
27
|
+
resolve_property(@attributes.unresolved_names.first) until @attributes.unresolved_names.empty?
|
27
28
|
undefine_method_stub(:method_missing)
|
28
29
|
@model
|
29
30
|
end
|
@@ -33,16 +34,8 @@ module Famili
|
|
33
34
|
end
|
34
35
|
|
35
36
|
def resolve_property(name)
|
36
|
-
@unresolved_property_names.delete(name)
|
37
37
|
undefine_property_stub(name)
|
38
|
-
|
39
|
-
if attribute_value.is_a?(::Proc)
|
40
|
-
attribute_value = @model.instance_exec(&attribute_value)
|
41
|
-
elsif attribute_value.respond_to?(:call)
|
42
|
-
attribute_value = attribute_value.call
|
43
|
-
end
|
44
|
-
attribute_value = attribute_value.build if attribute_value.is_a?(::Famili::Father)
|
45
|
-
@model.send("#{name}=", attribute_value)
|
38
|
+
@model.send("#{name}=", @attributes.resolve(@model, name))
|
46
39
|
end
|
47
40
|
|
48
41
|
def define_property_stub(property_name)
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class Module
|
2
|
+
def delegate(*method_names)
|
3
|
+
opts = method_names.pop
|
4
|
+
declarations = ''
|
5
|
+
to = opts[:to]
|
6
|
+
method_names.each do |name|
|
7
|
+
declarations << <<-RUBY
|
8
|
+
def #{name}(*args, &block)
|
9
|
+
#{to}.#{name}(*args, &block)
|
10
|
+
end
|
11
|
+
RUBY
|
12
|
+
end
|
13
|
+
module_eval declarations
|
14
|
+
end
|
15
|
+
end
|
data/lib/famili/grand_mother.rb
CHANGED
@@ -1,6 +1,13 @@
|
|
1
1
|
require 'famili/father'
|
2
2
|
require "famili/lazy_value"
|
3
3
|
|
4
|
+
unless Class.respond_to?(:class_attribute)
|
5
|
+
require 'famili/class_attribute'
|
6
|
+
end
|
7
|
+
unless Module.respond_to?(:delegate)
|
8
|
+
require 'famili/delegate'
|
9
|
+
end
|
10
|
+
|
4
11
|
module Famili
|
5
12
|
class GrandMother
|
6
13
|
class_attribute :father_class
|
@@ -12,12 +19,19 @@ module Famili
|
|
12
19
|
@father ||= self.class.father_class.new(self, self.class.attributes)
|
13
20
|
end
|
14
21
|
|
22
|
+
#noinspection RubyUnusedLocalVariable
|
23
|
+
def instantiate(attributes)
|
24
|
+
self.class.model_class.new
|
25
|
+
end
|
26
|
+
|
15
27
|
def save(model)
|
16
28
|
end
|
17
29
|
|
30
|
+
#noinspection RubyUnusedLocalVariable
|
18
31
|
def before_save(model)
|
19
32
|
end
|
20
33
|
|
34
|
+
#noinspection RubyUnusedLocalVariable
|
21
35
|
def after_create(model)
|
22
36
|
end
|
23
37
|
|
data/lib/famili/mother.rb
CHANGED
data/lib/famili/version.rb
CHANGED
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: 1.
|
4
|
+
version: 1.2.0
|
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: 2013-08-
|
13
|
+
date: 2013-08-21 00:00:00.000000000 Z
|
14
14
|
dependencies: []
|
15
15
|
description: Yet another object mother pattern implementation.
|
16
16
|
email:
|
@@ -19,8 +19,10 @@ extensions: []
|
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
21
|
- lib/famili.rb
|
22
|
+
- lib/famili/attributes.rb
|
22
23
|
- lib/famili/child.rb
|
23
24
|
- lib/famili/class_attribute.rb
|
25
|
+
- lib/famili/delegate.rb
|
24
26
|
- lib/famili/father.rb
|
25
27
|
- lib/famili/grand_mother.rb
|
26
28
|
- lib/famili/lazy_value.rb
|
@@ -37,7 +39,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
37
39
|
requirements:
|
38
40
|
- - ! '>='
|
39
41
|
- !ruby/object:Gem::Version
|
40
|
-
hash: -
|
42
|
+
hash: -1151566627510109189
|
41
43
|
version: '0'
|
42
44
|
segments:
|
43
45
|
- 0
|
@@ -46,7 +48,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
46
48
|
requirements:
|
47
49
|
- - ! '>='
|
48
50
|
- !ruby/object:Gem::Version
|
49
|
-
hash: -
|
51
|
+
hash: -1151566627510109189
|
50
52
|
version: '0'
|
51
53
|
segments:
|
52
54
|
- 0
|
@@ -56,5 +58,5 @@ rubyforge_project:
|
|
56
58
|
rubygems_version: 1.8.24
|
57
59
|
signing_key:
|
58
60
|
specification_version: 3
|
59
|
-
summary: famili-1.
|
61
|
+
summary: famili-1.2.0
|
60
62
|
test_files: []
|