juvet 0.0.3 → 0.0.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5ac5e5b95d6312d763f506e3ba5e5d60ae2da66a
4
- data.tar.gz: 728de00cdd354de2f413f893e4f5c78aae180f06
3
+ metadata.gz: 7d3483a72f0e396a5f010a3ad73f5a1d888e89d2
4
+ data.tar.gz: 551045f13d3322dd05a2a1bd6574e9d32f37e07b
5
5
  SHA512:
6
- metadata.gz: e42c4c453fddc8fecfbfc6a7974fd8bd8d6b427ee601fd151393850eb65d4e05d1096227abd76780a20c2f306346d2ff5067dc469abe042529a368e8c42c2ef1
7
- data.tar.gz: 3ec51c75ca8e74654b86c2a31da27a58ccd5a272c08c976b766bd7de8440e5da316c7e091223a6a6ef30dc4122260a6c46744ddd5fc102f8c19d6917f43065e6
6
+ metadata.gz: 1dcd42919b068c0e841e1f7fa4052f6c5323f9d6071fb70b142dce80908f25835cd05661738cb9a2fdcd697c4ab4a09d0c8c1aee80f12f2c0cd657aa39c0e628
7
+ data.tar.gz: bd7ad250abe13a6c48caa4e0ec24d80090c669a5c86937f3faaaef49076c11ecadb398cb45eeda81a1927fffbf574e362421cbe11982e95576c501b81b655aa0
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- juvet (0.0.3)
4
+ juvet (0.0.4)
5
5
  redis
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -19,7 +19,7 @@ Juvet.configure do
19
19
  adapter :redis, url: ENV["REDIS_URL"]
20
20
  mapping do
21
21
  collection :bots do
22
- repository Juvet::BotRepository
22
+ repository Juvet::BotRepository, adapter: :redis
23
23
  entity Juvet::Bot
24
24
  end
25
25
  end
@@ -1,10 +1,5 @@
1
1
  module Juvet
2
2
  module Attributable
3
- def initialize(attributes={})
4
- initialize! attributes
5
- attribute! attributes
6
- end
7
-
8
3
  def initialize!(attributes)
9
4
  attributes.each { |name, value| set_variable! name, value }
10
5
  end
@@ -26,6 +21,11 @@ module Juvet
26
21
 
27
22
  private
28
23
 
24
+ def initialize(attributes={})
25
+ initialize! attributes
26
+ attribute! attributes
27
+ end
28
+
29
29
  def create_method(name, &block)
30
30
  self.class.send(:define_method, name, &block)
31
31
  end
data/lib/juvet/bot.rb CHANGED
@@ -6,7 +6,7 @@ module Juvet
6
6
 
7
7
  class << self
8
8
  def create(id, attributes={})
9
- new id, attributes
9
+ new attributes.merge(id: id)
10
10
  end
11
11
 
12
12
  def create!(id, attributes={})
@@ -25,12 +25,5 @@ module Juvet
25
25
  def update!(attributes={})
26
26
  BotRepository.update self
27
27
  end
28
-
29
- private
30
-
31
- def initialize(id, attributes={})
32
- @id = id
33
- super attributes
34
- end
35
28
  end
36
29
  end
@@ -5,35 +5,26 @@ module Juvet
5
5
  attr_reader :options
6
6
  attr_reader :type
7
7
 
8
- def initialize(options={})
9
- opts = default_options.merge((options || {}))
8
+ def initialize(type=nil, options={})
9
+ opts = (options || {}).dup
10
10
 
11
- @type = opts.delete :type
11
+ @type = type || :null
12
12
  @options = opts
13
- @class_name = Juvet::String.new("#{type}_adapter").classify
13
+ @class_name = Juvet::String.new("#{self.type}_adapter").classify
14
14
  end
15
15
 
16
- def build(mapper)
16
+ def build
17
17
  load_adapter
18
- instantiate_adapter mapper
18
+ Juvet::Adapters.const_get(class_name)
19
19
  end
20
20
 
21
21
  private
22
22
 
23
- def default_options
24
- { type: :null }.freeze
25
- end
26
-
27
23
  def load_adapter
28
24
  require "juvet/adapters/#{type}_adapter"
29
25
  rescue LoadError => e
