Class: Cinch::Storage::YAML
- Inherits:
-
Cinch::Storage
- Object
- Cinch::Storage
- Cinch::Storage::YAML
- Defined in:
- lib/cinch/storage/yaml.rb
Overview
A basic storage backed by YAML, using one file per plugin.
Instance Method Summary (collapse)
-
- (Object?) [](key)
-
- (Object) []=(key, value)
-
- (Object?) delete(key)
-
- (self) delete_if
-
- (self) each
-
- (self) each_key
-
- (self) each_value
-
- (Boolean) has_key?(key)
(also: #include?, #key?, #member?)
-
- (Storage) initialize(options, plugin)
constructor
A new instance of Storage.
-
- (Object) save
-
- (Object) unload
Constructor Details
- (Storage) initialize(options, plugin)
A new instance of Storage
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/cinch/storage/yaml.rb', line 9 def initialize(, plugin) # We are a basic example, so we load everything into memory. yey. @file = .basedir + plugin.class.plugin_name + ".yaml" if File.exist?(@file) @yaml = ::YAML.load_file(@file) || {} else @yaml = {} end @options = @mutex = Mutex.new end |
Instance Method Details
- (Object?) [](key)
23 24 25 |
# File 'lib/cinch/storage/yaml.rb', line 23 def [](key) @yaml[key] end |
- (Object) []=(key, value)
28 29 30 |
# File 'lib/cinch/storage/yaml.rb', line 28 def []=(key, value) @yaml[key] = value end |
- (Object?) delete(key)
62 63 64 65 66 |
# File 'lib/cinch/storage/yaml.rb', line 62 def delete(key) obj = @yaml.delete(key) obj end |
- (self) delete_if
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/cinch/storage/yaml.rb', line 69 def delete_if delete_keys = [] each do |key, value| delete_keys << key if yield(key, value) end delete_keys.each do |key| delete(key) end self end |
- (self) each
41 42 43 44 45 |
# File 'lib/cinch/storage/yaml.rb', line 41 def each @yaml.each {|e| yield(e)} self end |
- (self) each_key
48 49 50 51 52 |
# File 'lib/cinch/storage/yaml.rb', line 48 def each_key @yaml.each_key {|e| yield(e)} self end |
- (self) each_value
55 56 57 58 59 |
# File 'lib/cinch/storage/yaml.rb', line 55 def each_value @yaml.each_value {|e| yield(e)} self end |
- (Boolean) has_key?(key) Also known as: include?, key?, member?
33 34 35 |
# File 'lib/cinch/storage/yaml.rb', line 33 def has_key?(key) @yaml.has_key?(key) end |
- (Object) save
83 84 85 86 87 88 89 |
# File 'lib/cinch/storage/yaml.rb', line 83 def save @mutex.synchronize do File.open(@file, "w") do |f| f.write @yaml.to_yaml end end end |
- (Object) unload
92 93 |
# File 'lib/cinch/storage/yaml.rb', line 92 def unload end |