Class: Cinch::Storage::YAML

Inherits:
Cinch::Storage show all
Defined in:
lib/cinch/storage/yaml.rb

Overview

A basic storage backed by YAML, using one file per plugin.

Instance Method Summary (collapse)

Constructor Details

- (Storage) initialize(options, plugin)

A new instance of Storage

Parameters:

  • options (Hash)
  • plugin (Plugin)
  • options (Hash)
  • plugin (Plugin)


9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/cinch/storage/yaml.rb', line 9

def initialize(options, plugin)
  # We are a basic example, so we load everything into memory. yey.
  @file = options.basedir + plugin.class.plugin_name + ".yaml"
  if File.exist?(@file)
    @yaml = ::YAML.load_file(@file) || {}
  else
    @yaml = {}
  end
  @options = options

  @mutex = Mutex.new
end

Instance Method Details

- (Object?) [](key)

Parameters:

  • key (Object)
  • key (Object)

Returns:

  • (Object, nil)
  • (Object, nil)


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)

Parameters:

  • key (Object)
  • key (Object)

Returns:

  • (Object, nil)

    The deleted object

  • (Object, nil)

    The deleted object



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

Returns:

  • (self)
  • (self)


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

Returns:

  • (self)
  • (self)


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

Returns:

  • (self)
  • (self)


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

Returns:

  • (self)
  • (self)


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?

Parameters:

  • key (Object)
  • key (Object)

Returns:

  • (Boolean)
  • (Boolean)


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