30
26
  raise LoadError.new("Cannot find Juvet adapter '#{type}' (#{e.message})")
31
27
  end
32
-
33
- def instantiate_adapter(mapper)
34
- klass = Juvet::Adapters.const_get(class_name)
35
- klass.new(mapper.collection_for_repository(klass), options)
36
- end
37
28
  end
38
29
  end
39
30
  end
@@ -7,8 +7,10 @@ module Juvet
7
7
  @block = block
8
8
  end
9
9
 
10
- def build
11
- Juvet::Mapper.new &@block
10
+ def build(adapter)
11
+ mapper = Juvet::Mapper.new &@block
12
+ mapper.build(adapter)
13
+ mapper
12
14
  end
13
15
  end
14
16
  end
@@ -3,8 +3,9 @@ require_relative "configuration/mapping"
3
3
 
4
4
  module Juvet
5
5
  class Configuration
6
- def adapter(options=nil)
7
- @adapter ||= Juvet::Configuration::Adapter.new(options)
6
+ def adapter(type=nil, options=nil)
7
+ return @adapter if type.nil?
8
+ @adapter ||= Juvet::Configuration::Adapter.new(type, options)
8
9
  end
9
10
 
10
11
  def mapping(&block)
@@ -12,7 +13,7 @@ module Juvet
12
13
  end
13
14
 
14
15
  def load!
15
- adapter.build mapping.build
16
+ mapping.build adapter
16
17
  end
17
18
  end
18
19
  end
@@ -1,3 +1,5 @@
1
+ require_relative "mapped_repository"
2
+
1
3
  module Juvet
2
4
  class Mapper
3
5
  class Collection
@@ -16,9 +18,9 @@ module Juvet
16
18
  end
17
19
  end
18
20
 
19
- def repository(klass=nil)
21
+ def repository(klass=nil, options=nil)
20
22
  if klass
21
- @repository = klass
23
+ @repository = MappedRepository.new klass, options
22
24
  else
23
25
  @repository
24
26
  end
@@ -0,0 +1,12 @@
1
+ module Juvet
2
+ class Mapper
3
+ class MappedRepository
4
+ attr_reader :klass, :adapter
5
+
6
+ def initialize(klass, options=nil)
7
+ @klass = klass
8
+ @adapter = (options || {}).fetch(:adapter) { :null }
9
+ end
10
+ end
11
+ end
12
+ end
data/lib/juvet/mapper.rb CHANGED
@@ -14,8 +14,13 @@ module Juvet
14
14
  collections[name] = Juvet::Mapper::Collection.new name, &block
15
15
  end
16
16
 
17
- def collection_for_repository(klass)
18
- collections.values.find { |collection| collection.repository == klass }
17
+ def build(adapter)
18
+ collections.each_pair do |name, collection|
19
+ if collection.repository && collection.repository.adapter == adapter.type
20
+ adapter_instance = adapter.build.new collection, adapter.options
21
+ collection.repository.klass.adapter = adapter_instance
22
+ end
23
+ end
19
24
  end
20
25
  end
21
26
  end
data/lib/juvet/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Juvet
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -1,26 +1,26 @@
1
1
  describe Juvet::Configuration::Adapter do
2
2
  describe "#type" do
3
3
  it "sets the type from the options" do
4
- subject = described_class.new type: :null
4
+ subject = described_class.new :null
5
5
  expect(subject.type).to eq :null
6
6
  end
7
7
  end
8
8
 
9
9
  describe "#options" do
10
10
  it "includes any additional options initialized" do
11
- subject = described_class.new type: :nil, blah: :bleh
11
+ subject = described_class.new :null, blah: :bleh
12
12
  expect(subject.options).to eq({ blah: :bleh })
13
13
  end
14
14
  end
15
15
 
16
16
  describe "#class_name" do
17
17
  it "creates a class name from the type" do
18
- subject = described_class.new type: :null
18
+ subject = described_class.new :null
19
19
  expect(subject.class_name).to eq "NullAdapter"
20
20
  end
21
21
  end
22
22
 
23
- describe "default options" do
23
+ describe "default type" do
24
24
  it "has a null adapter type" do
25
25
  expect(described_class.new.type).to eq :null
