famili 1.0.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/famili/father.rb +1 -1
- data/lib/famili/grand_mother.rb +120 -0
- data/lib/famili/mother.rb +4 -107
- data/lib/famili/version.rb +1 -1
- metadata +6 -5
data/lib/famili/father.rb
CHANGED
@@ -0,0 +1,120 @@
|
|
1
|
+
require 'famili/father'
|
2
|
+
require "famili/lazy_value"
|
3
|
+
|
4
|
+
module Famili
|
5
|
+
class GrandMother
|
6
|
+
class_attribute :father_class
|
7
|
+
self.father_class = Famili::Father
|
8
|
+
|
9
|
+
delegate :build, :create, :build_brothers, :create_brothers, :build_hash, :scoped, to: :father
|
10
|
+
|
11
|
+
def father
|
12
|
+
@father ||= self.class.father_class.new(self, self.class.attributes)
|
13
|
+
end
|
14
|
+
|
15
|
+
def save(model)
|
16
|
+
end
|
17
|
+
|
18
|
+
def before_save(model)
|
19
|
+
end
|
20
|
+
|
21
|
+
def after_create(model)
|
22
|
+
end
|
23
|
+
|
24
|
+
class << self
|
25
|
+
alias_method :class_name, :name
|
26
|
+
|
27
|
+
delegate :build, :create, :build_brothers, :create_brothers, :build_hash, :scoped, to: :new
|
28
|
+
|
29
|
+
def parent_class=(klass)
|
30
|
+
@parent_class = klass
|
31
|
+
end
|
32
|
+
|
33
|
+
def parent_class
|
34
|
+
@parent_class
|
35
|
+
end
|
36
|
+
|
37
|
+
def inherited(child)
|
38
|
+
child.parent_class = self
|
39
|
+
end
|
40
|
+
|
41
|
+
def name(&block)
|
42
|
+
return class_name unless block_given?
|
43
|
+
field(:name, &block)
|
44
|
+
end
|
45
|
+
|
46
|
+
def method_missing(method, &block)
|
47
|
+
return field(method, &block) if block_given?
|
48
|
+
super
|
49
|
+
end
|
50
|
+
|
51
|
+
def attributes
|
52
|
+
@attributes ||= parent_class && parent_class.attributes.clone || {}
|
53
|
+
end
|
54
|
+
|
55
|
+
def field(method, value = nil, &block)
|
56
|
+
block = -> { value } if value
|
57
|
+
attributes[method] = block
|
58
|
+
end
|
59
|
+
|
60
|
+
def lazy(&block)
|
61
|
+
Famili::LazyValue.new(&block)
|
62
|
+
end
|
63
|
+
|
64
|
+
def has(name, &block)
|
65
|
+
attributes[name] = lazy do
|
66
|
+
father = "#{model_class.reflect_on_association(name.to_sym).klass.name}Famili".constantize.new.father
|
67
|
+
father = father.scoped(collect_attributes(&block)) if block_given?
|
68
|
+
father
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def trait(name, &block)
|
73
|
+
attributes = collect_attributes(&block)
|
74
|
+
scope(name) { scoped(attributes) }
|
75
|
+
end
|
76
|
+
|
77
|
+
def scope(name, &block)
|
78
|
+
ensure_own_father_class.send(:define_method, name, &block)
|
79
|
+
delegate name, to: :father
|
80
|
+
singleton_class.delegate name, to: :new
|
81
|
+
end
|
82
|
+
|
83
|
+
def scopes
|
84
|
+
@scopes ||= parent_class && parent_class.scopes.dup || {}
|
85
|
+
end
|
86
|
+
|
87
|
+
def model_class(klass = nil)
|
88
|
+
if klass
|
89
|
+
self.model_class = klass
|
90
|
+
return
|
91
|
+
end
|
92
|
+
|
93
|
+
@model_class ||= if class_name =~ /(.*)Famili$/ || class_name =~ /Famili::(.*)/
|
94
|
+
$1.split('::').inject(Object) do |mod, const|
|
95
|
+
mod.const_get(const)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def model_class=(klass)
|
101
|
+
@model_class = klass
|
102
|
+
end
|
103
|
+
|
104
|
+
protected
|
105
|
+
|
106
|
+
def ensure_own_father_class
|
107
|
+
@father_class = self.father_class = Class.new(self.father_class) unless @father_class == father_class
|
108
|
+
@father_class
|
109
|
+
end
|
110
|
+
|
111
|
+
def collect_attributes
|
112
|
+
saved_attributes, @attributes = @attributes, {}
|
113
|
+
yield
|
114
|
+
@attributes
|
115
|
+
ensure
|
116
|
+
@attributes = saved_attributes
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
data/lib/famili/mother.rb
CHANGED
@@ -1,20 +1,14 @@
|
|
1
1
|
require 'date'
|
2
|
-
require 'famili/
|
3
|
-
require "famili/lazy_value"
|
2
|
+
require 'famili/grand_mother'
|
4
3
|
|
5
4
|
unless Class.respond_to?(:class_attribute)
|
6
5
|
require 'famili/class_attribute'
|
7
6
|
end
|
8
7
|
|
9
8
|
module Famili
|
10
|
-
class Mother
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
def before_save(model)
|
15
|
-
end
|
16
|
-
|
17
|
-
def after_create(model)
|
9
|
+
class Mother < Famili::GrandMother
|
10
|
+
def save(model)
|
11
|
+
model.save!
|
18
12
|
end
|
19
13
|
|
20
14
|
def unique
|
@@ -26,107 +20,10 @@ module Famili
|
|
26
20
|
end
|
27
21
|
|
28
22
|
class << self
|
29
|
-
alias_method :class_name, :name
|
30
|
-
|
31
|
-
delegate :build, :create, :build_brothers, :create_brothers, :build_hash, :scoped, to: :new_father
|
32
|
-
|
33
23
|
def objects_sequence_number
|
34
24
|
@sequence_number ||= 0
|
35
25
|
@sequence_number += 1
|
36
26
|
end
|
37
|
-
|
38
|
-
def parent_class=(klass)
|
39
|
-
@parent_class = klass
|
40
|
-
end
|
41
|
-
|
42
|
-
def parent_class
|
43
|
-
@parent_class
|
44
|
-
end
|
45
|
-
|
46
|
-
def inherited(child)
|
47
|
-
child.parent_class = self
|
48
|
-
end
|
49
|
-
|
50
|
-
def name(&block)
|
51
|
-
return class_name unless block_given?
|
52
|
-
field(:name, &block)
|
53
|
-
end
|
54
|
-
|
55
|
-
def method_missing(method, &block)
|
56
|
-
return field(method, &block) if block_given?
|
57
|
-
super
|
58
|
-
end
|
59
|
-
|
60
|
-
def attributes
|
61
|
-
@attributes ||= parent_class && parent_class.attributes.clone || {}
|
62
|
-
end
|
63
|
-
|
64
|
-
def field(method, value = nil, &block)
|
65
|
-
block = -> { value } if value
|
66
|
-
attributes[method] = block
|
67
|
-
end
|
68
|
-
|
69
|
-
def lazy(&block)
|
70
|
-
Famili::LazyValue.new(&block)
|
71
|
-
end
|
72
|
-
|
73
|
-
def has(name, &block)
|
74
|
-
attributes[name] = lazy do
|
75
|
-
father = "#{model_class.reflect_on_association(name.to_sym).klass.name}Famili".constantize.new_father
|
76
|
-
father = father.scoped(collect_attributes(&block)) if block_given?
|
77
|
-
father
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
def new_father
|
82
|
-
father_class.new(self.new, attributes)
|
83
|
-
end
|
84
|
-
|
85
|
-
def trait(name, &block)
|
86
|
-
attributes = collect_attributes(&block)
|
87
|
-
scope(name) { scoped(attributes) }
|
88
|
-
end
|
89
|
-
|
90
|
-
def scope(name, &block)
|
91
|
-
ensure_own_father_class.send(:define_method, name, &block)
|
92
|
-
singleton_class.delegate name, to: :new_father
|
93
|
-
end
|
94
|
-
|
95
|
-
def scopes
|
96
|
-
@scopes ||= parent_class && parent_class.scopes.dup || {}
|
97
|
-
end
|
98
|
-
|
99
|
-
def model_class(klass = nil)
|
100
|
-
if klass
|
101
|
-
self.model_class = klass
|
102
|
-
return
|
103
|
-
end
|
104
|
-
|
105
|
-
@model_class ||= if class_name =~ /(.*)Famili$/ || class_name =~ /Famili::(.*)/
|
106
|
-
$1.split('::').inject(Object) do |mod, const|
|
107
|
-
mod.const_get(const)
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
def model_class=(klass)
|
113
|
-
@model_class = klass
|
114
|
-
end
|
115
|
-
|
116
|
-
protected
|
117
|
-
|
118
|
-
def ensure_own_father_class
|
119
|
-
@father_class = self.father_class = Class.new(self.father_class) unless @father_class == father_class
|
120
|
-
@father_class
|
121
|
-
end
|
122
|
-
|
123
|
-
def collect_attributes
|
124
|
-
saved_attributes, @attributes = @attributes, {}
|
125
|
-
yield
|
126
|
-
@attributes
|
127
|
-
ensure
|
128
|
-
@attributes = saved_attributes
|
129
|
-
end
|
130
27
|
end
|
131
28
|
end
|
132
29
|
end
|
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.1.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-
|
13
|
+
date: 2012-11-09 00:00:00.000000000 Z
|
14
14
|
dependencies: []
|
15
15
|
description: Yet another object mother pattern implementation.
|
16
16
|
email:
|
@@ -22,6 +22,7 @@ files:
|
|
22
22
|
- lib/famili/child.rb
|
23
23
|
- lib/famili/class_attribute.rb
|
24
24
|
- lib/famili/father.rb
|
25
|
+
- lib/famili/grand_mother.rb
|
25
26
|
- lib/famili/lazy_value.rb
|
26
27
|
- lib/famili/mother.rb
|
27
28
|
- lib/famili/version.rb
|
@@ -40,7 +41,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
40
41
|
version: '0'
|
41
42
|
segments:
|
42
43
|
- 0
|
43
|
-
hash:
|
44
|
+
hash: 845392613528680780
|
44
45
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
46
|
none: false
|
46
47
|
requirements:
|
@@ -49,11 +50,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
49
50
|
version: '0'
|
50
51
|
segments:
|
51
52
|
- 0
|
52
|
-
hash:
|
53
|
+
hash: 845392613528680780
|
53
54
|
requirements: []
|
54
55
|
rubyforge_project:
|
55
56
|
rubygems_version: 1.8.24
|
56
57
|
signing_key:
|
57
58
|
specification_version: 3
|
58
|
-
summary: famili-1.
|
59
|
+
summary: famili-1.1.1
|
59
60
|
test_files: []
|