Class: MonitoredItems::Store
- Inherits:
-
Object
- Object
- MonitoredItems::Store
- Defined in:
- lib/FileMonitor/store.rb
Overview
A Store object is much like a hash but instead of getting an setting keys you get and set instance variables. The following examples are using methods used by the FileMonitor object, however the store object is not limited to these methods.
Example:
s = MonitoredItems::Store.new
s.path = '/tmp' # => "/tmp"
s.callback Proc.new {'Hello World'} # => #<Proc:0x0000000100317508@(irb):18>
s.callback.call # => "Hello World"
s.path # => "/tmp"
# OR send a hash when initializing the store
i = MonitoredItems::Store.new({:path => '/tmp'})
i.path #=> "/tmp"
Instance Method Summary collapse
-
#initialize(hsh = {}) ⇒ Store
constructor
Supports initialization with a hash of methods & values.
-
#method_missing(mth, arg = Nothing) ⇒ Object
Gets or sets instance variables based upon the methods called.
-
#to_h ⇒ Object
Return Hash representation of Store object.
-
#to_s ⇒ Object
Return inspection of hash representation of Store object.
Constructor Details
#initialize(hsh = {}) ⇒ Store
Supports initialization with a hash of methods & values. It makes no difference if the keys of the hash are strings or symbols, but they are case sensitive.
22 23 24 |
# File 'lib/FileMonitor/store.rb', line 22 def initialize(hsh = {}) hsh.map {|k,v| self.send(k, v)} if Hash === hsh end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(mth, arg = Nothing) ⇒ Object
Gets or sets instance variables based upon the methods called.
27 28 29 30 31 32 |
# File 'lib/FileMonitor/store.rb', line 27 def method_missing(mth, arg=Nothing) # append the @ symbol and remove the equal symbol (if exists): mth = "@#{mth}".chomp('=').to_sym # get or set instnace variable arg == Nothing ? self.instance_variable_get(mth) : self.instance_variable_set(mth, arg) end |
Instance Method Details
#to_h ⇒ Object
Return Hash representation of Store object
35 36 37 |
# File 'lib/FileMonitor/store.rb', line 35 def to_h Hash[*self.instance_variables.collect {|m| [m.slice(1..-1).to_sym, self.instance_variable_get(m)] }.flatten] end |
#to_s ⇒ Object
Return inspection of hash representation of Store object
40 41 42 |
# File 'lib/FileMonitor/store.rb', line 40 def to_s self.to_h.inspect end |