Class: Zn::Associators::BelongsTo

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

Overview

BelongsTo associator helps define belongs_to relations between objects

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(datasets, params) ⇒ BelongsTo

Returns a new instance of BelongsTo.



9
10
11
12
13
14
# File 'lib/zn/associators/belongs_to.rb', line 9

def initialize(datasets, params)
  @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/belongs_to.rb', line 7

def dataset
  @dataset
end

#dataset_nameObject (readonly)

Returns the value of attribute dataset_name.



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

def dataset_name
  @dataset_name
end

#datasetsObject (readonly)

Returns the value of attribute datasets.



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

def datasets
  @datasets
end

#foreign_keyObject (readonly)

Returns the value of attribute foreign_key.



7
8
9
# File 'lib/zn/associators/belongs_to.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/belongs_to.rb', line 7

def primary_key
  @primary_key
end

#repoObject (readonly)

Returns the value of attribute repo.



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

def repo
  @repo
end

Instance Method Details

#associate(object) ⇒ Object



16
17
18
19
# File 'lib/zn/associators/belongs_to.rb', line 16

def associate(object)
  @dataset ||= datasets.fetch(dataset_name)
  dataset.search(key: primary_key, value: object[foreign_key], exact: true).to_a.first
end