Class: RemotePartial::YamlStore
- Inherits:
-
Hashie::Mash
- Object
- Hashie::Mash
- RemotePartial::YamlStore
- Defined in:
- lib/remote_partial/yaml_store.rb
Direct Known Subclasses
Class Method Summary collapse
- .all ⇒ Object
- .count ⇒ Object
- .create(hash) ⇒ Object
- .dir ⇒ Object
- .file ⇒ Object
- .find(name) ⇒ Object
- .merge!(hash) ⇒ Object
- .read ⇒ Object
- .root ⇒ Object
- .string_keys(hash) ⇒ Object
- .write(hash) ⇒ Object
Instance Method Summary collapse
Class Method Details
.all ⇒ Object
27 28 29 |
# File 'lib/remote_partial/yaml_store.rb', line 27 def self.all read.keys.collect{|name| find(name)} end |
.count ⇒ Object
31 32 33 |
# File 'lib/remote_partial/yaml_store.rb', line 31 def self.count read.keys.length end |
.create(hash) ⇒ Object
17 18 19 |
# File 'lib/remote_partial/yaml_store.rb', line 17 def self.create(hash) new(string_keys(hash)).save end |
.dir ⇒ Object
44 45 46 |
# File 'lib/remote_partial/yaml_store.rb', line 44 def self.dir File.dirname file end |
.file ⇒ Object
35 36 37 |
# File 'lib/remote_partial/yaml_store.rb', line 35 def self.file @file ||= File.("#{underscore(name)}.yml", root) end |
.find(name) ⇒ Object
21 22 23 24 25 |
# 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) ⇒ Object
48 49 50 |
# File 'lib/remote_partial/yaml_store.rb', line 48 def self.merge!(hash) write(read.merge(string_keys(hash))) end |
.read ⇒ Object
13 14 15 |
# File 'lib/remote_partial/yaml_store.rb', line 13 def self.read File.exists?(file) ? YAML.load_file(file) : {} end |
.root ⇒ Object
39 40 41 42 |
# File 'lib/remote_partial/yaml_store.rb', line 39 def self.root location = 'db' File.(location, RemotePartial.root) end |
.string_keys(hash) ⇒ Object
52 53 54 |
# 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) ⇒ Object
7 8 9 10 11 |
# 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 |
Instance Method Details
#save ⇒ Object
56 57 58 59 60 61 |
# 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 ⇒ Object
63 64 65 66 67 68 |
# File 'lib/remote_partial/yaml_store.rb', line 63 def time_stamps { 'created_at' => created_at, 'updated_at' => updated_at } end |