Class: Yamlize

Inherits:
Object show all
Defined in:
lib/sup/yamlize.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, type = Hash, &block) ⇒ Yamlize

Returns a new instance of Yamlize.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sup/yamlize.rb', line 15

def initialize(path, type=Hash, &block)
  @path = File.expand_path path
  File.open(@path,'w'){} unless File.exists?(@path)
  @attributes = YAML.load_file(@path) || type.new
  
  if block_given?
    yield self
    save
  end
rescue Errno::ENOTDIR
  raise "Path invalid."
rescue Errno::ENOENT
  raise "Path invalid."
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sup/yamlize.rb', line 30

def method_missing(name, *args, &block)
  if attribute = name.to_s[/(.*?)=/,1]
    @attributes[attribute] = args.first
    return
  end
  
  begin
    if !@attributes[name.to_s].nil?
      return @attributes[name.to_s]
    end
  rescue TypeError
  end
  
  if @attributes.respond_to?(name)
    return @attributes.send name, *args, &block
  end
  
  super name, *args
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



13
14
15
# File 'lib/sup/yamlize.rb', line 13

def attributes
  @attributes
end

#pathObject (readonly)

Returns the value of attribute path.



13
14
15
# File 'lib/sup/yamlize.rb', line 13

def path
  @path
end

Instance Method Details

#saveObject



50
51
52
# File 'lib/sup/yamlize.rb', line 50

def save
  File.open(@path,'w'){|f| YAML.dump @attributes, f}
end