Class: Filebase::Drivers::YAML

Inherits:
Object
  • Object
show all
Includes:
Mixin
Defined in:
lib/filebase/drivers/yaml.rb

Instance Method Summary collapse

Methods included from Mixin

#count, #delete, #has_key?, #keys, #path, #root

Constructor Details

#initialize(root) ⇒ YAML

Returns a new instance of YAML.



11
12
13
14
# File 'lib/filebase/drivers/yaml.rb', line 11

def initialize( root )
  super
  @extension = "yml"
end

Instance Method Details

#allObject



16
17
18
19
20
21
22
# File 'lib/filebase/drivers/yaml.rb', line 16

def all
   Dir["#{@root}/*.yml"].map do |file|
    obj = ::YAML.load_file(file) 
    obj['key'] = File.basename(file, '.yml') if obj.is_a? Hash
    obj or nil
   end
end

#find(key) ⇒ Object



24
25
26
27
28
# File 'lib/filebase/drivers/yaml.rb', line 24

def find( key )
	obj = ::YAML.load_file( path( key ) ) rescue nil
	obj['key'] = key if obj.is_a? Hash
	obj or nil # convert false to nil
end

#find_keys(keys) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/filebase/drivers/yaml.rb', line 30

def find_keys(keys)
  keys.map do |key|
    obj = ::YAML.load_file( path( key ) ) rescue nil
		obj['key'] = key if obj.is_a? Hash
		obj or nil # convert false to nil
  end
end

#write(key, object) ⇒ Object



38
39
40
# File 'lib/filebase/drivers/yaml.rb', line 38

def write( key, object )
 object if File.open( path(key), "w" ) { |f| ::YAML.dump(object, f) }
end