cinch-storage 1.0.3 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -26,6 +26,10 @@ Or install it yourself as:
26
26
 
27
27
  ## Usage
28
28
 
29
+ First, require it.
30
+
31
+ require 'cinch/storage'
32
+
29
33
  In your plugin you will need to spin up a Storage object as part of your init.
30
34
 
31
35
  def initialize(*args)
@@ -42,14 +46,10 @@ data collision.
42
46
  @storage.save
43
47
  end
44
48
 
45
- ## This is Horrible Code!
46
-
47
- The module is very fragile at the moment as a result of being written in like 20 minutes
48
- or something as a stopgap.
49
+ ## Think this is terrible?
49
50
 
50
- However, since it doesn't look like Cinch::Storage will be happening very soon I will
51
- be going back through and adding tests and error handling and proper debugging and a
52
- little tighter integration with Cinch.
51
+ I wouldn't disagree; this is my first time writing anyting remotely
52
+ ORM-like. Please feel free to fork and submit pulls to increase sanity.
53
53
 
54
54
  ## Contributing
55
55
 
@@ -4,23 +4,25 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'cinch/storage/version'
5
5
 
6
6
  Gem::Specification.new do |gem|
7
- gem.name = "cinch-storage"
7
+ gem.name = 'cinch-storage'
8
8
  gem.version = Cinch::Storage::VERSION
9
- gem.authors = ["Brian Haberer"]
10
- gem.email = ["bhaberer@gmail.com"]
11
- gem.description = %q{Simle YAML backed Storage solution for Cinch plugins.}
12
- gem.summary = %q{Simle YAML backed Storage solution for Cinch plugins.}
13
- gem.homepage = "https://github.com/bhaberer/cinch-storage"
9
+ gem.authors = ['Brian Haberer']
10
+ gem.email = ['bhaberer@gmail.com']
11
+ gem.description = %q{Simple YAML backed Storage solution for Cinch plugins.}
12
+ gem.summary = %q{Data Storage for Cinch plugins.}
13
+ gem.homepage = 'https://github.com/bhaberer/cinch-storage'
14
+ gem.license = 'MIT'
14
15
 
15
16
  gem.files = `git ls-files`.split($/)
16
17
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
18
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
- gem.require_paths = ["lib"]
19
+ gem.require_paths = ['lib']
19
20
 
20
21
  gem.add_development_dependency 'rake'
21
22
  gem.add_development_dependency 'rspec'
22
23
  gem.add_development_dependency 'coveralls'
23
24
  gem.add_development_dependency 'cinch-test'
24
25
 
26
+ gem.add_dependency 'cinch', '~> 2.0.12'
25
27
  gem.add_dependency 'psych', '~> 1.3.4'
26
28
  end
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'cinch/storage/version'
3
+ require 'psych'
4
+
5
+ module Cinch
6
+ # Addon for Cinch to allow for simple YAML storage of data
7
+ class Storage
8
+ attr_accessor :filename, :data
9
+
10
+ def initialize(file, init = {})
11
+ @filename = file
12
+ @data = Psych.load(File.open(@filename)) if File.exist?(@filename)
13
+ @data ||= init
14
+ end
15
+
16
+ def save
17
+ File.open(@filename, 'w') do |file|
18
+ Psych.dump(@data, file)
19
+ end
20
+ end
21
+
22
+ def synced_save(bot = @bot)
23
+ bot.synchronize(@filename) do
24
+ save
25
+ end
26
+ end
27
+ end
28
+ end
@@ -1,5 +1,7 @@
1
+ # -*- encoding: utf-8 -*-
1
2
  module Cinch
2
- module Storage
3
- VERSION = "1.0.3"
3
+ # versioning info
4
+ class Storage
5
+ VERSION = '1.1.0'
4
6
  end
5
7
  end
@@ -1,43 +1,58 @@
1
1
  require 'spec_helper'
2
2
 
3
+ Thing = Struct.new(:foo)
4
+
3
5
  class MyPlugin
4
6
  include Cinch::Plugin
5
7
 
6
8
  def initialize(*args)
7
9
  super
8
- @storage = CinchStorage.new(config[:filename] || '/dev/null', [])
10
+ @storage = Cinch::Storage.new(config[:filename] || '/dev/null', [])
9
11
  end
10
12
 
11
- match /store (.*)/
12
13
 
13
- def execute(m, thing)
14
- @storage.data << thing
14
+ match /store (.*)/, method: :store_text
15
+ match /storething (.*)/, method: :store_object
16
+
17
+ def store_text(m, text)
18
+ @storage.data << text
19
+ @storage.synced_save(@bot)
20
+ end
21
+
22
+ def store_object(m, text)
23
+ @storage.data << Thing.new(text)
15
24
  @storage.synced_save(@bot)
