module ActiveYaml::ClassMethods

Public Instance Methods

add(record) click to toggle source
# File lib/active_yaml.rb, line 14
def add(record)
  store.transaction do
    id = (store.roots.max || 0) + 1
    record.send(:id=, id)
    store[id] = record
  end
end
all() click to toggle source
# File lib/active_yaml.rb, line 48
def all
  records = []
  store.transaction(true) do
    store.roots.each do |id|
      records << store[id]
    end
  end
  records
end
any?() click to toggle source
# File lib/active_yaml.rb, line 64
def any?
  store.transaction(true) do
    store.roots.any?
  end
end
count() click to toggle source
# File lib/active_yaml.rb, line 58
def count
  store.transaction(true) do
    store.roots.size
  end
end
destroy(id) click to toggle source
# File lib/active_yaml.rb, line 28
def destroy(id)
  store.transaction do
    store.delete(id)
  end
end
empty?() click to toggle source
# File lib/active_yaml.rb, line 70
def empty?
  store.transaction(true) do
    store.roots.empty?
  end
end
find(id) click to toggle source
# File lib/active_yaml.rb, line 76
def find(id)
  store.transaction(true) do
    store[id]
  end
end
find_by(condition) click to toggle source
# File lib/active_yaml.rb, line 82
def find_by(condition)
  key, value = condition.first
  all.each do |record|
    return record if record[key] == value
  end
end
first() click to toggle source
# File lib/active_yaml.rb, line 34
def first
  store.transaction(true) do
    id = store.roots.min
    store[id]
  end
end
last() click to toggle source
# File lib/active_yaml.rb, line 41
def last
  store.transaction(true) do
    size = store.roots.size
    store[size]
  end
end
store() click to toggle source
# File lib/active_yaml.rb, line 10
def store
  @store ||= YAML::Store.new("db/#{self.to_s.downcase}.yml")
end
update(record) click to toggle source
# File lib/active_yaml.rb, line 22
def update(record)
  store.transaction do
    store[record.id] = record
  end
end
where(conditions) click to toggle source
# File lib/active_yaml.rb, line 89
def where(conditions)
  found = false
  matches = []
  all.each do |record|
    conditions.each do |condition|
      key, value = condition
      found = (record[key] == value)
    end
    matches << record if found
  end
  matches
end