Class: Terrestrial::RelationalStore

Inherits:
Object
  • Object
show all
Includes:
Enumerable, InspectionString
Defined in:
lib/terrestrial/relational_store.rb

Instance Method Summary collapse

Methods included from InspectionString

#inspect

Constructor Details

#initialize(mappings:, mapping_name:, datastore:, load_pipeline:, dump_pipeline:, dataset: nil) ⇒ RelationalStore

Returns a new instance of RelationalStore.



10
11
12
13
14
15
16
17
18
# File 'lib/terrestrial/relational_store.rb', line 10

def initialize(mappings:, mapping_name:, datastore:, load_pipeline:, dump_pipeline:, dataset: nil)
  @mappings = mappings
  @mapping_name = mapping_name
  @datastore = datastore
  @dataset = dataset
  @load_pipeline = load_pipeline
  @dump_pipeline = dump_pipeline
  @eager_data = {}
end

Instance Method Details

#allObject



47
48
49
# File 'lib/terrestrial/relational_store.rb', line 47

def all
  self
end

#changes(graph) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/terrestrial/relational_store.rb', line 37

def changes(graph)
  changes, _ = dump_pipeline
    .take_until(:remove_unchanged_fields)
    .call(
      serialize_graph(graph)
    )

  changes
end

#changes_sql(graph) ⇒ Object



31
32
33
34
35
# File 'lib/terrestrial/relational_store.rb', line 31

def changes_sql(graph)
  changes(graph).map { |record|
    datastore.changes_sql(record)
  }
end

#delete(object) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/terrestrial/relational_store.rb', line 77

def delete(object)
  dump_pipeline.call(
    serialize_graph(object)
      .select { |record| record.depth == 0 }
      .reverse
      .take(1)
      .map { |record| DeletedRecord.new(mapping, record.attributes, 0) }
  )
end

#each(&block) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/terrestrial/relational_store.rb', line 63

def each(&block)
  dataset
    .map { |record|
      graph_loader.call(mapping_name, record, @eager_data)
    }
    .each(&block)
end

#eager_load(association_name_map) ⇒ Object



71
72
73
74
75
# File 'lib/terrestrial/relational_store.rb', line 71

def eager_load(association_name_map)
  @eager_data = eager_load_associations(mapping, dataset, association_name_map)

  self
end

#save(graph) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/terrestrial/relational_store.rb', line 23

def save(graph)
  record_dump = serialize_graph(graph)

  dump_pipeline.call(record_dump)

  self
end

#subset(name, *params) ⇒ Object



57
58
59
60
61
# File 'lib/terrestrial/relational_store.rb', line 57

def subset(name, *params)
  new_with_dataset(
    mapping.subsets.execute(dataset, name, *params)
  )
end

#where(query) ⇒ Object



51
52
53
54
55
# File 'lib/terrestrial/relational_store.rb', line 51

def where(query)
  new_with_dataset(
    dataset.where(query)
  )
end