module Poros::ClassMethods

Attributes

data_changed[RW]
in_transaction[RW]

Public Instance Methods

all() click to toggle source
# File lib/poros/class_methods.rb, line 53
def all
  Dir.glob(File.join(data_directory, '*.yml')).map { |file|
    next if file == index_file
    data = YAML.load(File.read(file))
    find(data[:uuid])
  }.compact
end
data_directory() click to toggle source
# File lib/poros/class_methods.rb, line 41
def data_directory
  "./db/#{self}/"
end
file_name(uuid) click to toggle source
# File lib/poros/class_methods.rb, line 45
def file_name(uuid)
  "#{uuid}.yml"
end
file_path(uuid) click to toggle source
# File lib/poros/class_methods.rb, line 36
def file_path(uuid)
  FileUtils.mkdir_p(data_directory) unless File.exist?(data_directory)
  File.join(data_directory, file_name(uuid))
end
find(uuid) click to toggle source
# File lib/poros/class_methods.rb, line 27
def find(uuid)
  attrs = YAML.load(File.read(file_path(uuid)))
  attrs.delete(:uuid)

  object = new(**attrs)
  object.uuid = uuid
  object
end
index_data() click to toggle source
# File lib/poros/class_methods.rb, line 65
def index_data
  return @index_data if defined? @index_data

  data = File.exist?(index_file) ? YAML.load(File.read(index_file)) : {}
  # Make sure we always have every index as a key
  poro_indexes.each do |index|
    data[index] = {} unless data.has_key?(index)
  end

  @index_data = data
end
index_file() click to toggle source
# File lib/poros/class_methods.rb, line 49
def index_file
  File.join(data_directory, "indexes.yml")
end
poro_attr(*attrs) click to toggle source
# File lib/poros/class_methods.rb, line 7
def poro_attr(*attrs)
  @poro_attrs = [:uuid] | attrs
  attrs.each { |column|
    class_eval "attr_accessor :#{column}"
  }
end
poro_attrs() click to toggle source
# File lib/poros/class_methods.rb, line 14
def poro_attrs
  @poro_attrs ||= []
end
poro_index(*attrs) click to toggle source
# File lib/poros/class_methods.rb, line 18
def poro_index(*attrs)
  @poro_indexes ||= []
  @poro_indexes += attrs
end
poro_indexes() click to toggle source
# File lib/poros/class_methods.rb, line 23
def poro_indexes
  @poro_indexes ||= []
end
rebuild_indexes() click to toggle source
# File lib/poros/class_methods.rb, line 112
def rebuild_indexes
  transaction do
    @data_changed = true
    @index_data = {}
    File.delete(index_file) if File.exist?(index_file)
    all.each { |object| update_index(object) }
  end
end
remove_from_index(object, perist = true) click to toggle source
# File lib/poros/class_methods.rb, line 97
def remove_from_index(object, perist = true)
  index_data
  @data_changed = true

  poro_indexes.each do |index|
    @index_data[index] = {} unless @index_data.has_key?(index)
    @index_data[index].keys.each do |value|
      @index_data[index][value] ||= []
      @index_data[index][value] -= [object.uuid]
      @index_data[index].delete(value) if @index_data[index][value] == []
    end
  end
  write_index_data if perist
end
transaction(&block) click to toggle source
# File lib/poros/class_methods.rb, line 121
def transaction(&block)
  @in_transaction = true
  @data_changed = false
  block.call
  @in_transaction = false
  write_index_data if @data_changed
end
update_index(object) click to toggle source
# File lib/poros/class_methods.rb, line 82
def update_index(object)
  remove_from_index(object, false)

  index_data

  poro_indexes.each do |index|
    @index_data[index] = {} unless @index_data.has_key?(index)
    value = object.send(index)
    @index_data[index][value] ||= []
    @index_data[index][value] = @index_data[index][value] | [object.uuid]
  end

  write_index_data
end
where(query) click to toggle source
# File lib/poros/class_methods.rb, line 61
def where(query)
  Poros::Query.new(self).where(query)
end
write_index_data() click to toggle source
# File lib/poros/class_methods.rb, line 77
def write_index_data
  return if in_transaction
  File.write(index_file, @index_data.to_yaml)
end