Class: Famili::GrandMother
- Inherits:
-
Object
- Object
- Famili::GrandMother
- Defined in:
- lib/famili/grand_mother.rb
Direct Known Subclasses
Class Method Summary collapse
- .attributes ⇒ Object
- .class_name ⇒ Object
- .field(method, value = nil, &block) ⇒ Object
- .has(name, &block) ⇒ Object
- .inherited(child) ⇒ Object
- .lazy(&block) ⇒ Object
- .method_missing(method, &block) ⇒ Object
- .model_class(klass = nil) ⇒ Object
- .model_class=(klass) ⇒ Object
- .name(&block) ⇒ Object
- .parent_class ⇒ Object
- .parent_class=(klass) ⇒ Object
- .scope(name, &block) ⇒ Object
- .scopes ⇒ Object
- .trait(name, &block) ⇒ Object
Instance Method Summary collapse
-
#after_create(model) ⇒ Object
noinspection RubyUnusedLocalVariable.
-
#before_save(model) ⇒ Object
noinspection RubyUnusedLocalVariable.
- #born(child) ⇒ Object
- #father ⇒ Object
-
#instantiate(attributes) ⇒ Object
noinspection RubyUnusedLocalVariable.
- #save(model) ⇒ Object
Class Method Details
.attributes ⇒ Object
71 72 73 |
# File 'lib/famili/grand_mother.rb', line 71 def attributes @attributes ||= parent_class && parent_class.attributes.clone || {} end |
.class_name ⇒ Object
45 |
# File 'lib/famili/grand_mother.rb', line 45 alias_method :class_name, :name |
.field(method, value = nil, &block) ⇒ Object
75 76 77 78 |
# File 'lib/famili/grand_mother.rb', line 75 def field(method, value = nil, &block) block = -> { value } if value attributes[method] = block end |
.has(name, &block) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/famili/grand_mother.rb', line 84 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
57 58 59 |
# File 'lib/famili/grand_mother.rb', line 57 def inherited(child) child.parent_class = self end |
.lazy(&block) ⇒ Object
80 81 82 |
# File 'lib/famili/grand_mother.rb', line 80 def lazy(&block) Famili::LazyValue.new(&block) end |
.method_missing(method, &block) ⇒ Object
66 67 68 69 |
# File 'lib/famili/grand_mother.rb', line 66 def method_missing(method, &block) return field(method, &block) if block_given? super end |
.model_class(klass = nil) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/famili/grand_mother.rb', line 107 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
120 121 122 |
# File 'lib/famili/grand_mother.rb', line 120 def model_class=(klass) @model_class = klass end |
.name(&block) ⇒ Object
61 62 63 64 |
# File 'lib/famili/grand_mother.rb', line 61 def name(&block) return class_name unless block_given? field(:name, &block) end |
.parent_class ⇒ Object
53 54 55 |
# File 'lib/famili/grand_mother.rb', line 53 def parent_class @parent_class end |
.parent_class=(klass) ⇒ Object
49 50 51 |
# File 'lib/famili/grand_mother.rb', line 49 def parent_class=(klass) @parent_class = klass end |
.scope(name, &block) ⇒ Object
97 98 99 100 101 |
# File 'lib/famili/grand_mother.rb', line 97 def scope(name, &block) ensure_own_father_class.send(:define_method, name, &block) delegate name, to: :father singleton_class.delegate name, to: :new end |
.scopes ⇒ Object
103 104 105 |
# File 'lib/famili/grand_mother.rb', line 103 def scopes @scopes ||= parent_class && parent_class.scopes.dup || {} end |
.trait(name, &block) ⇒ Object
92 93 94 95 |
# File 'lib/famili/grand_mother.rb', line 92 def trait(name, &block) attributes = collect_attributes(&block) scope(name) { scoped(attributes) } end |
Instance Method Details
#after_create(model) ⇒ Object
noinspection RubyUnusedLocalVariable
41 42 |
# File 'lib/famili/grand_mother.rb', line 41 def after_create(model) end |
#before_save(model) ⇒ Object
noinspection RubyUnusedLocalVariable
37 38 |
# File 'lib/famili/grand_mother.rb', line 37 def before_save(model) end |
#born(child) ⇒ Object
27 28 29 30 31 |
# File 'lib/famili/grand_mother.rb', line 27 def born(child) child.resolve_attributes(instantiate(child)) do |instance, name, value| instance.send("#{name}=", value) end end |
#father ⇒ Object
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
33 34 |
# File 'lib/famili/grand_mother.rb', line 33 def save(model) end |