riak-record 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3b2c2cddb2404fb67c841c07fc0834d675316b8e
4
- data.tar.gz: ea745d2eaffd595e3ccbb626f69166fb7c7349cb
3
+ metadata.gz: 85acfab0540c576334b9040c88a76e04c5a9d59f
4
+ data.tar.gz: db8d4bb1ad14f2b93866512641a7d13d13068f44
5
5
  SHA512:
6
- metadata.gz: 5c639fa02eff967d10dccec7b9677aa6e26fb0d9e22e7da40fd31d7381e5f00ed572f00a982d34f9307da8b39df82f51631ecdb5c11bc57bc8af187147a0f4b6
7
- data.tar.gz: 4114ff4f1766e9c59e5fc2d07fd31ae18a311e1ed7d41d056a8a53fb124d6d0973ef032cc25f0a9bc4418b6471c0d37c9f3729db0988d5ebdf14d85419ec08a2
6
+ metadata.gz: 6abaf2c13290039a9d166fa0ac863259b9bb7a4b4235be3a59a339c25f478e956b61e7acf4117288d6748770231fb27e621e691a04161097d945c8118bb7b4e7
7
+ data.tar.gz: b3420c21d49b66f30ce5ff1a1d7fdc0c5e5232dcc35ebd3a933b1bbec2b01f0a07374d4dfe41e7677796506bbd448846fa89a020fe0577225dd876b3d253f91c
data/Gemfile CHANGED
@@ -1,10 +1,7 @@
1
1
  source "http://rubygems.org"
2
- # Add dependencies required to use your gem here.
3
- # Example:
4
- # gem "activesupport", ">= 2.3.5"
5
2
 
6
- # Add dependencies to develop your gem here.
7
- # Include everything needed to run rake, tests, features, etc.
3
+ gem "riak-client", "~> 2.0.0"
4
+
8
5
  group :development do
9
6
  gem "rspec", "~> 3.1.0"
10
7
  gem "rdoc", "~> 3.12"
data/Gemfile.lock CHANGED
@@ -2,7 +2,9 @@ GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
4
  addressable (2.3.6)
5
+ beefcake (1.0.0)
5
6
  builder (3.2.2)
7
+ cert_validator (0.0.1)
6
8
  descendants_tracker (0.0.4)
7
9
  thread_safe (~> 0.3, >= 0.3.1)
8
10
  diff-lcs (1.2.5)
@@ -20,6 +22,8 @@ GEM
20
22
  oauth2
21
23
  hashie (3.3.1)
22
24
  highline (1.6.21)
25
+ i18n (0.6.11)
26
+ innertube (1.0.2)
23
27
  jeweler (2.0.1)
24
28
  builder
25
29
  bundler (>= 1.0)
@@ -47,6 +51,12 @@ GEM
47
51
  rake (10.3.2)
48
52
  rdoc (3.12.2)
49
53
  json (~> 1.4)
54
+ riak-client (2.0.0)
55
+ beefcake (~> 1.0)
56
+ cert_validator (~> 0.0.1)
57
+ i18n (>= 0.4.0)
58
+ innertube (~> 1.0.2)
59
+ multi_json (~> 1.0)
50
60
  rspec (3.1.0)
51
61
  rspec-core (~> 3.1.0)
52
62
  rspec-expectations (~> 3.1.0)
@@ -73,5 +83,6 @@ DEPENDENCIES
73
83
  bundler (~> 1.0)
74
84
  jeweler (~> 2.0.1)
75
85
  rdoc (~> 3.12)
86
+ riak-client (~> 2.0.0)
76
87
  rspec (~> 3.1.0)
77
88
  simplecov
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
data/lib/riak-record.rb CHANGED
@@ -1,14 +1,22 @@
1
+ require 'riak'
2
+
1
3
  class RiakRecord
2
4
  attr_reader :riak_object
3
5
 
4
- def initialize(riak_object)
5
- @riak_object = riak_object
6
+ def initialize(r = nil)
7
+ r = self.class.bucket.new(r) if r.nil? || r.is_a?(String)
8
+ raise ArgumentError unless r.is_a? Riak::RObject
9
+ @riak_object = r
6
10
  end
