Class: Famili::GrandMother

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

Direct Known Subclasses

Mother

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attributesObject



65
66
67
# File 'lib/famili/grand_mother.rb', line 65

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

.class_nameObject



39
# File 'lib/famili/grand_mother.rb', line 39

alias_method :class_name, :name

.field(method, value = nil, &block) ⇒ Object



69
70
71
72
# File 'lib/famili/grand_mother.rb', line 69

def field(method, value = nil, &block)
  block = -> { value } if value
  attributes[method] = block
end

.has(name, &block) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/famili/grand_mother.rb', line 78

def has(name, &block)
  attributes[name] = lazy 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?
    father
  end
end

.inherited(child) ⇒ Object



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

def inherited(child)
  child.parent_class = self
end

.lazy(&block) ⇒ Object



74
75
76
# File 'lib/famili/grand_mother.rb', line 74

def lazy(&block)
  Famili::LazyValue.new(&block)
end

.method_missing(method, &block) ⇒ Object



60
61
62
63
# File 'lib/famili/grand_mother.rb', line 60

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

.model_class(klass = nil) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/famili/grand_mother.rb', line 101

def model_class(klass = nil)
  if klass
    self.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

.model_class=(klass) ⇒ Object



114
115
116
# File 'lib/famili/grand_mother.rb', line 114

def model_class=(klass)
  @model_class = klass
end

.name(&block) ⇒ Object



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

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

.parent_classObject



47
48
49
# File 'lib/famili/grand_mother.rb', line 47

def parent_class
  @parent_class
end

.parent_class=(klass) ⇒ Object



43
44
45
# File 'lib/famili/grand_mother.rb', line 43

def parent_class=(klass)
  @parent_class = klass
end

.scope(name, &block) ⇒ Object



91
92
93
94
95
# File 'lib/famili/grand_mother.rb', line 91

def scope(name, &block)
  ensure_own_father_class.send(:define_method, name, &block)
  delegate name, to: :father
  singleton_class.delegate name, to: :new
end

.scopesObject



97
98
99
# File 'lib/famili/grand_mother.rb', line 97

def scopes
  @scopes ||= parent_class && parent_class.scopes.dup || {}
end

.trait(name, &block) ⇒ Object



86
87
88
89
# File 'lib/famili/grand_mother.rb', line 86

def trait(name, &block)
  attributes = collect_attributes(&block)
  scope(name) { scoped(attributes) }
end

Instance Method Details

#after_create(model) ⇒ Object

noinspection RubyUnusedLocalVariable



35
36
# File 'lib/famili/grand_mother.rb', line 35

def after_create(model)
end

#before_save(model) ⇒ Object

noinspection RubyUnusedLocalVariable



31
32
# File 'lib/famili/grand_mother.rb', line 31

def before_save(model)
end

#fatherObject



18
19
20
# File 'lib/famili/grand_mother.rb', line 18

def father
  @father ||= self.class.father_class.new(self, self.class.attributes)
end

#instantiate(attributes) ⇒ Object

noinspection RubyUnusedLocalVariable



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

def instantiate(attributes)
  self.class.model_class.new
end

#save(model) ⇒ Object



27
28
# File 'lib/famili/grand_mother.rb', line 27

def save(model)
end