26
26
  expect(described_class.new.class_name).to eq "NullAdapter"
@@ -28,19 +28,22 @@ describe Juvet::Configuration::Adapter do
28
28
  end
29
29
 
30
30
  describe "#build" do
31
- let(:mapper) { double(:mapper, collection_for_repository: nil) }
32
- subject { described_class.new type: :null }
31
+ subject { described_class.new :redis }
33
32
 
34
- it "creates an instance of the adapter" do
35
- adapter = subject.build mapper
33
+ it "loads the adapter" do
34
+ subject.build
35
+ expect { Juvet::Adapters::RedisAdapter }.to_not raise_error
36
+ end
36
37
 
37
- expect(adapter).to be_instance_of Juvet::Adapters::NullAdapter
38
+ it "returns the class name for the adapter" do
39
+ klass = subject.build
40
+ expect(klass).to eq Juvet::Adapters::RedisAdapter
38
41
  end
39
42
 
40
- it "returns a load error if the adapter type cannot be loaded" do
41
- subject = described_class.new type: :blah
43
+ it "raises a load error if the adapter type cannot be loaded" do
44
+ subject = described_class.new :blah
42
45
 
43
- expect { subject.build(mapper) }.to raise_error LoadError
46
+ expect { subject.build }.to raise_error LoadError
44
47
  end
45
48
  end
46
49
  end
@@ -6,10 +6,11 @@ describe Juvet::Configuration::Mapping do
6
6
  end
7
7
  }
8
8
  end
9
+ let(:adapter) { Juvet::Configuration::Adapter.new :null }
9
10
  subject { described_class.new &blk }
10
11
 
11
12
  it "creates a mapper from a block" do
12
- result = subject.build
13
+ result = subject.build adapter
13
14
 
14
15
  expect(result).to be_an_instance_of Juvet::Mapper
15
16
  expect(result.collections.length).to eq 1
@@ -2,7 +2,7 @@ describe Juvet::Configuration do
2
2
  subject { described_class.new }
3
3
 
4
4
  it "has a default repository adapter" do
5
- expect(subject.adapter).to be_instance_of Juvet::Configuration::Adapter
5
+ expect(subject.adapter(:null)).to be_instance_of Juvet::Configuration::Adapter
6
6
  end
7
7
 
8
8
  it "has a default mapping" do
@@ -13,12 +13,8 @@ describe Juvet::Configuration do
13
13
  subject { described_class.new }
14
14
 
15
15
  it "builds the mapping" do
16
- expect(subject.mapping).to receive(:build).and_call_original
17
- subject.load!
18
- end
19
-
20
- it "builds the adapter" do
21
- expect(subject.adapter).to receive(:build).with(Juvet::Mapper)
16
+ subject.adapter :null
17
+ expect(subject.mapping).to receive(:build).with(subject.adapter)
22
18
  subject.load!
23
19
  end
24
20
  end
@@ -9,11 +9,12 @@ describe Juvet::Mapper::Collection do
9
9
  it "executes the block if one is provided" do
10
10
  subject = described_class.new(:widgets) do
11
11
  entity Object
12
- repository Object
12
+ repository Object, adapter: :blah
13
13
  end
14
14
 
15
15
  expect(subject.entity).to eq Object
16
- expect(subject.repository).to eq Object
16
+ expect(subject.repository.klass).to eq Object
17
+ expect(subject.repository.adapter).to eq :blah
17
18
  end
18
19
  end
19
20
  end
@@ -0,0 +1,13 @@
1
+ describe Juvet::Mapper::MappedRepository do
2
+ describe "#initialize" do
3
+ it "extracts the adapter from the options" do
4
+ subject = described_class.new Object, adapter: :blah
5
+ expect(subject.adapter).to eq :blah
6
+ end
7
+
8
+ it "defaults to the null adapter if it's not specified" do
9
+ subject = described_class.new Object
10
+ expect(subject.adapter).to eq :null
11
+ end
12
+ end
13
+ end
@@ -10,19 +10,10 @@ describe Juvet::Mapper do
10
10
  end
11
11
  end
12
12
 
