Class: MethodHash
- Inherits:
-
Hash
- Object
- Hash
- MethodHash
- Defined in:
- lib/methodhash.rb
Instance Attribute Summary collapse
-
#changed ⇒ Object
readonly
Returns the value of attribute changed.
-
#force_calc ⇒ Object
Returns the value of attribute force_calc.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#retry_errors ⇒ Object
Returns the value of attribute retry_errors.
Instance Method Summary collapse
-
#[](*arg) ⇒ Object
— [] — If not previously called with given arguments, calls ‘mymethod’ with the arguments and inserts the answer in the Hash.
-
#[]=(key, value) ⇒ Object
— []= — This method cannot be used.
-
#changed? ⇒ Boolean
— changed? — Has something changed since the last call to dump?.
- #delete(arg) ⇒ Object
-
#dump ⇒ Object
— dump — If a path has been given, writes self to that file and returns the path.
-
#initialize(path = nil) ⇒ MethodHash
constructor
— initialize — A path is optional but necessary if you want to store the Hash to disk.
- #originalassign ⇒ Object
Constructor Details
#initialize(path = nil) ⇒ MethodHash
— initialize — A path is optional but necessary if you want to store the Hash to disk.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/methodhash.rb', line 11 def initialize(path=nil) unless self.respond_to? :mymethod raise ArgumentError, "No calculation method (with name 'mymethod') defined for #{self.class}" end if self.class.to_s == 'MethodHash' raise ArgumentError, "Make a subclass. Don't use MethodHash directly." end @path = path @changed = false @retry_errors = false @force_calc = false if @path and File.exists? @path hash = YAML.load open(@path) raise ArgumentError, "Path holds class #{hash.class}" if hash.class != self.class self.merge! hash end end |
Instance Attribute Details
#changed ⇒ Object (readonly)
Returns the value of attribute changed.
6 7 8 |
# File 'lib/methodhash.rb', line 6 def changed @changed end |
#force_calc ⇒ Object
Returns the value of attribute force_calc.
5 6 7 |
# File 'lib/methodhash.rb', line 5 def force_calc @force_calc end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
6 7 8 |
# File 'lib/methodhash.rb', line 6 def path @path end |
#retry_errors ⇒ Object
Returns the value of attribute retry_errors.
5 6 7 |
# File 'lib/methodhash.rb', line 5 def retry_errors @retry_errors end |
Instance Method Details
#[](*arg) ⇒ Object
— [] — If not previously called with given arguments, calls ‘mymethod’ with the arguments and inserts the answer in the Hash. If force_calc is set to true, mymethod is always called. If retry_errors is set to true, mymethod is also called if value has previously been retrieved but raised an error at that time.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/methodhash.rb', line 36 def [](*arg) arg = arg.first if arg.size==1 if !force_calc and s=super(arg) if s.is_a? String and s[0,6]=='ERROR:' if @retry_errors self.delete arg self.[](*arg) else raise s[7..-1] end else s end else @changed = true begin originalassign(arg,self.mymethod(*arg)) rescue originalassign(arg,"ERROR: #{$!}") raise end end end |
#[]=(key, value) ⇒ Object
— []= — This method cannot be used.
62 63 64 |
# File 'lib/methodhash.rb', line 62 def []=(key, value) raise "You can't just put values into a #{self.class}." end |
#changed? ⇒ Boolean
— changed? — Has something changed since the last call to dump?
89 90 91 |
# File 'lib/methodhash.rb', line 89 def changed? @changed end |
#delete(arg) ⇒ Object
82 83 84 85 |
# File 'lib/methodhash.rb', line 82 def delete(arg) @changed = true super end |
#dump ⇒ Object
— dump — If a path has been given, writes self to that file and returns the path. Otherwise, returns a string containing the dump.
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/methodhash.rb', line 69 def dump if @path if self.changed? open(@path,'w'){|f| YAML.dump(self,f) } @changed = false end @path elsif !path @changed = false YAML.dump(self) end end |
#originalassign ⇒ Object
7 |
# File 'lib/methodhash.rb', line 7 alias_method :originalassign, :[]= |