Class: Zn::Associators::HasMany

Inherits:
Object
  • Object
show all
Defined in:
lib/zn/associators/has_many.rb

Overview

HasMany associator helps define has_many relations between objects

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(datasets, params) ⇒ HasMany

Returns a new instance of HasMany.

Raises:

  • (ArgumentError)


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

#datasetObject (readonly)

Returns the value of attribute dataset.



7
8
9
# File 'lib/zn/associators/has_many.rb', line 7

def dataset
  @dataset
end

#dataset_nameObject (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

#datasetsObject (readonly)

Returns the value of attribute datasets.



7
8
9
# File 'lib/zn/associators/has_many.rb', line 7

def datasets
  @datasets
end

#foreign_keyObject (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_keyObject (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

#repoObject (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