16
25
  end
17
26
  end
18
27
 
19
28
 
20
- describe CinchStorage do
29
+ describe Cinch::Storage do
21
30
  include Cinch::Test
22
31
 
23
32
  before(:each) do
24
33
  @filename = '/tmp/storage_test.tmp'
25
34
  File.delete(@filename) if File.exist?(@filename)
26
- @bot = make_bot(MyPlugin, { :filename => @filename })
35
+ @bot = make_bot(MyPlugin, { filename: @filename })
27
36
  end
28
37
 
29
38
  it 'should allow users to store information between bot runs' do
30
- storage = CinchStorage.new(@filename, [])
39
+ storage = Cinch::Storage.new(@filename, [])
31
40
  storage.data << 'foo'
32
41
  storage.save
33
42
 
34
- CinchStorage.new(@filename).data.
35
- should include 'foo'
43
+ Cinch::Storage.new(@filename).data
44
+ .should include 'foo'
36
45
  end
37
46
 
38
47
  it 'should handle multiple bot threads attempting to write to the same file' do
39
- 10.times { |x| get_replies(make_message(@bot, "!store #{x}"))}
40
- CinchStorage.new(@filename).data.length.
41
- should == 10
48
+ 10.times { |x| get_replies(make_message(@bot, "!store #{x}")) }
49
+ Cinch::Storage.new(@filename).data.length
50
+ .should == 10
51
+ end
52
+
53
+ it 'should write object data to the disk' do
54
+ get_replies(make_message(@bot, "!storething foobar"))
55
+ Cinch::Storage.new(@filename).data.first
56
+ .should == Thing.new('foobar')
42
57
  end
43
58
  end
@@ -1,4 +1,4 @@
1
1
  require 'coveralls'
2
2
  Coveralls.wear!
3
- require 'cinch-storage'
3
+ require 'cinch/storage'
4
4
  require 'cinch/test'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cinch-storage
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-21 00:00:00.000000000 Z
12
+ date: 2014-02-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -75,6 +75,22 @@ dependencies:
75
75
  - - ! '>='
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: cinch
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 2.0.12
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 2.0.12
78
94
  - !ruby/object:Gem::Dependency
79
95
  name: psych
80
96
  requirement: !ruby/object:Gem::Requirement
@@ -91,7 +107,7 @@ dependencies:
91
107
  - - ~>
92
108
  - !ruby/object:Gem::Version
93
109
  version: 1.3.4
94
- description: Simle YAML backed Storage solution for Cinch plugins.
110
+ description: Simple YAML backed Storage solution for Cinch plugins.
95
111
  email:
96
112
  - bhaberer@gmail.com
97
113
  executables: []
@@ -105,12 +121,13 @@ files:
105
121
  - README.md
106
122
  - Rakefile
107
123
  - cinch-storage.gemspec
108
- - lib/cinch-storage.rb
124
+ - lib/cinch/storage.rb
109
125
  - lib/cinch/storage/version.rb
110
126
  - spec/cinch-storage_spec.rb
111
127
  - spec/spec_helper.rb
112
128
  homepage: https://github.com/bhaberer/cinch-storage
113
- licenses: []
129
+ licenses:
130
+ - MIT
114
131
  post_install_message:
115
132
  rdoc_options: []
116
133
  require_paths:
@@ -132,7 +149,7 @@ rubyforge_project:
132
149
  rubygems_version: 1.8.25
133
150
  signing_key:
134
151
  specification_version: 3
135
- summary: Simle YAML backed Storage solution for Cinch plugins.
152
+ summary: Data Storage for Cinch plugins.
136
153
  test_files:
137
154
  - spec/cinch-storage_spec.rb
138
155
  - spec/spec_helper.rb
@@ -1,25 +0,0 @@
1
- require 'cinch/storage/version'
2
- require 'psych'
3
-
4
- class CinchStorage
5
- attr_accessor :filename, :data
6
-
7
- def initialize(file, init = Hash.new)
8
- @filename = file
9
- @data = Psych::load(File.open(@filename)) if File::exist?(@filename)
10
- @data ||= init
11
- end
12
-
13
- def save
14
- File.open(@filename, 'w') do |file|
15
- Psych::dump(@data, file)
16
- end
17
- end
18
-
19
- def synced_save(bot = @bot)
20
- bot.synchronize(@filename) do
21
- save
22
- end
23
- end
24
- end
25
-