class SocialSnippet::DocumentBackend::YAMLDocument

Attributes

fields[R]
id[RW]
path[R]

Public Class Methods

activate!() click to toggle source

replace self with ::SocialSnippet::Document class

# File lib/social_snippet/document_backend/yaml_document.rb, line 241
def activate!
  ::SocialSnippet.class_eval do
    remove_const :Document
    const_set :Document, YAMLDocument
  end
end
all() click to toggle source

Queries

# File lib/social_snippet/document_backend/yaml_document.rb, line 142
def all
  Query.new self, collection
end
collection() click to toggle source
# File lib/social_snippet/document_backend/yaml_document.rb, line 190
def collection
  yaml_document_hash[self.name] ||= ::Hash.new
end
count() click to toggle source
# File lib/social_snippet/document_backend/yaml_document.rb, line 150
def count
  all.count
end
create(options = {}) click to toggle source
# File lib/social_snippet/document_backend/yaml_document.rb, line 231
def create(options = {})
  doc = new
  options.each {|k, v| doc.send "#{k}=", v }
  doc.id = uuid if doc.id.nil?
  collection[doc.id] = doc.serialize
  update_file!
  doc
end
default_field() click to toggle source
# File lib/social_snippet/document_backend/yaml_document.rb, line 198
def default_field
  @default_field
end
exists?() click to toggle source
# File lib/social_snippet/document_backend/yaml_document.rb, line 146
def exists?
  all.exists?
end
field(sym, options = {}) click to toggle source
# File lib/social_snippet/document_backend/yaml_document.rb, line 206
def field(sym, options = {})
  @field_keys ||= ::Set.new
  @default_field ||= ::Hash.new
  @field_type ||= ::Hash.new

  default_field[sym] = options[:default] unless options[:default].nil?

  field_keys.add sym
  field_type[sym] = options[:type] || :String

  # define getter
  define_method sym do
    @fields[sym]
  end

  # define setter
  define_method "#{sym}=" do |v|
    @fields[sym] = v
  end
end
field_keys() click to toggle source
# File lib/social_snippet/document_backend/yaml_document.rb, line 194
def field_keys
  @field_keys
end
field_type() click to toggle source
# File lib/social_snippet/document_backend/yaml_document.rb, line 202
def field_type
  @field_type
end
find(id) click to toggle source
# File lib/social_snippet/document_backend/yaml_document.rb, line 154
def find(id)
  if collection.has_key?(id)
    new collection[id], id
  else
    raise "ERROR: document not found"
  end
end
find_by(cond) click to toggle source
# File lib/social_snippet/document_backend/yaml_document.rb, line 162
def find_by(cond)
  result = collection.select do |item_key|
    item = collection[item_key]
    cond.keys.all? do |key|
      cond[key] === item[key]
    end
  end

  if result.empty?
    raise "ERROR: document not found"
  else
    key, item = result.first
    new item, item[:id]
  end
end
find_or_create_by(cond) click to toggle source
# File lib/social_snippet/document_backend/yaml_document.rb, line 178
def find_or_create_by(cond)
  if where(cond).exists?
    find_by cond
  else
    create cond
  end
end
inherited(child) click to toggle source

“Callback invoked whenever a subclass of the current class is created.” docs.ruby-lang.org/en/2.2.0/Class.html#method-i-inherited

# File lib/social_snippet/document_backend/yaml_document.rb, line 108
def inherited(child)
  load_file!
end
load_file!() click to toggle source
# File lib/social_snippet/document_backend/yaml_document.rb, line 120
def load_file!
  $yaml_document_hash = ::YAML.load(::File.read path) unless path.nil?
end
new(options = {}, new_id = nil) click to toggle source
# File lib/social_snippet/document_backend/yaml_document.rb, line 16
def initialize(options = {}, new_id = nil)
  @path   = @@path
  @fields = ::Hash.new
  init_fields options
  @id ||= new_id || self.class.uuid
end
path() click to toggle source
# File lib/social_snippet/document_backend/yaml_document.rb, line 129
def path
  @@path
end
reset_yaml_document_hash!() click to toggle source
# File lib/social_snippet/document_backend/yaml_document.rb, line 116
def reset_yaml_document_hash!
  $yaml_document_hash = nil
end
set_path(new_path) click to toggle source
# File lib/social_snippet/document_backend/yaml_document.rb, line 124
def set_path(new_path)
  @@path = new_path
  ::FileUtils.touch(path) unless ::File.exists?(path)
end
update_file!() click to toggle source
# File lib/social_snippet/document_backend/yaml_document.rb, line 133
def update_file!
  ::File.write path, yaml_document_hash.to_yaml
  reset_yaml_document_hash!
  load_file!
end
uuid() click to toggle source
# File lib/social_snippet/document_backend/yaml_document.rb, line 227
def uuid
  ::SecureRandom.uuid
end
where(cond) click to toggle source
# File lib/social_snippet/document_backend/yaml_document.rb, line 186
def where(cond)
  Query.new(self, collection).find cond
end
yaml_document_hash() click to toggle source
# File lib/social_snippet/document_backend/yaml_document.rb, line 112
def yaml_document_hash
  $yaml_document_hash ||= ::Hash.new
end

Public Instance Methods

add_to_set(attrs) click to toggle source
# File lib/social_snippet/document_backend/yaml_document.rb, line 77
def add_to_set(attrs)
  attrs.each do |key, value|
    fields[key].push value
    fields[key].uniq!
  end
  save!
end
clone_value(val) click to toggle source
# File lib/social_snippet/document_backend/yaml_document.rb, line 40
def clone_value(val)
  if val.nil?
    nil
  else
    val.clone
  end
end
field_keys() click to toggle source
# File lib/social_snippet/document_backend/yaml_document.rb, line 23
def field_keys
  self.class.field_keys
end
field_type() click to toggle source
# File lib/social_snippet/document_backend/yaml_document.rb, line 27
def field_type
  self.class.field_type
end
init_fields(options = {}) click to toggle source
# File lib/social_snippet/document_backend/yaml_document.rb, line 48
def init_fields(options = {})
  field_keys.each do |k|
    fields[k] = clone_value(self.class.default_field[k])
  end
  options.each do |k, v|
    fields[k] = clone_value(v)
  end
end
pull(attrs) click to toggle source
# File lib/social_snippet/document_backend/yaml_document.rb, line 97
def pull(attrs)
  attrs.each do |key, value|
    fields[key].delete value
  end
  save!
end
push(attrs) click to toggle source
# File lib/social_snippet/document_backend/yaml_document.rb, line 85
def push(attrs)
  attrs.each do |key, value|
    case field_type[key].to_s
    when "Array"
      fields[key].push value
    when "Hash"
      fields[key].merge! value
    end
  end
  save!
end
remove() click to toggle source
# File lib/social_snippet/document_backend/yaml_document.rb, line 57
def remove
  self.class.collection.delete id
  self.class.update_file!
end
save!() click to toggle source

Persistence Methods

# File lib/social_snippet/document_backend/yaml_document.rb, line 72
def save!
  self.class.collection[id] = serialize
  self.class.update_file!
end
serialize() click to toggle source
# File lib/social_snippet/document_backend/yaml_document.rb, line 31
def serialize
  attrs = field_keys.inject(::Hash.new) do |attrs, key|
    attrs[key] = fields[key]
    attrs
  end
  attrs[:id] = id
  attrs
end
update_attributes!(attrs) click to toggle source
# File lib/social_snippet/document_backend/yaml_document.rb, line 62
def update_attributes!(attrs)
  attrs.each do |key, value|
    fields[key] = clone_value(value)
  end
  save!
end