Module: Vedeu::Model::ClassMethods
- Defined in:
- lib/vedeu/repositories/model.rb
Overview
When Vedeu::Model is included in a class, the methods within this module are included as class methods on that class.
Instance Attribute Summary collapse
- #repository ⇒ Vedeu::Repository readonly
Instance Method Summary collapse
-
#build(attributes = {}, &block) ⇒ Object
Build models using a simple DSL when a block is given, otherwise returns a new instance of the class including this module.
-
#by_name(name) ⇒ void
Fetch an instance of a repository’s model by name.
-
#child(klass) ⇒ void
(also: #member, #collection)
Provide a convenient way to define the child or children of a model.
-
#defaults ⇒ Hash<Symbol => NilClass, String>
private
The default values for a new instance of this class.
-
#repo(klass) ⇒ void
Allow models to specify their repository using a class method.
Instance Attribute Details
#repository ⇒ Vedeu::Repository (readonly)
21 22 23 |
# File 'lib/vedeu/repositories/model.rb', line 21 def repository @repository end |
Instance Method Details
#build(attributes = {}, &block) ⇒ Object
Build models using a simple DSL when a block is given, otherwise returns a new instance of the class including this module.
31 32 33 34 35 36 37 |
# File 'lib/vedeu/repositories/model.rb', line 31 def build(attributes = {}, &block) attributes = defaults.merge!(attributes) model = new(attributes) model.deputy(attributes[:client]).instance_eval(&block) if block_given? model end |
#by_name(name) ⇒ void
This method returns an undefined value.
Fetch an instance of a repository’s model by name.
43 44 45 |
# File 'lib/vedeu/repositories/model.rb', line 43 def by_name(name) repository.by_name(name) if repository end |
#child(klass) ⇒ void Also known as: member, collection
This method returns an undefined value.
Provide a convenient way to define the child or children of a model.
53 54 55 |
# File 'lib/vedeu/repositories/model.rb', line 53 def child(klass) send(:define_method, __callee__) { klass } end |
#defaults ⇒ Hash<Symbol => NilClass, String> (private)
The default values for a new instance of this class.
72 73 74 75 76 77 |
# File 'lib/vedeu/repositories/model.rb', line 72 def defaults { client: nil, name: '', } end |
#repo(klass) ⇒ void
This method returns an undefined value.
Allow models to specify their repository using a class method.
63 64 65 |
# File 'lib/vedeu/repositories/model.rb', line 63 def repo(klass) @repository = klass end |