7
11
 
8
12
  def data
9
13
  riak_object.data
10
14
  end
11
15
 
16
+ def save
17
+ riak_object.store
18
+ end
19
+
12
20
  def self.bucket_name(name = :not_a_name)
13
21
  @bucket_name = name unless name == :not_a_name
14
22
  @bucket_name
@@ -20,22 +28,35 @@ class RiakRecord
20
28
 
21
29
  def self.record_attributes(*attributes)
22
30
  attributes.each do |method_name|
23
- send :define_method, method_name do
31
+ define_method(method_name) do
24
32
  data[method_name]
25
33
  end
34
+
35
+ define_method("#{method_name}=".to_sym) do |value|
36
+ data[method_name] = value
37
+ end
26
38
  end
27
39
  end
28
40
 
29
-
30
41
  def self.find(key_or_keys)
31
- if key_or_keys.is_a?(Array)
32
- bucket.get_many(key_or_keys).map{ |r| self.new(r) }
33
- else
34
- r = bucket.get(key_or_keys)
35
- self.new(r) unless r.nil?
42
+ return find_many(key_or_keys) if key_or_keys.is_a?(Array)
43
+
44
+ begin
45
+ self.new(bucket.get(key_or_keys.to_s))
46
+ rescue Riak::FailedRequest => e
47
+ if e.not_found?
48
+ nil
49
+ else
50
+ raise e
51
+ end
36
52
  end
37
53
  end
38
54
 
55
+ def self.find_many(keys)
56
+ hash = bucket.get_many(keys.map(&:to_s))
57
+ keys.map{ |k| hash[k] }
58
+ end
59
+
39
60
  def self.client=(client)
40
61
  @@client = client
41
62
  end
data/riak-record.gemspec CHANGED
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: riak-record 0.0.2 ruby lib
5
+ # stub: riak-record 0.0.3 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "riak-record"
9
- s.version = "0.0.2"
9
+ s.version = "0.0.3"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["Robert Graff"]
14
- s.date = "2014-09-17"
14
+ s.date = "2014-09-18"
15
15
  s.description = "RiakRecord is a thin and immature wrapper around riak-ruby-client. It creates a bucket for\n each class, provides a simple finder, and creates attribute reader."
16
16
  s.email = "robert_graff@yahoo.com"
