class Store

Constants

VERSION

Public Class Methods

new(entity_classes, data_mapping, entity_mapping) click to toggle source
# File lib/store.rb, line 9
def initialize(entity_classes, data_mapping, entity_mapping)
  @entity_classes         = entity_classes
  @resolver_data_mapper   = Resolver::DataMapper.new(data_mapping)
  @resolver_entity_mapper = Resolver::EntityMapper.new(self, entity_mapping)
  @entity_references      = EntityReferences.new
end

Public Instance Methods

build(entity_class_name, attributes = {}) click to toggle source
# File lib/store.rb, line 16
def build(entity_class_name, attributes = {})
  entity = @entity_classes[entity_class_name].new

  attributes.each do |attribute, value|
    entity.public_send :"#{attribute}=", value
  end

  entity
end
bulk_load(refs) click to toggle source
# File lib/store.rb, line 90
def bulk_load(refs)
  data_mapper = data_mapper(refs.first)
  entity_class = @entity_classes[refs.first.entity_type]

  references = refs.map(&:reference)
  mapped_entities = data_mapper.bulk_find(references)

  entities = []

  references.each_with_index do |reference, i|
    mapped_entity = mapped_entities[i]

    entity = entity_mapper(refs.first).unmap(entity_class, mapped_entity)
    save_entity_reference reference, entity
    entities << entity
  end

  entities
end
load(refs) click to toggle source
# File lib/store.rb, line 62
def load(refs)
  if refs.kind_of? Array
    bulk_load refs
  else
    single_load refs
  end
end
query(query) click to toggle source
# File lib/store.rb, line 47
def query(query)
  data_mapper = data_mapper(query)
  entity_class = @entity_classes[query.entity]

  raise_unless_data_mapper_found(query, data_mapper)

  mapped_entities_with_references = data_mapper.select(query) || {}

  mapped_entities_with_references.map do |reference, mapped_entity|
    entity = entity_mapper(query).unmap(entity_class, mapped_entity)
    save_entity_reference reference, entity
    entity
  end
end
reference(entity) click to toggle source
# File lib/store.rb, line 110
def reference(entity)
  reference = find_entity_reference(entity)
  raise "Unknown entity #{entity.inspect}" unless reference
  build_reference entity, reference if reference
end
reload(entity) click to toggle source
# File lib/store.rb, line 70
def reload(entity)
  reference = find_entity_reference(entity)
  raise "Unknown entity #{entity.inspect}" unless reference
  ref = Store::Ref.new(extract_entity_class_name(entity), reference)
  single_load(ref)
end
remove(entity) click to toggle source
# File lib/store.rb, line 37
def remove(entity)
  data_mapper = data_mapper(entity)
  raise_unless_data_mapper_found(entity, data_mapper)

  reference = find_entity_reference(entity)
  data_mapper.delete(reference)

  remove_entity_reference entity
end
save(entities) click to toggle source
# File lib/store.rb, line 26
def save(entities)
  if entities.kind_of? Array
    entities.map do |entity|
      save_entity(entity)
    end
  else
    entity = entities
    save_entity(entity)
  end
end
single_load(ref) click to toggle source
# File lib/store.rb, line 77
def single_load(ref)
  data_mapper = data_mapper(ref)
  entity_class = @entity_classes[ref.entity_type]
  reference = ref.reference

  mapped_entity = data_mapper.single_find(reference)
  raise "Entity not found for reference #{ref.inspect}" unless mapped_entity

  entity = entity_mapper(ref).unmap(entity_class, mapped_entity)
  save_entity_reference reference, entity
  entity
end

Private Instance Methods

build_reference(entity, reference) click to toggle source
# File lib/store.rb, line 134
def build_reference(entity, reference)
  Ref.new(extract_entity_class_name(entity), reference)
end
data_mapper(obj) click to toggle source
# File lib/store.rb, line 142
def data_mapper(obj)
  @resolver_data_mapper.resolve_data_mapper(obj)
end
entity_mapper(obj) click to toggle source
# File lib/store.rb, line 146
def entity_mapper(obj)
  @resolver_entity_mapper.resolve_entity_mapper(obj)
end
extract_entity_attributes(entity) click to toggle source
# File lib/store.rb, line 150
def extract_entity_attributes(entity)
  entity_mapper(entity).map(entity)
end
extract_entity_class_name(obj) click to toggle source
# File lib/store.rb, line 154
def extract_entity_class_name(obj)
  @resolver_entity_mapper.extract_entity_class_name_from_object(obj)
end
find_entity_reference(entity) click to toggle source
# File lib/store.rb, line 158
def find_entity_reference(entity)
  @entity_references.find(entity)
end
raise_unless_data_mapper_found(source, data_mapper) click to toggle source
# File lib/store.rb, line 138
def raise_unless_data_mapper_found(source, data_mapper)
  raise "No data_mapper for #{source.inspect} found" unless data_mapper
end
remove_entity_reference(entity) click to toggle source
# File lib/store.rb, line 166
def remove_entity_reference(entity)
  @entity_references.remove entity
end
save_entity(entity) click to toggle source
# File lib/store.rb, line 117
def save_entity(entity)
  return nil unless entity

  data_mapper = data_mapper(entity)
  raise_unless_data_mapper_found(entity, data_mapper)

  reference = find_entity_reference(entity)
  if reference.nil?
    reference = data_mapper.insert(extract_entity_attributes(entity))
    save_entity_reference(reference, entity)
  else
    data_mapper.update(reference, extract_entity_attributes(entity))
  end

  build_reference entity, reference
end
save_entity_reference(reference, entity) click to toggle source
# File lib/store.rb, line 162
def save_entity_reference(reference, entity)
  @entity_references.save(reference, entity)
end