Class: RemotePartial::YamlStore

Inherits:
Hashie::Mash
  • Object
show all
Defined in:
lib/remote_partial/yaml_store.rb

Direct Known Subclasses

Partial

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.allObject



27
28
29
# File 'lib/remote_partial/yaml_store.rb', line 27

def self.all
  read.keys.collect{|name| find(name)}
end

.countObject



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

.dirObject



44
45
46
# File 'lib/remote_partial/yaml_store.rb', line 44

def self.dir
  File.dirname file
end

.fileObject



35
36
37
# File 'lib/remote_partial/yaml_store.rb', line 35

def self.file
  @file ||= File.expand_path("#{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

.readObject



13
14
15
# File 'lib/remote_partial/yaml_store.rb', line 13

def self.read
  File.exists?(file) ? YAML.load_file(file) : {}
end

.rootObject



39
40
41
42
# File 'lib/remote_partial/yaml_store.rb', line 39

def self.root
  location = 'db'
  File.expand_path(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

#saveObject



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_stampsObject



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