juvet 0.0.4 → 0.0.5

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: 7d3483a72f0e396a5f010a3ad73f5a1d888e89d2
4
- data.tar.gz: 551045f13d3322dd05a2a1bd6574e9d32f37e07b
3
+ metadata.gz: 5d0de0f49adf12f3bb1ee241861332316fb1beb2
4
+ data.tar.gz: 5bfb76bc640fd10ed6ff0d079e34f033879aee12
5
5
  SHA512:
6
- metadata.gz: 1dcd42919b068c0e841e1f7fa4052f6c5323f9d6071fb70b142dce80908f25835cd05661738cb9a2fdcd697c4ab4a09d0c8c1aee80f12f2c0cd657aa39c0e628
7
- data.tar.gz: bd7ad250abe13a6c48caa4e0ec24d80090c669a5c86937f3faaaef49076c11ecadb398cb45eeda81a1927fffbf574e362421cbe11982e95576c501b81b655aa0
6
+ metadata.gz: e2a10cd29cdba17de6dd041a388abf3b21c349fd94987bfd96d33f77db9aba836daa5b0ecffed619a37958de921296bce8d0baf8bc68d5cdad7c914353f93840
7
+ data.tar.gz: c3702f246548de6bda6d1f6283f30ddc487b558f97fda747d6f3f18ea4b690e44c8ebb49f0985efb8c52d94455134f890614c84858136061ccac9a2c53885bfe
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- juvet (0.0.4)
4
+ juvet (0.0.5)
5
5
  redis
6
6
 
7
7
  GEM
@@ -4,6 +4,10 @@ module Juvet
4
4
  def initialize(collection=nil, options={})
5
5
  end
6
6
 
7
+ def all
8
+ []
9
+ end
10
+
7
11
  def create(entity)
8
12
  entity
9
13
  end
@@ -11,6 +11,12 @@ module Juvet
11
11
  @redis = Redis.new options
12
12
  end
13
13
 
14
+ def all
15
+ redis.keys("#{collection_prefix}:*").map do |key|
16
+ find_by_key(key)
17
+ end
18
+ end
19
+
14
20
  def create(entity)
15
21
  update_attributes entity
16
22
  end
@@ -20,9 +26,7 @@ module Juvet
20
26
  end
21
27
 
22
28
  def find(id)
23
- attributes = redis.get(collection_key(id))
24
- return nil if attributes.nil?
25
- collection.entity.new ({ id: id }).merge(JSON.load(attributes))
29
+ find_by_key collection_key(id)
26
30
  end
27
31
 
28
32
  def persist(entity)
@@ -37,7 +41,21 @@ module Juvet
37
41
  private
38
42
 
39
43
  def collection_key(id)
40
- "#{Juvet::String.new(collection.name).underscore}:#{id}"
44
+ "#{collection_prefix}:#{id}"
45
+ end
46
+
47
+ def collection_prefix
48
+ Juvet::String.new(collection.name).underscore
49
+ end
50
+
51
+ def find_by_key(key)
52
+ attributes = redis.get key
53
+ return nil if attributes.nil?
54
+ collection.entity.new ({ id: id_from_key(key) }).merge(JSON.load(attributes))
55
+ end
56
+
57
+ def id_from_key(key)
58
+ key.gsub("#{collection_prefix}:", "")
41
59
  end
42
60
 
43
61
  def key(entity)
@@ -5,6 +5,10 @@ module Juvet
5
5
  attr_reader :id
6
6
 
7
7
  class << self
8
+ def all!
9
+ BotRepository.all
10
+ end
11
+
8
12
  def create(id, attributes={})
9
13
  new attributes.merge(id: id)
10
14
  end
@@ -19,6 +19,10 @@ module Juvet
19
19
  @adapter = adapter
20
20
  end
21
21
 
22
+ def all
23
+ adapter.all
24
+ end
25
+
22
26
  def create(entity)
23
27
  adapter.create entity
24
28
  end
@@ -1,3 +1,3 @@
1
1
  module Juvet
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -2,6 +2,12 @@ describe Juvet::Adapters::NullAdapter do
2
2
  let(:entity) { Object.new }
3
3
  subject { described_class.new }
4
4
 
5
+ describe "#all" do
6
+ it "simply returns an empty array" do
7
+ expect(subject.all).to be_empty
8
+ end
9
+ end
10
+
5
11
  describe "#create" do
6
12
  it "simply returns the entity" do
7
13
  expect(subject.create(entity)).to eq entity
@@ -14,13 +14,24 @@ describe Juvet::Adapters::RedisAdapter do
14
14
  end
15
15
 
16
16
  let(:collection) { double(:collection, name: :entities, entity: Entity) }
17
- let(:entity) { Entity.new id: 123 }
17
+ let(:entity) { Entity.new id: "123" }
18
18
  let(:url) { "redis://localhost:6379" }
19
19
  subject { described_class.new collection, url: url, db: 15 }
20
20
 
21
21
  before(:all) { @redis = Redis.new db: 15 }
22
22
  after(:all) { @redis.flushdb }
23
23
 
24
+ describe "#all" do
25
+ it "returns all the entities for a given collection" do
26
+ entity1 = subject.create(entity)
27
+ entity2 = subject.create(Entity.new(id: "1234"))
28
+
29
+ result = subject.all
30
+
31
+ expect(result.map(&:id)).to eq ["1234", "123"]
32
+ end
33
+ end
34
+
24
35
  describe "#create" do
25
36
  it "stores the entity in the redis store" do
26
37
  result = subject.create entity
@@ -48,7 +59,7 @@ describe Juvet::Adapters::RedisAdapter do
48
59
  end
49
60
 
50
61
  it "returns nil if the entity can't be found" do
51
- result = subject.find 789
62
+ result = subject.find "789"
52
63
 
53
64
  expect(result).to be_nil
54
65
  end
@@ -84,7 +95,7 @@ describe Juvet::Adapters::RedisAdapter do
84
95
  end
85
96
 
86
97
  it "throws an exception if the entity is not already stored" do
87
- entity = Entity.new id: 456
98
+ entity = Entity.new id: "456"
88
99
 
89
100
  expect { subject.update entity }.to raise_error Juvet::EntityNotFoundError
90
101
  end
@@ -25,6 +25,13 @@ describe Juvet::Bot do
25
25
  end
26
26
  end
27
27
 
28
+ describe ".all!" do
29
+ it "returns all the bots from the repository" do
30
+ expect(Juvet::BotRepository).to receive(:all)
31
+ described_class.all!
32
+ end
33
+ end
34
+
28
35
  describe ".create!" do
29
36
  it "stores the created instance in the repository" do
30
37
  expect(Juvet::BotRepository).to receive(:create).with Juvet::Bot
@@ -7,6 +7,13 @@ describe Juvet::Repository do
7
7
  expect(Repository.adapter).to be_instance_of Juvet::Adapters::NullAdapter
8
8
  end
9
9
 
10
+ describe ".all" do
11
+ it "passes the retrieval to the adapter" do
12
+ expect(Repository.adapter).to receive(:all)
13
+ Repository.all
14
+ end
15
+ end
16
+
10
17
  describe ".create" do
11
18
  it "passes creation to the adapter" do
12
19
  object = Object.new
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: juvet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jamie Wright
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-16 00:00:00.000000000 Z
11
+ date: 2016-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis