Class: Zn::Associators::HasMany
- Inherits:
-
Object
- Object
- Zn::Associators::HasMany
- Defined in:
- lib/zn/associators/has_many.rb
Overview
HasMany associator helps define has_many relations between objects
Instance Attribute Summary collapse
-
#dataset ⇒ Object
readonly
Returns the value of attribute dataset.
-
#dataset_name ⇒ Object
readonly
Returns the value of attribute dataset_name.
-
#datasets ⇒ Object
readonly
Returns the value of attribute datasets.
-
#foreign_key ⇒ Object
readonly
Returns the value of attribute foreign_key.
-
#primary_key ⇒ Object
readonly
Returns the value of attribute primary_key.
-
#repo ⇒ Object
readonly
Returns the value of attribute repo.
Instance Method Summary collapse
- #associate(object) ⇒ Object
-
#initialize(datasets, params) ⇒ HasMany
constructor
A new instance of HasMany.
Constructor Details
#initialize(datasets, params) ⇒ HasMany
Returns a new instance of HasMany.
9 10 11 12 13 14 15 16 |
# File 'lib/zn/associators/has_many.rb', line 9 def initialize(datasets, params) raise ArgumentError, 'Datasets is nil' if datasets.nil? @datasets = datasets @dataset_name = params.fetch('dataset') @foreign_key = params.fetch('foreign_key') @primary_key = params.fetch('primary_key') end |
Instance Attribute Details
#dataset ⇒ Object (readonly)
Returns the value of attribute dataset.
7 8 9 |
# File 'lib/zn/associators/has_many.rb', line 7 def dataset @dataset end |
#dataset_name ⇒ Object (readonly)
Returns the value of attribute dataset_name.
7 8 9 |
# File 'lib/zn/associators/has_many.rb', line 7 def dataset_name @dataset_name end |
#datasets ⇒ Object (readonly)
Returns the value of attribute datasets.
7 8 9 |
# File 'lib/zn/associators/has_many.rb', line 7 def datasets @datasets end |
#foreign_key ⇒ Object (readonly)
Returns the value of attribute foreign_key.
7 8 9 |
# File 'lib/zn/associators/has_many.rb', line 7 def foreign_key @foreign_key end |
#primary_key ⇒ Object (readonly)
Returns the value of attribute primary_key.
7 8 9 |
# File 'lib/zn/associators/has_many.rb', line 7 def primary_key @primary_key end |
#repo ⇒ Object (readonly)
Returns the value of attribute repo.
7 8 9 |
# File 'lib/zn/associators/has_many.rb', line 7 def repo @repo end |
Instance Method Details
#associate(object) ⇒ Object
18 19 20 21 22 |
# File 'lib/zn/associators/has_many.rb', line 18 def associate(object) @dataset ||= datasets.fetch(dataset_name) dataset.search(key: foreign_key, value: object.fetch(primary_key), exact: true).to_a end |