module Terrestrial::PublicConveniencies::Private

Public Instance Methods

build_dirty_map(storage = {}) click to toggle source
# File lib/terrestrial/public_conveniencies.rb, line 96
def build_dirty_map(storage = {})
  DirtyMap.new(storage)
end
build_dump_pipeline(dirty_map:, datastore:, clock:) click to toggle source
# File lib/terrestrial/public_conveniencies.rb, line 132
def build_dump_pipeline(dirty_map:, datastore:, clock:)
  Terrestrial::FunctionalPipeline.from_array([
    [:dedup, :uniq.to_proc],
    [:sort_by_depth, ->(rs) { rs.sort_by(&:depth) }],
    [:select_changed, ->(rs) { rs.select { |r| dirty_map.dirty?(r) } }],
    [:remove_unchanged_fields, ->(rs) { rs.map { |r| dirty_map.reject_unchanged_fields(r) } }],
    [:save_records, ->(rs) {
      datastore.transaction {
          rs.each { |r|
            r.if_upsert(&datastore.method(:upsert))
            r.if_delete(&datastore.method(:delete))
          }
        }
      }
    ],
    [:add_new_records_to_dirty_map, ->(rs) { rs.map { |r| dirty_map.load_if_new(r) } }],
  ])
end
build_identity_map(storage = {}) click to toggle source
# File lib/terrestrial/public_conveniencies.rb, line 92
def build_identity_map(storage = {})
  IdentityMap.new(storage)
end
build_load_pipeline(dirty_map:, identity_map:) click to toggle source
# File lib/terrestrial/public_conveniencies.rb, line 113
def build_load_pipeline(dirty_map:, identity_map:)
  ->(mapping, record, associated_fields = {}) {
    [
      ->(record) { Record.new(mapping, record) },
      dirty_map.method(:load),
      ->(record) {
        attributes = record.to_h.select { |k,_v|
          mapping.fields.include?(k)
        }

        object = mapping.load(attributes.merge(associated_fields))
        identity_map.call(mapping, record, object)
      },
    ].reduce(record) { |agg, operation|
        operation.call(agg)
      }
  }
end
datastore_adapter(datastore) click to toggle source
# File lib/terrestrial/public_conveniencies.rb, line 100
def datastore_adapter(datastore)
  if datastore.is_a?(Terrestrial::Adapters::AbstractAdapter)
    return datastore
  end

  case datastore.class.name
  when "Sequel::Postgres::Database"
    Adapters::SequelPostgresAdapter.new(datastore)
  else
    raise "No adapter found for #{datastore.inspect}"
  end
end
relational_store(mappings:, name:, datastore:, identity_map:, dirty_map:, load_pipeline:, dump_pipeline:) click to toggle source
# File lib/terrestrial/public_conveniencies.rb, line 82
def relational_store(mappings:, name:, datastore:, identity_map:, dirty_map:, load_pipeline:, dump_pipeline:)
  RelationalStore.new(
    mappings: mappings,
    mapping_name: name,
    datastore: datastore,
    load_pipeline: load_pipeline,
    dump_pipeline: dump_pipeline,
  )
end