17
17
  s.extra_rdoc_files = [
@@ -41,12 +41,14 @@ Gem::Specification.new do |s|
41
41
  s.specification_version = 4
42
42
 
43
43
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
44
+ s.add_runtime_dependency(%q<riak-client>, ["~> 2.0.0"])
44
45
  s.add_development_dependency(%q<rspec>, ["~> 3.1.0"])
45
46
  s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
46
47
  s.add_development_dependency(%q<bundler>, ["~> 1.0"])
47
48
  s.add_development_dependency(%q<jeweler>, ["~> 2.0.1"])
48
49
  s.add_development_dependency(%q<simplecov>, [">= 0"])
49
50
  else
51
+ s.add_dependency(%q<riak-client>, ["~> 2.0.0"])
50
52
  s.add_dependency(%q<rspec>, ["~> 3.1.0"])
51
53
  s.add_dependency(%q<rdoc>, ["~> 3.12"])
52
54
  s.add_dependency(%q<bundler>, ["~> 1.0"])
@@ -54,6 +56,7 @@ Gem::Specification.new do |s|
54
56
  s.add_dependency(%q<simplecov>, [">= 0"])
55
57
  end
56
58
  else
59
+ s.add_dependency(%q<riak-client>, ["~> 2.0.0"])
57
60
  s.add_dependency(%q<rspec>, ["~> 3.1.0"])
58
61
  s.add_dependency(%q<rdoc>, ["~> 3.12"])
59
62
  s.add_dependency(%q<bundler>, ["~> 1.0"])
@@ -1,11 +1,5 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
-
4
- class ExampleClient
5
- end
6
-
7
- RiakRecord.client = ExampleClient.new
8
-
9
3
  class ExampleA < RiakRecord
10
4
  bucket_name 'example_a'
11
5
  record_attributes :attribute1, :attribute2
@@ -26,6 +20,33 @@ describe "RiakRecord" do
26
20
  expect(ExampleA.client).to eq(ExampleB.client)
27
21
  end
28
22
 
23
+ describe "new" do
24
+ context "when passed in an RObject" do
25
+ let(:robject) { Riak::RObject.new("a-key") }
26
+ let(:record) { ExampleA.new(robject) }
27
+ it "should wrap it" do
28
+ expect(record.riak_object).to eq(robject)
29
+ expect(record).to be_a(ExampleA)
30
+ end
31
+ end
32
+ context "when passed in a string" do
33
+ let(:key) { 'judy' }
34
+ let(:record) { ExampleA.new(key) }
35
+ it "should create an RObject with that id" do
36
+ expect(record).to be_a(ExampleA)
37
+ expect(record.riak_object).to be_a(Riak::RObject)
38
+ expect(record.riak_object.key).to eq(key)
39
+ end
40
+ end
41
+ context "when passed in nil" do
42
+ let(:record) { ExampleA.new() }
43
+ it "should create an RObject with no id" do
44
+ expect(record).to be_a(ExampleA)
45
+ expect(record.riak_object).to be_a(Riak::RObject)
46
+ end
47
+ end
48
+ end
49
+
29
50
  describe "find" do
30
51
  let(:bucket){ double }
31
52
  let(:array){ Array.new }
@@ -39,26 +60,36 @@ describe "RiakRecord" do
39
60
  end
40
61
 
41
62
  it "should take a string and return nil if not found" do
42
- allow(bucket).to receive(:get).with("a-key").and_return(nil)
63
+ e = Riak::FailedRequest.new("not found")
64
+ allow(e).to receive(:not_found?).and_return(true)
65
+ allow(bucket).to receive(:get).with("a-key").and_raise(e)
43
66
  expect( ExampleA.find("a-key") ).to be_nil
44
67
  end
45
68
 
46
69
  it "should take a string and return an instance of Example" do
47
- allow(bucket).to receive(:get).with("a-key").and_return(double)
70
+ allow(bucket).to receive(:get).with("a-key").and_return(Riak::RObject.new("a-key"))
48
71
  expect( ExampleA.find("a-key") ).to be_an_instance_of(ExampleA)
49
72
  end
50
73
  end
51
74
 
52
75
  describe "record_attributes" do
53
- let(:riak_object) { double(:data => data) }
76
+ let(:riak_object) { Riak::RObject.new("obj").tap{|r| r.data = data } }
54
77
  let(:data) { {:attribute1 => "1"} }
55
- it "should respond to each attribute" do
56
- expect( ExampleA.new(riak_object) ).to respond_to(:attribute1)
57
- expect( ExampleA.new(riak_object) ).to respond_to(:attribute2)
78
+ let(:record) { ExampleA.new(riak_object) }
79
+ it "should read and write each attribute" do
80
+ expect{
81
+ record.attribute1=('b')
82
+ }.to change{record.attribute1}.from("1").to('b')
83
+
84
+ expect{
85
+ record.attribute2 = 'c'
86
+ }.to change{record.attribute2}.from(nil).to('c')
87
+
58
88
  end
59
89
 
60
90
  it "should return attribute from riak_object data" do
61
91
  expect( ExampleA.new(riak_object).attribute1 ).to eq("1")
62
92
  end
93
+
63
94
  end
64
95
  end
data/spec/spec_helper.rb CHANGED
@@ -20,6 +20,8 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
20
20
  require 'rspec'
21
21
  require 'riak-record'
22
22
 
23
+ RiakRecord.client = Riak::Client.new(:host => 'localhost', :pb_port => '8087')
24
+
23
25
  # Requires supporting files with custom matchers and macros, etc,
24
26
  # in ./support/ and its subdirectories.
25
27
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: riak-record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Graff
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-17 00:00:00.000000000 Z
11
+ date: 2014-09-18 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: riak-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 2.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 2.0.0
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: rspec
15
29
  requirement: !ruby/object:Gem::Requirement