rico 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rico.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Jason Coene
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,153 @@
1
+ # Rico
2
+
3
+ Rico provides primative data types on Riak.
4
+
5
+ ## Installation
6
+
7
+ Add rico to your Gemfile and `bundle install`:
8
+
9
+ ```ruby
10
+ gem "rico"
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ Instantiate a Rico object with a **bucket** and a **key** then perform operations.
16
+
17
+ Here's an example of how to use a set to manage a list of followed users:
18
+
19
+ ```ruby
20
+ follows = Rico::Set.new "follows", @user.id
21
+
22
+ follows.member? @other_user.id # => false
23
+
24
+ follows.add @other_user.id
25
+
26
+ follows.member? @other_user.id # => true
27
+ follows.length # => 1
28
+
29
+ follows.remove @other_user.id
30
+
31
+ follows.member? @other_user.id # => false
32
+ follows.length # => 0
33
+ ```
34
+
35
+ ## Configuration
36
+
37
+ By default, Rico uses a generic Riak::Client instance for operations. You can specify your own (perhaps inside of a rails initializer) like so:
38
+
39
+ ```ruby
40
+ Rico.configure do |c|
41
+ c.riak = Riak::Client.new(http_port: 1234, ...)
42
+ end
43
+ ```
44
+
45
+ You can also provide a namespace to be used as a key prefix:
46
+
47
+ ```ruby
48
+ Rico.configure do |c|
49
+ c.namespace = "development" # => "development:BUCKET:KEY"
50
+ c.namespace = ["my_app", "production"] # => "my_app:production:BUCKET:KEY"
51
+ end
52
+ ```
53
+
54
+ ## Data Types
55
+
56
+ **Arrays** - sequence of values
57
+
58
+ ```ruby
59
+ a = Rico::Array.new "bucket", "key"
60
+ a.add [3, 1, 1, 4, 2]
61
+ a.members # => [3, 1, 1, 4, 2]
62
+ a.length # => 5
63
+ ```
64
+
65
+ **Lists** - sorted sequence of values
66
+
67
+ ```ruby
68
+ l = Rico::List.new "bucket", "key"
69
+ l.add [3, 1, 1, 4, 2]
70
+ l.members # => [1, 1, 2, 3, 4]
71
+ l.length # => 5
72
+ ```
73
+
74
+ **Sets** - unique sequence of values
75
+
76
+ ```ruby
77
+ s = Rico::Set.new "bucket", "key"
78
+ s.add [3, 1, 1, 4, 2]
79
+ s.members # => [3, 1, 4, 2]
80
+ s.length # => 4
81
+ ```
82
+
83
+ **Sorted Sets** - unique, sorted sequence of values
84
+
85
+ ```ruby
86
+ s = Rico::SortedSet.new "bucket", "key"
87
+ s.add [3, 1, 1, 4, 2]
88
+ s.members # => [1, 2, 3, 4]
89
+ s.length # => 4
90
+ ```
91
+
92
+ **Values** - generic serialized values
93
+
94
+ ```ruby
95
+ v = Rico::Value.new "bucket", "key"
96
+ v.exists? # => false
97
+ v.get # => nil
98
+ v.set "bob"
99
+ v.get # => "bob"
100
+ v.exists? # => true
101
+ ```
102
+
103
+ ## Notes
104
+
105
+ ### Enumerable
106
+
107
+ Enumerable-looking types are indeed Enumerable
108
+
109
+ ### Serialization
110
+
111
+ Data is serialized using the Riak client libary provided by Basho, which serializes values as JSON and sets a Content-Type value of "application/json"
112
+
113
+ ### Persistence
114
+
115
+ Data is persisted at operation time. For example, List#add(5) will immediately update the record in Riak. It'd generally be wise to compute a list of values to be added or removed and then issue a single operation.
116
+
117
+ ## TODO
118
+
119
+ - Automatic sibling resolution for simple types
120
+ - Ability to provide sibling resolution callback
121
+
122
+ ## Contributing
123
+
124
+ 1. Fork it
125
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
126
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
127
+ 4. Push to the branch (`git push origin my-new-feature`)
128
+ 5. Create new Pull Request
129
+
130
+ ## License
131
+
132
+ Copyright (c) 2012 Jason Coene
133
+
134
+ MIT License
135
+
136
+ Permission is hereby granted, free of charge, to any person obtaining
137
+ a copy of this software and associated documentation files (the
138
+ "Software"), to deal in the Software without restriction, including
139
+ without limitation the rights to use, copy, modify, merge, publish,
140
+ distribute, sublicense, and/or sell copies of the Software, and to
141
+ permit persons to whom the Software is furnished to do so, subject to
142
+ the following conditions:
143
+
144
+ The above copyright notice and this permission notice shall be
145
+ included in all copies or substantial portions of the Software.
146
+
147
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
148
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
149
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
150
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
151
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
152
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
153
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env rake
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: "spec"
data/lib/rico/array.rb ADDED
@@ -0,0 +1,63 @@
1
+ module Rico
2
+ class Array
3
+ include Rico::Object
4
+ include Enumerable
5
+ extend Forwardable
6
+
7
+ def_delegators :members, :each, :[]
8
+
9
+ public
10
+
11
+ # Adds the requested items to the array and stores the object
12
+ #
13
+ # items - items to be added to the array
14
+ #
15
+ # Returns the result of the store operation
16
+ def add(*items)
17
+ mutate compute_add(items)
18
+ end
19
+
20
+ # Removes the requested items from the array and stores the object
21
+ #
22
+ # items - items to be removed from the array
23
+ #
24
+ # Returns the result of the store operation
25
+ def remove(*items)
26
+ mutate compute_remove(items)
27
+ end
28
+
29
+ # Obtains the items in the array
30
+ #
31
+ # Returns the data in the object as an array
32
+ def members
33
+ Array(data)
34
+ end
35
+
36
+ # Tests whether or not an item exists in the array
37
+ #
38
+ # item - item to test against
39
+ #
40
+ # Returns true or false
41
+ def member?(item)
42
+ members.include? item
43
+ end
44
+
45
+ # Returns the number of items in the array
46
+ #
47
+ # Returns an Integer
48
+ def length
49
+ members.length
50
+ end
51
+ alias_method :count, :length
52
+
53
+ protected
54
+
55
+ def compute_add(items)
56
+ members + items
57
+ end
58
+
59
+ def compute_remove(items)
60
+ members - items
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,9 @@
1
+ module Rico
2
+ class Counter
3
+ include Rico::Object
4
+
5
+ def initialize(bucket, key)
6
+ raise "DO YOU THINK THIS IS A MOTHERFUCKIGN GAME?"
7
+ end
8
+ end
9
+ end
data/lib/rico/list.rb ADDED
@@ -0,0 +1,13 @@
1
+ module Rico
2
+ class List < Array
3
+ protected
4
+
5
+ def compute_add(items)
6
+ super(items).sort
7
+ end
8
+
9
+ def compute_remove(items)
10
+ super(items).sort
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,45 @@
1
+ module Rico
2
+ module Object
3
+ extend Forwardable
4
+
5
+ def_delegators :riak_object, :store, :delete
6
+
7
+ # Initialize an object with a bucket and key
8
+ #
9
+ # bucket - the name of the bucket (not prefixed by a namespace)
10
+ # key - the name of the key
11
+ #
12
+ # Returns nothing
13
+ def initialize(bucket, key)
14
+ @bucket, @key = bucket, key
15
+ end
16
+
17
+ def data
18
+ @data ||= riak_object.data
19
+ end
20
+
21
+ # Sets a new value on the object and stores it
22
+ #
23
+ # value - new value to set
24
+ #
25
+ # Returns the result of the store operation
26
+ def mutate(value)
27
+ @data = value
28
+ riak_object.data = value
29
+ riak_object.store
30
+ end
31
+
32
+ # Determine whether an object exists or not
33
+ #
34
+ # Returns true or false
35
+ def exists?
36
+ Rico.bucket(@bucket).exists? @key
37
+ end
38
+
39
+ protected
40
+
41
+ def riak_object
42
+ @riak_object ||= Rico.bucket(@bucket).get_or_new @key
43
+ end
44
+ end
45
+ end
data/lib/rico/set.rb ADDED
@@ -0,0 +1,13 @@
1
+ module Rico
2
+ class Set < Array
3
+ protected
4
+
5
+ def compute_add(items)
6
+ super(items).uniq
7
+ end
8
+
9
+ def compute_remove(items)
10
+ super(items).uniq
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module Rico
2
+ class SortedSet < Set
3
+ protected
4
+
5
+ def compute_add(items)
6
+ super(items).sort
7
+ end
8
+
9
+ def compute_remove(items)
10
+ super(items).sort
11
+ end
12
+ end
13
+ end
data/lib/rico/value.rb ADDED
@@ -0,0 +1,31 @@
1
+ module Rico
2
+ class Value
3
+ include Rico::Object
4
+
5
+ # Gets the value of the object
6
+ #
7
+ # Returns the deserialized value
8
+ alias_method :get, :data
9
+
10
+ # Sets and stores the new value for the object
11
+ #
12
+ # value - the new value to store
13
+ #
14
+ # Returns the result of the store operation
15
+ alias_method :set, :mutate
16
+
17
+ # Sets the value if it does not exist
18
+ #
19
+ # value - the value to store
20
+ #
21
+ # Returns true if stored, false if not
22
+ def setnx(value)
23
+ if redis_object.exists?
24
+ false
25
+ else
26
+ set value
27
+ true
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module Rico
2
+ VERSION = "0.0.1"
3
+ end
data/lib/rico.rb ADDED
@@ -0,0 +1,40 @@
1
+ require "riak"
2
+
3
+ require "rico/object"
4
+
5
+ require "rico/array"
6
+ require "rico/list"
7
+ require "rico/set"
8
+ require "rico/sorted_set"
9
+ require "rico/value"
10
+
11
+ require "rico/version"
12
+
13
+ module Rico
14
+
15
+ def self.configure
16
+ yield self if block_given?
17
+ end
18
+
19
+ def self.bucket(key)
20
+ namespaced_key = [@namespace, key].flatten.select(&:present?).join(":")
21
+ @bucket_cache ||= {}
22
+ @bucket_cache[namespaced_key] ||= riak.bucket(namespaced_key)
23
+ end
24
+
25
+ def self.namespace
26
+ @namespace
27
+ end
28
+
29
+ def self.namespace=(namespace)
30
+ @namespace = namespace
31
+ end
32
+
33
+ def self.riak
34
+ @riak ||= Riak::Client.new
35
+ end
36
+
37
+ def self.riak=(riak)
38
+ @riak = riak
39
+ end
40
+ end
data/rico.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/rico/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Jason Coene"]
6
+ gem.email = ["jcoene@gmail.com"]
7
+ gem.description = "Primative data types on Riak"
8
+ gem.summary = gem.description
9
+ gem.homepage = "https://github.com/jcoene/rico"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "rico"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Rico::VERSION
17
+
18
+ gem.add_dependency "riak-client", "~> 1.1"
19
+ gem.add_development_dependency "rspec", "~> 2.12"
20
+ end
@@ -0,0 +1,144 @@
1
+ require "spec_helper"
2
+
3
+ describe Rico::Array do
4
+ before :each do
5
+ RiakHelpers.reset!
6
+ end
7
+
8
+ describe "#add" do
9
+ it "writes values to an array" do
10
+ a = Rico::Array.new RiakHelpers.bucket, "add_writes_values"
11
+ a.add(1, 2, 3)
12
+ b = Rico::Array.new RiakHelpers.bucket, "add_writes_values"
13
+ b.members.should eql [1, 2, 3]
14
+ end
15
+
16
+ it "allows duplicate values" do
17
+ a = Rico::Array.new RiakHelpers.bucket, "add_allows_duplicates"
18
+ a.add(1, 2, 3)
19
+ a.add(2, 3, 4)
20
+ b = Rico::Array.new RiakHelpers.bucket, "add_allows_duplicates"
21
+ b.members.should eql [1, 2, 3, 2, 3, 4]
22
+ end
23
+
24
+ it "retains order of addition" do
25
+ a = Rico::Array.new RiakHelpers.bucket, "add_retains_order"
26
+ a.add(5, 3, 1, 6, 7)
27
+ b = Rico::Array.new RiakHelpers.bucket, "add_retains_order"
28
+ b.members.should eql [5, 3, 1, 6, 7]
29
+ end
30
+
31
+ it "works with strings" do
32
+ a = Rico::Array.new RiakHelpers.bucket, "add_works_with_strings"
33
+ a.add("john", "joe", "jason")
34
+ a.add("brian", "bob")
35
+ b = Rico::Array.new RiakHelpers.bucket, "add_works_with_strings"
36
+ b.members.should eql ["john", "joe", "jason", "brian", "bob"]
37
+ end
38
+
39
+ it "works with arrays" do
40
+ a = Rico::Array.new RiakHelpers.bucket, "add_works_with_arrays"
41
+ a.add([1,2,3], [4,5,6], [7,8,9])
42
+ a.add(["a", "b", 37.2])
43
+ b = Rico::Array.new RiakHelpers.bucket, "add_works_with_arrays"
44
+ b.members.should eql [[1,2,3], [4,5,6], [7,8,9], ["a", "b", 37.2]]
45
+ end
46
+
47
+ it "works with hashmaps" do
48
+ a = Rico::Array.new RiakHelpers.bucket, "add_works_with_hashmaps"
49
+ a.add({a: 1, b: 2}, {charlie: 6})
50
+ a.add({"usd" => 37.2, "eur" => 31.6})
51
+ b = Rico::Array.new RiakHelpers.bucket, "add_works_with_hashmaps"
52
+ b.members.should eql [{"a" => 1, "b" => 2}, {"charlie" => 6}, {"usd" => 37.2, "eur" => 31.6}]
53
+ end
54
+
55
+ it "works with mixed types" do
56
+ a = Rico::Array.new RiakHelpers.bucket, "add_works_with_mixed_types"
57
+ a.add({"usd" => 123.41, "cad" => 61.89})
58
+ a.add("Bears", "Beets", "Battlestar Galactica")
59
+ a.add(3.14159)
60
+ a.add(71)
61
+ b = Rico::Array.new RiakHelpers.bucket, "add_works_with_mixed_types"
62
+ b.members.should eql [{"usd" => 123.41, "cad" => 61.89}, "Bears", "Beets", "Battlestar Galactica", 3.14159, 71]
63
+ end
64
+ end
65
+
66
+ describe "#remove" do
67
+ it "removes existing values" do
68
+ a = Rico::Array.new RiakHelpers.bucket, "remove_removes"
69
+ a.add({"usd" => 123.41, "cad" => 61.89}, "Bears", "Beets", :slumdog, "Battlestar Galactica", 3.14159, 71)
70
+ b = Rico::Array.new RiakHelpers.bucket, "remove_removes"
71
+ b.remove(3.14159, "Beets")
72
+ b.remove("slumdog")
73
+ c = Rico::Array.new RiakHelpers.bucket, "remove_removes"
74
+ c.members.should eql [{"usd" => 123.41, "cad" => 61.89}, "Bears", "Battlestar Galactica", 71]
75
+ end
76
+
77
+ it "does not throw on removal on non-key" do
78
+ lambda do
79
+ Rico::Array.new(RiakHelpers.bucket, "remove_nonkey").remove("bill")
80
+ end.should_not raise_error
81
+ end
82
+
83
+ it "does not throw on removal on non-value" do
84
+ lambda do
85
+ a = Rico::Array.new RiakHelpers.bucket, "remove_nonvalue"
86
+ a.add 37, 68, 54
87
+ a.remove "josh"
88
+ end.should_not raise_error
89
+ end
90
+ end
91
+
92
+ describe "#members" do
93
+ it "returns a list of members" do
94
+ a = Rico::Array.new RiakHelpers.bucket, "members_lists"
95
+ a.add({"usd" => 123.41, "cad" => 61.89}, "Bears", "Beets", "Battlestar Galactica", 3.14159, 71)
96
+ b = Rico::Array.new RiakHelpers.bucket, "members_lists"
97
+ b.members.should eql [{"usd" => 123.41, "cad" => 61.89}, "Bears", "Beets", "Battlestar Galactica", 3.14159, 71]
98
+ end
99
+ end
100
+
101
+ describe "#member?" do
102
+ it "returns a true if a member" do
103
+ a = Rico::Array.new RiakHelpers.bucket, "members_lists"
104
+ a.add({"usd" => 123.41, "cad" => 61.89}, "Bears", "Beets", "Battlestar Galactica", 3.14159, 71)
105
+ b = Rico::Array.new RiakHelpers.bucket, "members_lists"
106
+ b.member?({"usd" => 123.41, "cad" => 61.89}).should eql true
107
+ end
108
+
109
+ it "returns a true if not a member" do
110
+ a = Rico::Array.new RiakHelpers.bucket, "members_lists"
111
+ a.add({"usd" => 123.41, "cad" => 61.89}, "Bears", "Beets", "Battlestar Galactica", 3.14159, 71)
112
+ b = Rico::Array.new RiakHelpers.bucket, "members_lists"
113
+ b.member?({"usd" => 123.42, "cad" => 61.89}).should eql false
114
+ end
115
+ end
116
+
117
+ describe "#length" do
118
+ it "returns zero for an empty list" do
119
+ a = Rico::Array.new RiakHelpers.bucket, "length_empty"
120
+ a.length.should eql 0
121
+ end
122
+
123
+ it "returns the number of entries" do
124
+ a = Rico::Array.new RiakHelpers.bucket, "length_6"
125
+ a.add(1, 2, 3, 4, 5, 6)
126
+ a.length.should eql 6
127
+ end
128
+
129
+ it "is aliased to #count" do
130
+ a = Rico::Array.new RiakHelpers.bucket, "length_alias"
131
+ a.add(1, 2, 3, 4, 5, 6)
132
+ a.count.should eql 6
133
+ end
134
+ end
135
+
136
+ it "is enumerable" do
137
+ a = Rico::Array.new RiakHelpers.bucket, "enumerable"
138
+ a.add(3, 1, 4, 1, 5, 9)
139
+ a.to_a.should eql [3, 1, 4, 1, 5, 9]
140
+ a.each { }.length.should eql 6
141
+ a.map {|x| x + 1 }.should eql [4, 2, 5, 2, 6, 10]
142
+ a[4].should eql 5
143
+ end
144
+ end
data/spec/list_spec.rb ADDED
@@ -0,0 +1,40 @@
1
+ require "spec_helper"
2
+
3
+ describe Rico::List do
4
+ before :each do
5
+ RiakHelpers.reset!
6
+ end
7
+
8
+ describe "#add" do
9
+ it "adds values and sorts them in instance" do
10
+ a = Rico::List.new RiakHelpers.bucket, "list_add_1"
11
+ a.add(3, 2, 1)
12
+ a.members.should eql [1, 2, 3]
13
+ end
14
+
15
+ it "adds values and sorts them on read" do
16
+ a = Rico::List.new RiakHelpers.bucket, "list_add_2"
17
+ a.add(3, 2, 1)
18
+ b = Rico::List.new RiakHelpers.bucket, "list_add_2"
19
+ b.members.should eql [1, 2, 3]
20
+ end
21
+
22
+ it "allows duplicates of the same value" do
23
+ a = Rico::List.new RiakHelpers.bucket, "list_add_duplicate"
24
+ a.add(3, 6, 4, 1, 7, 9, 1, 1, 3, 3)
25
+ b = Rico::List.new RiakHelpers.bucket, "list_add_duplicate"
26
+ b.members.should eql [1, 1, 1, 3, 3, 3, 4, 6, 7, 9]
27
+ end
28
+ end
29
+
30
+ describe "#remove" do
31
+ it "removes all occurence of the item" do
32
+ a = Rico::List.new RiakHelpers.bucket, "list_remove_all_duplicates"
33
+ a.add(1, 1, 1, 2, 2, 2)
34
+ b = Rico::List.new RiakHelpers.bucket, "list_remove_all_duplicates"
35
+ b.remove(1, 2)
36
+ c = Rico::List.new RiakHelpers.bucket, "list_remove_all_duplicates"
37
+ c.members.should eql []
38
+ end
39
+ end
40
+ end
data/spec/rico_spec.rb ADDED
@@ -0,0 +1,45 @@
1
+ require "spec_helper"
2
+
3
+ describe Rico do
4
+ before :each do
5
+ RiakHelpers.reset!
6
+ end
7
+
8
+ describe ".configure" do
9
+ it "accepts a namespace" do
10
+ Rico.configure do |r|
11
+ r.namespace = "myapp"
12
+ end
13
+
14
+ Rico.namespace.should eql "myapp"
15
+ end
16
+
17
+ it "accepts a riak client instance" do
18
+ riak = Riak::Client.new
19
+ Rico.configure do |r|
20
+ r.riak = riak
21
+ end
22
+
23
+ Rico.riak.should eql riak
24
+ end
25
+ end
26
+
27
+ describe ".bucket" do
28
+ describe "namespacing" do
29
+ it "supports an empty namespace" do
30
+ Rico.namespace = nil
31
+ Rico.bucket("users").name.should eql "users"
32
+ end
33
+
34
+ it "supports a single namespace" do
35
+ Rico.namespace = "development"
36
+ Rico.bucket("users").name.should eql "development:users"
37
+ end
38
+
39
+ it "supports multiple namespaces" do
40
+ Rico.namespace = ["myapp", "development"]
41
+ Rico.bucket("users").name.should eql "myapp:development:users"
42
+ end
43
+ end
44
+ end
45
+ end
data/spec/set_spec.rb ADDED
@@ -0,0 +1,22 @@
1
+ require "spec_helper"
2
+
3
+ describe Rico::Set do
4
+ before :each do
5
+ RiakHelpers.reset!
6
+ end
7
+
8
+ describe "#add" do
9
+ it "dedupes and retains order in instance" do
10
+ a = Rico::Set.new RiakHelpers.bucket, "set_add_1"
11
+ a.add(3, 4, 6, 8, 1, 2, 3, 4, 7)
12
+ a.members.should eql [3, 4, 6, 8, 1, 2, 7]
13
+ end
14
+
15
+ it "dedupes and sort is retained on read" do
16
+ a = Rico::Set.new RiakHelpers.bucket, "set_add_2"
17
+ a.add(3, 4, 6, 8, 1, 2, 3, 4, 7)
18
+ b = Rico::Set.new RiakHelpers.bucket, "set_add_2"
19
+ b.members.should eql [3, 4, 6, 8, 1, 2, 7]
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ require "spec_helper"
2
+
3
+ describe Rico::SortedSet do
4
+ before :each do
5
+ RiakHelpers.reset!
6
+ end
7
+
8
+ describe "#add" do
9
+ it "dedupes and sorts in instance" do
10
+ a = Rico::SortedSet.new RiakHelpers.bucket, "sorted_set_add_1"
11
+ a.add(3, 2, 2, 1, 1)
12
+ a.members.should eql [1, 2, 3]
13
+ end
14
+
15
+ it "dedupes and sort is retained on read" do
16
+ a = Rico::SortedSet.new RiakHelpers.bucket, "sorted_set_add_2"
17
+ a.add(3, 3, 2, 2, 1)
18
+ b = Rico::SortedSet.new RiakHelpers.bucket, "sorted_set_add_2"
19
+ b.members.should eql [1, 2, 3]
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,21 @@
1
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
2
+
3
+ require "rubygems"
4
+ require "rico"
5
+
6
+ module RiakHelpers
7
+ def self.bucket
8
+ "rico_test"
9
+ end
10
+
11
+ def self.reset!
12
+ Rico.namespace = "test"
13
+ Rico.riak = Riak::Client.new(http_port: 8091)
14
+
15
+ Riak.disable_list_keys_warnings = true
16
+ b = Rico.bucket(bucket)
17
+ b.keys.each do |k|
18
+ b.delete k
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,55 @@
1
+ require "spec_helper"
2
+
3
+ describe Rico::Value do
4
+ before :each do
5
+ RiakHelpers.reset!
6
+ end
7
+
8
+ describe "#exists?" do
9
+ it "returns true if it exists" do
10
+ a = Rico::Value.new RiakHelpers.bucket, "value_exists_true"
11
+ a.set "bob"
12
+ b = Rico::Value.new RiakHelpers.bucket, "value_exists_true"
13
+ b.exists?.should eql true
14
+ end
15
+
16
+ it "returns false if it does not exists" do
17
+ a = Rico::Value.new RiakHelpers.bucket, "value_exists_false"
18
+ a.exists?.should eql false
19
+ end
20
+ end
21
+
22
+ describe "#get" do
23
+ it "returns the value" do
24
+ a = Rico::Value.new RiakHelpers.bucket, "value_get_return_value"
25
+ a.set([1, 2, 3, 4, 5, "bob", "joe"])
26
+ b = Rico::Value.new RiakHelpers.bucket, "value_get_return_value"
27
+ b.get.should eql [1, 2, 3, 4, 5, "bob", "joe"]
28
+ end
29
+
30
+ it "returns nil if the value does not exist" do
31
+ a = Rico::Value.new RiakHelpers.bucket, "value_get_nil"
32
+ a.get.should eql nil
33
+ end
34
+ end
35
+
36
+ describe "#set" do
37
+ it "writes the value" do
38
+ a = Rico::Value.new RiakHelpers.bucket, "value_set"
39
+ a.set "john"
40
+ b = Rico::Value.new RiakHelpers.bucket, "value_set"
41
+ b.get.should eql "john"
42
+ end
43
+ end
44
+
45
+ describe "#delete" do
46
+ it "deletes an existing object" do
47
+ a = Rico::Value.new RiakHelpers.bucket, "delete_existing"
48
+ a.set "john"
49
+ a.delete
50
+ b = Rico::Value.new RiakHelpers.bucket, "delete_existing"
51
+ b.exists?.should eql false
52
+ b.get.should eql nil
53
+ end
54
+ end
55
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rico
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jason Coene
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: riak-client
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.1'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.1'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '2.12'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '2.12'
46
+ description: Primative data types on Riak
47
+ email:
48
+ - jcoene@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .rspec
55
+ - Gemfile
56
+ - LICENSE
57
+ - README.md
58
+ - Rakefile
59
+ - lib/rico.rb
60
+ - lib/rico/array.rb
61
+ - lib/rico/counter.rb
62
+ - lib/rico/list.rb
63
+ - lib/rico/object.rb
64
+ - lib/rico/set.rb
65
+ - lib/rico/sorted_set.rb
66
+ - lib/rico/value.rb
67
+ - lib/rico/version.rb
68
+ - rico.gemspec
69
+ - spec/array_spec.rb
70
+ - spec/list_spec.rb
71
+ - spec/rico_spec.rb
72
+ - spec/set_spec.rb
73
+ - spec/sorted_set_spec.rb
74
+ - spec/spec_helper.rb
75
+ - spec/value_spec.rb
76
+ homepage: https://github.com/jcoene/rico
77
+ licenses: []
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 1.8.23
97
+ signing_key:
98
+ specification_version: 3
99
+ summary: Primative data types on Riak
100
+ test_files:
101
+ - spec/array_spec.rb
102
+ - spec/list_spec.rb
103
+ - spec/rico_spec.rb
104
+ - spec/set_spec.rb
105
+ - spec/sorted_set_spec.rb
106
+ - spec/spec_helper.rb
107
+ - spec/value_spec.rb