Class: Zn::Dataset

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/zn/dataset.rb

Overview

Holds repo, associators and performs aggregate operations

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo) ⇒ Dataset

Returns a new instance of Dataset.



12
13
14
15
# File 'lib/zn/dataset.rb', line 12

def initialize(repo)
  @repo = repo
  @associators = {}
end

Instance Attribute Details

#associatorsObject (readonly)

Returns the value of attribute associators.



10
11
12
# File 'lib/zn/dataset.rb', line 10

def associators
  @associators
end

#repoObject (readonly)

Returns the value of attribute repo.



10
11
12
# File 'lib/zn/dataset.rb', line 10

def repo
  @repo
end

Instance Method Details

#add_associator(name, associator) ⇒ Object



17
18
19
# File 'lib/zn/dataset.rb', line 17

def add_associator(name, associator)
  @associators[name] = associator
end

#associate(entities) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/zn/dataset.rb', line 37

def associate(entities)
  return enum_for(:associate, entities) unless block_given?

  entities.each do |entity|
    yield entity, associations(entity)
  end
end

#associations(entity) ⇒ Object



31
32
33
34
35
# File 'lib/zn/dataset.rb', line 31

def associations(entity)
  associators.each_with_object({}) do |(name, associator), hash|
    hash[name] = associator.associate(entity)
  end
end

#keysObject



21
22
23
24
25
26
27
28
29
# File 'lib/zn/dataset.rb', line 21

def keys
  keys = Set.new

  repo.all.each do |entity|
    keys = keys.merge entity.keys
  end

  keys.to_a
end