class RemotePartial::YamlStore
Public Class Methods
all()
click to toggle source
# File lib/remote_partial/yaml_store.rb, line 27 def self.all read.keys.collect{|name| find(name)} end
count()
click to toggle source
# File lib/remote_partial/yaml_store.rb, line 31 def self.count read.keys.length end
create(hash)
click to toggle source
# File lib/remote_partial/yaml_store.rb, line 17 def self.create(hash) new(string_keys(hash)).save end
dir()
click to toggle source
# File lib/remote_partial/yaml_store.rb, line 44 def self.dir File.dirname file end
file()
click to toggle source
# File lib/remote_partial/yaml_store.rb, line 35 def self.file @file ||= File.expand_path("#{underscore(name)}.yml", root) end
find(name)
click to toggle source
# File lib/remote_partial/yaml_store.rb, line 21 def self.find(name) name = name.to_s item = read[name] new(item.merge(name: name)) if item end
merge!(hash)
click to toggle source
# File lib/remote_partial/yaml_store.rb, line 48 def self.merge!(hash) write(read.merge(string_keys(hash))) end
read()
click to toggle source
# File lib/remote_partial/yaml_store.rb, line 13 def self.read File.exists?(file) ? YAML.load_file(file) : {} end
root()
click to toggle source
# File lib/remote_partial/yaml_store.rb, line 39 def self.root location = 'db' File.expand_path(location, RemotePartial.root) end
string_keys(hash)
click to toggle source
# File lib/remote_partial/yaml_store.rb, line 52 def self.string_keys(hash) hash.inject({}){|h,(key,value)| h[key.to_s] = value; h} end
write(hash)
click to toggle source
# File lib/remote_partial/yaml_store.rb, line 7 def self.write(hash) ensure_directory_exists output = string_keys(hash).to_yaml File.open(file, 'w+') { |file| file.write(output) } end
Private Class Methods
ensure_directory_exists()
click to toggle source
# File lib/remote_partial/yaml_store.rb, line 71 def self.ensure_directory_exists dir.split(/\//).inject do |path, folder| current_path = [path, folder].join('/') Dir.mkdir(current_path) unless Dir.exists?(current_path) current_path end end
underscore(camel_cased_word)
click to toggle source
# File lib/remote_partial/yaml_store.rb, line 79 def self.underscore(camel_cased_word) word = camel_cased_word.to_s.dup word.gsub!('::', '/') word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2') word.gsub!(/([a-z\d])([A-Z])/,'\1_\2') word.tr!("-", "_") word.downcase! word end
Public Instance Methods
save()
click to toggle source
# File lib/remote_partial/yaml_store.rb, line 56 def save raise "You must define a name before saving" unless name update_time_stamps self.class.merge!({name.to_s => data_stored_in_object}) self end
time_stamps()
click to toggle source
# File lib/remote_partial/yaml_store.rb, line 63 def time_stamps { 'created_at' => created_at, 'updated_at' => updated_at } end
Private Instance Methods
data_stored_in_object()
click to toggle source
# File lib/remote_partial/yaml_store.rb, line 94 def data_stored_in_object data = to_hash data.delete('name') data end
update_time_stamps()
click to toggle source
# File lib/remote_partial/yaml_store.rb, line 89 def update_time_stamps self.created_at = Time.now unless created_at? self.updated_at = Time.now end