famili 0.1.9.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,40 @@
1
+ class Module
2
+ def remove_possible_method(method)
3
+ if method_defined?(method) || private_method_defined?(method)
4
+ undef_method(method)
5
+ end
6
+ end
7
+ end
8
+
9
+ class Class
10
+ def class_attribute(*attrs)
11
+ attrs.each do |name|
12
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
13
+ def self.#{name}() nil end
14
+ def self.#{name}?() !!#{name} end
15
+
16
+ def self.#{name}=(val)
17
+ singleton_class.class_eval do
18
+ remove_possible_method(:#{name})
19
+ define_method(:#{name}) { val }
20
+ end
21
+
22
+ if singleton_class?
23
+ class_eval do
24
+ remove_possible_method(:#{name})
25
+ def #{name}
26
+ defined?(@#{name}) ? @#{name} : singleton_class.#{name}
27
+ end
28
+ end
29
+ end
30
+ val
31
+ end
32
+ RUBY
33
+ end
34
+ end
35
+
36
+ private
37
+ def singleton_class?
38
+ ancestors.first != self
39
+ end
40
+ end
data/lib/famili/father.rb CHANGED
@@ -2,6 +2,9 @@ require "famili/child"
2
2
 
3
3
  module Famili
4
4
  class Father
5
+ attr_reader :attributes
6
+ attr_reader :mother
7
+
5
8
  def initialize(mother, attributes)
6
9
  @mother = mother
7
10
  @attributes = attributes
@@ -50,21 +53,13 @@ module Famili
50
53
  end
51
54
 
52
55
  def scoped(attributes = {})
53
- Famili::Father.new(@mother, merge(attributes))
54
- end
55
-
56
- def method_missing(name, *args)
57
- if scope_attributes = @mother.class.scopes[name]
58
- scoped(scope_attributes)
59
- else
60
- super(name, *args)
61
- end
56
+ self.class.new(@mother, merge(attributes))
62
57
  end
63
58
 
64
59
  private
65
60
 
66
61
  def merge(attributes)
67
- @attributes.merge(attributes)
62
+ self.attributes.merge(attributes)
68
63
  end
69
64
  end
70
65
  end
data/lib/famili/mother.rb CHANGED
@@ -2,8 +2,15 @@ require 'date'
2
2
  require 'famili/father'
3
3
  require "famili/lazy_value"
4
4
 
5
+ unless Class.respond_to?(:class_attribute)
6
+ require 'famili/class_attribute'
7
+ end
8
+
5
9
  module Famili
6
10
  class Mother
11
+ class_attribute :father_class
12
+ self.father_class = Famili::Father
13
+
7
14
  def before_save(model)
8
15
  end
9
16
 
@@ -15,7 +22,7 @@ module Famili
15
22
  end
16
23
 
17
24
  def sequence_number
18
- @sequence_number||= self.class.objects_sequence_number
25
+ @sequence_number ||= self.class.objects_sequence_number
19
26
  end
20
27
 
21
28
  class << self
@@ -72,14 +79,17 @@ module Famili
72
79
  end
73
80
 
74
81
  def new_father
75
- Famili::Father.new(self.new, attributes)
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) }
76
88
  end
77
89
 
78
90
  def scope(name, &block)
79
- scopes[name] = collect_attributes(&block)
80
- singleton_class.send(:define_method, name) do
81
- new_father.send(name)
82
- end
91
+ ensure_own_father_class.send(:define_method, name, &block)
92
+ singleton_class.delegate name, to: :new_father
83
93
  end
84
94
 
85
95
  def scopes
@@ -105,6 +115,11 @@ module Famili
105
115
 
106
116
  protected
107
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
+
108
123
  def collect_attributes
109
124
  saved_attributes, @attributes = @attributes, {}
110
125
  yield
@@ -1,5 +1,5 @@
1
1
  module Famili
2
2
  module Version
3
- STRING = '0.1.9.1'
3
+ STRING = '1.0.0'
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.1
4
+ version: 1.0.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: 2012-07-03 00:00:00.000000000 Z
13
+ date: 2012-07-06 00:00:00.000000000 Z
14
14
  dependencies: []
15
15
  description: Yet another object mother pattern implementation.
16
16
  email:
@@ -20,6 +20,7 @@ extra_rdoc_files: []
20
20
  files:
21
21
  - lib/famili.rb
22
22
  - lib/famili/child.rb
23
+ - lib/famili/class_attribute.rb
23
24
  - lib/famili/father.rb
24
25
  - lib/famili/lazy_value.rb
25
26
  - lib/famili/mother.rb
@@ -39,7 +40,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
39
40
  version: '0'
40
41
  segments:
41
42
  - 0
42
- hash: -1932633742867940772
43
+ hash: -2702514337539949147
43
44
  required_rubygems_version: !ruby/object:Gem::Requirement
44
45
  none: false
45
46
  requirements:
@@ -48,11 +49,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
48
49
  version: '0'
49
50
  segments:
50
51
  - 0
51
- hash: -1932633742867940772
52
+ hash: -2702514337539949147
52
53
  requirements: []
53
54
  rubyforge_project:
54
- rubygems_version: 1.8.10
55
+ rubygems_version: 1.8.24
55
56
  signing_key:
56
57
  specification_version: 3
57
- summary: famili-0.1.9.1
58
+ summary: famili-1.0.0
58
59
  test_files: []