Class: Yamlize
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#initialize(path, type = Hash, &block) ⇒ Yamlize
constructor
A new instance of Yamlize.
- #method_missing(name, *args, &block) ⇒ Object
- #save ⇒ Object
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. 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
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
13 14 15 |
# File 'lib/sup/yamlize.rb', line 13 def attributes @attributes end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
13 14 15 |
# File 'lib/sup/yamlize.rb', line 13 def path @path end |
Instance Method Details
#save ⇒ Object
50 51 52 |
# File 'lib/sup/yamlize.rb', line 50 def save File.open(@path,'w'){|f| YAML.dump @attributes, f} end |