class Foxy::Storages::Yaml

Attributes

collection[RW]

Public Class Methods

new(collection) click to toggle source
# File lib/foxy/storages/yaml.rb, line 8
def initialize(collection)
  @collection = collection
end

Public Instance Methods

add(attrs) click to toggle source
# File lib/foxy/storages/yaml.rb, line 16
def add(attrs)
  attrs.tap { store.transaction { all! << attrs } }
end
all() click to toggle source
# File lib/foxy/storages/yaml.rb, line 20
def all
  store.transaction { all! }
end
delete(attrs) click to toggle source
# File lib/foxy/storages/yaml.rb, line 24
def delete(attrs)
  store.transaction {
    before = all!.count
    all!.delete_if(&query(attrs))
    before - all!.count
  }
end
delete_all() click to toggle source
# File lib/foxy/storages/yaml.rb, line 36
def delete_all
  File.delete store.path if File.exist? store.path
  @store = nil

  true
end
update(attrs, &block) click to toggle source
# File lib/foxy/storages/yaml.rb, line 32
def update(attrs, &block)
  store.transaction { all!.select(&query(attrs)).each(&block) }
end
where(attrs) click to toggle source
# File lib/foxy/storages/yaml.rb, line 12
def where(attrs)
  all.select(&query(attrs))
end

Private Instance Methods

all!() click to toggle source
# File lib/foxy/storages/yaml.rb, line 59
def all!
  store[:items] ||= []
end
env() click to toggle source
# File lib/foxy/storages/yaml.rb, line 72
def env
  Foxy.env && "-#{Foxy.env}"
end
path() click to toggle source
# File lib/foxy/storages/yaml.rb, line 68
def path
  "#{store_folder}/#{collection}#{env}.store.yaml"
end
query(attrs) click to toggle source
# File lib/foxy/storages/yaml.rb, line 53
def query(attrs)
  keys, values = attrs.keys, attrs.values

  Proc.new { |item| item.values_at(*keys) == values }
end
store() click to toggle source
# File lib/foxy/storages/yaml.rb, line 49
def store
  @store ||= store!
end
store!() click to toggle source
# File lib/foxy/storages/yaml.rb, line 63
def store!
  FileUtils.makedir_p(store_folder.split("/"))
  YAML::Store.new(path).tap { |s| s.transaction { s[:items] ||= [] } }
end
store_folder() click to toggle source
# File lib/foxy/storages/yaml.rb, line 45
def store_folder
  "store"
end