13
- describe "#collection" do
14
- subject { described_class.new }
15
-
16
- it "adds a collection with the block" do
17
- subject.collection(:widgets) do; end
18
-
19
- expect(subject.collections[:widgets]).to \
20
- be_instance_of Juvet::Mapper::Collection
13
+ describe "#build" do
14
+ class WidgetRepository
15
+ include Juvet::Repository
21
16
  end
22
- end
23
-
24
- describe "#collection_for_repository" do
25
- class WidgetRepository; end
26
17
  let(:subject) do
27
18
  described_class.new do
28
19
  collection :widgets do
@@ -31,17 +22,20 @@ describe Juvet::Mapper do
31
22
  end
32
23
  end
33
24
 
34
- it "returns the collection for the first one that specifies the repository" do
35
- result = subject.collection_for_repository WidgetRepository
36
-
37
- expect(result.name).to eq :widgets
38
- expect(result.repository).to eq WidgetRepository
25
+ it "sets the adapter on the repositories" do
26
+ expect(WidgetRepository).to receive(:adapter=).with(Juvet::Adapters::NullAdapter)
27
+ subject.build Juvet::Configuration::Adapter.new :null
39
28
  end
29
+ end
40
30
 
41
- it "returns nil if the repository is not found" do
42
- result = subject.collection_for_repository Object
31
+ describe "#collection" do
32
+ subject { described_class.new }
43
33
 
44
- expect(result).to be_nil
34
+ it "adds a collection with the block" do
35
+ subject.collection(:widgets) do; end
36
+
37
+ expect(subject.collections[:widgets]).to \
38
+ be_instance_of Juvet::Mapper::Collection
45
39
  end
46
40
  end
47
41
  end
data/spec/juvet_spec.rb CHANGED
@@ -19,9 +19,10 @@ describe Juvet do
19
19
 
20
20
  it "sets up the adapter" do
21
21
  described_class.configure do
22
- adapter type: :null, blah: "blah"
22
+ adapter :null, blah: "blah"
23
23
  end
24
- expect(described_class.configuration.adapter).to be_instance_of Juvet::Configuration::Adapter
24
+ expect(described_class.configuration.adapter).to \
25
+ be_instance_of Juvet::Configuration::Adapter
25
26
  expect(described_class.configuration.adapter.type).to eq :null
26
27
  expect(described_class.configuration.adapter.options).to eq({ blah: "blah" })
27
28
  expect(described_class.configuration.adapter.class_name).to eq "NullAdapter"
@@ -32,7 +33,8 @@ describe Juvet do
32
33
  mapping do
33
34
  end
34
35
  end
35
- expect(described_class.configuration.mapping).to be_instance_of Juvet::Configuration::Mapping
36
+ expect(described_class.configuration.mapping).to \
37
+ be_instance_of Juvet::Configuration::Mapping
36
38
  end
37
39
  end
38
40
  end
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.3
4
+ version: 0.0.4
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-15 00:00:00.000000000 Z
11
+ date: 2016-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis
@@ -52,6 +52,7 @@ files:
52
52
  - lib/juvet/entity_not_found_error.rb
53
53
  - lib/juvet/mapper.rb
54
54
  - lib/juvet/mapper/collection.rb
55
+ - lib/juvet/mapper/mapped_repository.rb
55
56
  - lib/juvet/repository.rb
56
57
  - lib/juvet/string.rb
57
58
  - lib/juvet/version.rb
@@ -63,6 +64,7 @@ files:
63
64
  - spec/juvet/configuration/mapping_spec.rb
64
65
  - spec/juvet/configuration_spec.rb
65
66
  - spec/juvet/mapper/collection_spec.rb
67
+ - spec/juvet/mapper/mapped_repository_spec.rb
66
68
  - spec/juvet/mapper_spec.rb
67
69
  - spec/juvet/repository_spec.rb
68
70
  - spec/juvet/string_spec.rb
@@ -101,6 +103,7 @@ test_files:
101
103
  - spec/juvet/configuration/mapping_spec.rb
102
104
  - spec/juvet/configuration_spec.rb
103
105
  - spec/juvet/mapper/collection_spec.rb
106
+ - spec/juvet/mapper/mapped_repository_spec.rb
104
107
  - spec/juvet/mapper_spec.rb
105
108
  - spec/juvet/repository_spec.rb
106
109
  - spec/juvet/string_spec.rb