Class: Secretary
- Inherits:
-
Object
- Object
- Secretary
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/secretary.rb,
lib/secretary.rb,
lib/secretary/error.rb,
lib/secretary/gopher.rb
Overview
If you subclass the Secretary class to be able to store a different kind of data, like, say, an Array instead of a Hash, then you’ll need to override the contents and to_hash methods. (In addition, you may want to use a different kind of gopher too. See the documentation on Secretary::Gopher for info about that.)
Defined Under Namespace
Modules: Error Classes: Gopher
Constant Summary collapse
- VERSION =
'0.1.0'
Instance Attribute Summary collapse
-
#defaults ⇒ Object
Returns the value of attribute defaults.
-
#file_path ⇒ Object
Returns the value of attribute file_path.
-
#gopher ⇒ Object
readonly
Returns the value of attribute gopher.
Instance Method Summary collapse
-
#==(other) ⇒ Object
A Secretary is equal to another object if the other object responds to :file_path, :defaults, :gopher, and :contents, and if the objects’ values of those corresponding methods are equal to each other.
-
#contents ⇒ Object
Returns a new object containing the Secretary’s data that she originally got from her file.
-
#initialize(file_path, options = {}) ⇒ Secretary
constructor
You can give an options hash.
-
#reload! ⇒ Object
Reloads data from the Secretary’s file.
-
#save ⇒ Object
Saves its contents to its file path.
- #to_hash ⇒ Object
Constructor Details
#initialize(file_path, options = {}) ⇒ Secretary
You can give an options hash. Two options are eaten by the Secretary intializer (the rest are passed into the gopher initializer). They are :defaults, which is a Hash containing the default data, and :gopher, which is a Gopher class from which the Secretary’s gopher will be created. She will raise an ArgumentError if you give a gopher class object that does not respond to :new, or if the newly initialized gopher object cannot act like a Gopher (i.e. if it cannot respond to :load and :save). If she finds a file at the file_path you give her, then she’ll combine the data that her new gopher loads from the file with the defaults that you gave her to get her contents. If no file is found at the given file_path, then just the defaults are put into the contents.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/secretary.rb', line 44 def initialize(file_path, ={}) defaults = .delete(:defaults) || {} gopher_class = .delete(:gopher) || Gopher::YAML unless gopher_class.respond_to? :new raise ArgumentError, "given gopher class is not a class (#{gopher_class})" end @file_path = file_path @defaults = defaults @gopher = gopher_class.new unless @gopher.respond_to? :load raise ArgumentError, "given gopher class\'s members do not respond to " + ":load (#{gopher})" end unless @gopher.respond_to? :save raise ArgumentError, "given gopher class\'s members do not respond to " + ":save (#{gopher})" end @contents = {} reload! end |
Instance Attribute Details
#defaults ⇒ Object
Returns the value of attribute defaults.
27 28 29 |
# File 'lib/secretary.rb', line 27 def defaults @defaults end |
#file_path ⇒ Object
Returns the value of attribute file_path.
27 28 29 |
# File 'lib/secretary.rb', line 27 def file_path @file_path end |
#gopher ⇒ Object (readonly)
Returns the value of attribute gopher.
28 29 30 |
# File 'lib/secretary.rb', line 28 def gopher @gopher end |
Instance Method Details
#==(other) ⇒ Object
A Secretary is equal to another object if the other object responds to :file_path, :defaults, :gopher, and :contents, and if the objects’ values of those corresponding methods are equal to each other.
72 73 74 75 76 77 |
# File 'lib/secretary.rb', line 72 def ==(other) other.respond_to? :file_path and other.respond_to? :defaults \ and other.respond_to? :gopher and other.respond_to? :contents \ and file_path == other.file_path and defaults == other.defaults \ and gopher == other.gopher and contents == other.contents end |
#contents ⇒ Object
Returns a new object containing the Secretary’s data that she originally got from her file. It consists of the her defaults overridden by her file’s data. Note that this method returns a new object, which means that it is only meant for inspection; modifying the object it returns will not affect her data. If you want to modify her data, directly tell her to use the []= or delete methods.
85 86 87 |
# File 'lib/secretary.rb', line 85 def contents defaults.merge @contents end |
#reload! ⇒ Object
Reloads data from the Secretary’s file. If she succeeds, then this method returns true. If she can’t find a file at her file_path, then this method returns false.
96 97 98 99 100 101 102 103 104 |
# File 'lib/secretary.rb', line 96 def reload! begin loaded_data = gopher.load(file_path) @contents.merge! loaded_data return true rescue Error::MissingFile return false end end |
#save ⇒ Object
Saves its contents to its file path.
107 108 109 |
# File 'lib/secretary.rb', line 107 def save gopher.save self.to_hash, file_path end |
#to_hash ⇒ Object
89 90 91 |
# File 'lib/secretary.rb', line 89 def to_hash contents end |