Class: Famili::Mother

Inherits:
Object
  • Object
show all
Defined in:
lib/famili/mother.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attributesObject



50
51
52
53
# File 'lib/famili/mother.rb', line 50

def attributes
  @attributes||=parent_class && parent_class.attributes.clone || {}
  @attributes
end

.build(opts = {}) ⇒ Object



67
68
69
# File 'lib/famili/mother.rb', line 67

def build(opts = {})
  father.build(opts)
end

.build_brothers(num, opts = {}, &block) ⇒ Object



75
76
77
# File 'lib/famili/mother.rb', line 75

def build_brothers(num, opts = {}, &block)
  father.build_brothers(num, opts, &block)
end

.build_hash(opts = {}) ⇒ Object



71
72
73
# File 'lib/famili/mother.rb', line 71

def build_hash(opts = {})
  father.build_hash(opts)
end

.class_nameObject



21
# File 'lib/famili/mother.rb', line 21

alias :class_name :name

.create(opts = {}) ⇒ Object



63
64
65
# File 'lib/famili/mother.rb', line 63

def create(opts = {})
  father.create(opts)
end

.create_brothers(num, opts = {}, &block) ⇒ Object



79
80
81
# File 'lib/famili/mother.rb', line 79

def create_brothers(num, opts = {}, &block)
  father.create_brothers(num, opts, &block)
end

.fatherObject



59
60
61
# File 'lib/famili/mother.rb', line 59

def father
  @father ||= Famili::Father.new(self.new, attributes)
end

.field(method, &block) ⇒ Object



55
56
57
# File 'lib/famili/mother.rb', line 55

def field(method, &block)
  attributes[method] = block
end

.inherited(child) ⇒ Object



36
37
38
# File 'lib/famili/mother.rb', line 36

def inherited(child)
  child.parent_class = self
end

.method_missing(method, &block) ⇒ Object



45
46
47
48
# File 'lib/famili/mother.rb', line 45

def method_missing(method, &block)
  return field(method, &block) if block_given?
  super
end

.model_class(klass = nil) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/famili/mother.rb', line 103

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



40
41
42
43
# File 'lib/famili/mother.rb', line 40

def name(&block)
  return class_name unless block_given?
  field(:name, &block)
end

.objects_sequence_numberObject



23
24
25
26
# File 'lib/famili/mother.rb', line 23

def objects_sequence_number
  @sequence_number ||=0
  @sequence_number += 1
end

.parent_classObject



32
33
34
# File 'lib/famili/mother.rb', line 32

def parent_class
  @parent_class
end

.parent_class=(klass) ⇒ Object



28
29
30
# File 'lib/famili/mother.rb', line 28

def parent_class=(klass)
  @parent_class = klass
end

.scope(name) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/famili/mother.rb', line 87

def scope(name)
  saved_attributes = @attributes
  @attributes = {}
  yield
  scopes[name] = @attributes
  singleton_class.send(:define_method, name) do
    father.send(name)
  end
ensure
  @attributes = saved_attributes
end

.scoped(attributes = {}) ⇒ Object



83
84
85
# File 'lib/famili/mother.rb', line 83

def scoped(attributes = {})
  father.scoped(attributes)
end

.scopesObject



99
100
101
# File 'lib/famili/mother.rb', line 99

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_numberObject



16
17
18
# File 'lib/famili/mother.rb', line 16

def sequence_number
  @sequence_number||= self.class.objects_sequence_number
end

#uniqueObject



12
13
14
# File 'lib/famili/mother.rb', line 12

def unique
  "#{"%10.6f" % Time.now.to_f}#{object_id.abs}"
end