Class: Famili::Mother
- Inherits:
-
Object
- Object
- Famili::Mother
- Defined in:
- lib/famili/mother.rb
Class Method Summary collapse
- .attributes ⇒ Object
- .class_name ⇒ Object
- .field(method, value = nil, &block) ⇒ Object
- .has(name, &block) ⇒ Object
- .inherited(child) ⇒ Object
- .method_missing(method, &block) ⇒ Object
- .model_class(klass = nil) ⇒ Object
- .name(&block) ⇒ Object
- .new_father ⇒ Object
- .objects_sequence_number ⇒ Object
- .parent_class ⇒ Object
- .parent_class=(klass) ⇒ Object
- .scope(name, &block) ⇒ Object
- .scopes ⇒ Object
Instance Method Summary collapse
- #after_create(model) ⇒ Object
- #before_save(model) ⇒ Object
- #sequence_number ⇒ Object
- #unique ⇒ Object
Class Method Details
.attributes ⇒ Object
53 54 55 |
# File 'lib/famili/mother.rb', line 53 def attributes @attributes ||= parent_class && parent_class.attributes.clone || {} end |
.class_name ⇒ Object
21 |
# File 'lib/famili/mother.rb', line 21 alias_method :class_name, :name |
.field(method, value = nil, &block) ⇒ Object
57 58 59 60 |
# File 'lib/famili/mother.rb', line 57 def field(method, value = nil, &block) block = -> { value } if value attributes[method] = block end |
.has(name, &block) ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/famili/mother.rb', line 62 def has(name, &block) pending_tasks.push(Proc.new do father = "#{model_class.reflect_on_association(name.to_sym).klass.name}Famili".constantize.new_father father = father.scoped(collect_attributes(&block)) if block_given? attributes[name] = father end) end |
.inherited(child) ⇒ Object
38 39 40 41 |
# File 'lib/famili/mother.rb', line 38 def inherited(child) child.parent_class = self child.pending_tasks = [] end |
.method_missing(method, &block) ⇒ Object
48 49 50 51 |
# File 'lib/famili/mother.rb', line 48 def method_missing(method, &block) return field(method, &block) if block_given? super end |
.model_class(klass = nil) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/famili/mother.rb', line 86 def model_class(klass = nil) if klass @model_class = klass return end @model_class ||= if class_name =~ /(.*)Famili$/ || class_name =~ /Famili::(.*)/ $1.split('::').inject(Object) do |mod, const| mod.const_get(const) end end end |
.name(&block) ⇒ Object
43 44 45 46 |
# File 'lib/famili/mother.rb', line 43 def name(&block) return class_name unless block_given? field(:name, &block) end |
.new_father ⇒ Object
70 71 72 73 |
# File 'lib/famili/mother.rb', line 70 def new_father invoke_pending_tasks if pending_tasks Famili::Father.new(self.new, attributes) end |
.objects_sequence_number ⇒ Object
25 26 27 28 |
# File 'lib/famili/mother.rb', line 25 def objects_sequence_number @sequence_number ||= 0 @sequence_number += 1 end |
.parent_class ⇒ Object
34 35 36 |
# File 'lib/famili/mother.rb', line 34 def parent_class @parent_class end |
.parent_class=(klass) ⇒ Object
30 31 32 |
# File 'lib/famili/mother.rb', line 30 def parent_class=(klass) @parent_class = klass end |
.scope(name, &block) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/famili/mother.rb', line 75 def scope(name, &block) scopes[name] = collect_attributes(&block) singleton_class.send(:define_method, name) do new_father.send(name) end end |
.scopes ⇒ Object
82 83 84 |
# File 'lib/famili/mother.rb', line 82 def scopes @scopes ||= parent_class && parent_class.scopes.dup || {} end |
Instance Method Details
#after_create(model) ⇒ Object
9 10 |
# File 'lib/famili/mother.rb', line 9 def after_create(model) end |
#before_save(model) ⇒ Object
6 7 |
# File 'lib/famili/mother.rb', line 6 def before_save(model) end |
#sequence_number ⇒ Object
16 17 18 |
# File 'lib/famili/mother.rb', line 16 def sequence_number @sequence_number||= self.class.objects_sequence_number end |
#unique ⇒ Object
12 13 14 |
# File 'lib/famili/mother.rb', line 12 def unique "#{"%10.6f" % Time.now.to_f}#{object_id.abs}" end |