basket 0.0.3 → 0.0.4

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
  SHA256:
3
- metadata.gz: '092352c400ad4483af38e9afe1060a39c6162c7318e4c69fb1ecd7649af56344'
4
- data.tar.gz: 9a6cb3e5937d7367b6e83bae58e796c71c7a2a7b77cec5bbbbd5178bf4e0107b
3
+ metadata.gz: fe1bb079417239ff859572188cdc08c280e471752d2e5bca308e26b57d375640
4
+ data.tar.gz: f8f4cb0b630b7f07b492a4281e04302322c4661266f0da966e4b5b4dc5f1b37c
5
5
  SHA512:
6
- metadata.gz: a0f510f3e3f9c7b7accff0b76580d426e939d82212d6a4d83979f3043e4d679c8fafe994e58f788cf920110f3094e45cac1e290b50c802a22a5937358cdabd86
7
- data.tar.gz: 33798eb722f92da71a396bac9529676f34cde316cdf3824fe5c42e66cfca26444caedb615fdcbad4ff67374a0a60dd646daf93eb349e0ee4d880f22b2186b2be
6
+ metadata.gz: 5cfa5e413c339a9f354457c7f2cf2a05edb21fecabaa63da9a6d8e51987d1e4ddef90b01235835ff330357834881836aa48dcf52edb8cc43e7d466e5f5528a6c
7
+ data.tar.gz: d880195c4f2d3ef24ed92e3ac288de53cfaca6990b1e8a8c596b0ad5fef126907b62336478edbf38c317c9f6a721bbcc5578c2a84af26047031acc25b0e5a11e
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- basket (0.0.2)
4
+ basket (0.0.3)
5
5
  redis
6
6
  redis-namespace
7
7
 
data/README.md CHANGED
@@ -26,7 +26,7 @@ If bundler is not being used to manage dependencies, install the gem by executin
26
26
  Add items to your basket as they come along. They might come along quickly, or there might be a delay between them. Regardless, you want to collect items into your basket before going and doing something with them.
27
27
 
28
28
  ```ruby
29
- while chicken.laying do
29
+ while chicken.laying? do
30
30
  egg = { egg: {color: brown, size: medium}}
31
31
  Basket.add('QuicheBasket', egg)
32
32
  end
@@ -89,7 +89,7 @@ Defining `on_add`, `on_failure`, and `on_success` is optional.
89
89
  In an initializer, or somewhere equally appropriate, you might put something like this:
90
90
 
91
91
  ```ruby
92
- Basket.config do |config|
92
+ Basket.configure do |config|
93
93
  config.redis_host = "127.0.0.2"
94
94
  config.redis_port = 6390
95
95
  config.redis_db = 10
@@ -98,10 +98,18 @@ Basket.config do |config|
98
98
  end
99
99
  ```
100
100
 
101
- The defaults for a redis backend are the standard "127.0.0.1", 6379, 15 with a namespace of :basket.
101
+ The defaults for a redis backend are the standard `"127.0.0.1"`, `6379`, `15` with a namespace of `:basket`.
102
102
 
103
103
  The default for the backend is the HashBackend, which can be set by passing `:hash` to `config.backend`, but you don't have to do that. Because it's the default!
104
104
 
105
+ For the redis configuration, you can alternatively pass a url, thusly:
106
+
107
+ ```ruby
108
+ Basket.configure do |config|
109
+ config.backend = :redis
110
+ config.redis_url = "redis://:p4ssw0rd@10.0.1.1:6380/15"
111
+ end
112
+ ```
105
113
  ## Development
106
114
 
107
115
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -110,6 +118,8 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
110
118
 
111
119
  This project uses Guard to facilitate local development. You can run it with `bundle exec guard`. It will run specs on change to files and will run `standard --fix` after passing tests.
112
120
 
121
+ Looking through the code base, the majority of the work happens in [lib/basket/handle_add.rb](https://github.com/nicholalexander/basket/blob/main/lib/basket/handle_add.rb). Alternatively, you might be interested in the [backend adapters](https://github.com/nicholalexander/basket/tree/main/lib/basket/backend_adapter) for how the gem works with in memory hashes and/or a redis backend.
122
+
113
123
  ## Contributing
114
124
 
115
125
  Bug reports and pull requests are welcome on GitHub at https://github.com/nicholalexander/basket. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/nicholalexander/basket/blob/main/CODE_OF_CONDUCT.md).
data/basket.gemspec CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
12
12
  spec.description = "A simple way of accumulating things and then acting on them."
13
13
  spec.homepage = "https://github.com/nicholalexander/basket"
14
14
  spec.license = "MIT"
15
- spec.required_ruby_version = ">= 2.6.0"
15
+ spec.required_ruby_version = ">= 3.0.6"
16
16
 
17
17
  spec.metadata["homepage_uri"] = spec.homepage
18
18
  spec.metadata["source_code_uri"] = "https://github.com/nicholalexander/basket"
@@ -6,11 +6,7 @@ module Basket
6
6
  attr_reader :client
7
7
 
8
8
  def initialize
9
- redis_connection = Redis.new(
10
- host: Basket.config.redis_host,
11
- port: Basket.config.redis_port,
12
- db: Basket.config.redis_db
13
- )
9
+ redis_connection = select_redis_connection
14
10
 
15
11
  @client = Redis::Namespace.new(
16
12
  Basket.config.namespace,
@@ -51,6 +47,28 @@ module Basket
51
47
  def deserialized_queue_data(queue)
52
48
  @client.lrange(queue, 0, -1).reverse.map { |marshalled_data| Marshal.load(marshalled_data) }
53
49
  end
50
+
51
+ def select_redis_connection
52
+ if Basket.config.redis_url
53
+ redis_connection_from_url
54
+ else
55
+ redis_connection_from_host
56
+ end
57
+ end
58
+
59
+ def redis_connection_from_host
60
+ Redis.new(
61
+ host: Basket.config.redis_host,
62
+ port: Basket.config.redis_port,
63
+ db: Basket.config.redis_db
64
+ )
65
+ end
66
+
67
+ def redis_connection_from_url
68
+ Redis.new(
69
+ url: Basket.config.redis_url
70
+ )
71
+ end
54
72
  end
55
73
  end
56
74
  end
@@ -1,6 +1,6 @@
1
1
  module Basket
2
2
  class Configuration
3
- attr_accessor :redis_host, :redis_port, :redis_db, :namespace
3
+ attr_accessor :redis_host, :redis_port, :redis_db, :namespace, :redis_url
4
4
  attr_reader :backend
5
5
 
6
6
  def initialize
@@ -9,6 +9,7 @@ module Basket
9
9
  @redis_db = 15
10
10
  @backend = BackendAdapter::HashBackend
11
11
  @namespace = :basket
12
+ @redis_url = nil
12
13
  end
13
14
 
14
15
  def backend=(backend)
@@ -7,43 +7,58 @@ module Basket
7
7
  def initialize(queue, data)
8
8
  @queue = queue
9
9
  @data = data
10
- @queue_collection = Basket.queue_collection
11
10
  end
12
11
 
13
- def call(data = @data)
14
- queue_length = @queue_collection.push(@queue, data)
15
- queue_class = class_for_queue
16
- queue_instance = queue_class.new
12
+ def call
13
+ setup_batchers
14
+ add_to_basket
15
+ perform if basket_full?(@queue_length, @queue_class)
16
+ rescue => error
17
+ maybe_raise_basket_error(error)
18
+ failure(error)
19
+ end
17
20
 
18
- queue_instance.define_singleton_method(:element) { data }
19
- queue_instance.on_add
21
+ private
20
22
 
21
- return unless basket_full?(queue_length, queue_class)
23
+ def setup_batchers
24
+ @queue_collection = Basket.queue_collection
25
+ @queue_class = class_for_queue
26
+ @queue_instance = @queue_class.new
27
+ end
22
28
 
23
- queue_instance.perform
24
- queue_instance.on_success
25
- @queue_collection.clear(@queue)
26
- rescue => e
27
- raise e if basket_error?(e)
29
+ def add_to_basket(data = @data)
30
+ @queue_length = @queue_collection.push(@queue, data)
31
+ @queue_instance.define_singleton_method(:element) { data }
32
+ @queue_instance.on_add
33
+ end
28
34
 
29
- queue_instance.define_singleton_method(:error) { e }
30
- queue_instance.on_failure
35
+ def perform
36
+ @queue_instance.perform
37
+ @queue_instance.on_success
38
+ @queue_collection.clear(@queue)
31
39
  end
32
40
 
33
- private
41
+ def failure(error)
42
+ @queue_instance.define_singleton_method(:error) { error }
43
+ @queue_instance.on_failure
44
+ end
34
45
 
35
46
  def class_for_queue
47
+ raise_basket_not_found unless Object.const_defined?(@queue)
36
48
  Object.const_get(@queue)
37
- rescue NameError => e
38
- raise Basket::BasketNotFoundError, "We couldn't find that basket anywhere, please make sure it is defined. | #{e.message}"
49
+ end
50
+
51
+ def raise_basket_not_found
52
+ raise Basket::BasketNotFoundError, "We couldn't find that basket anywhere, please make sure it is defined."
39
53
  end
40
54
 
41
55
  def basket_full?(queue_length, queue_class)
42
56
  queue_length == queue_class.basket_options_hash[:size]
43
57
  end
44
58
 
45
- def basket_error?(e)
46
- e.instance_of?(Basket::Error) || e.instance_of?(Basket::BasketNotFoundError)
59
+ def maybe_raise_basket_error(e)
60
+ raise e if e.instance_of?(Basket::Error)
61
+ raise e if e.instance_of?(Basket::BasketNotFoundError)
47
62
  end
48
63
  end
49
64
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Basket
4
- VERSION = "0.0.3"
4
+ VERSION = "0.0.4"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: basket
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
  - nichol alexander
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2023-03-26 00:00:00.000000000 Z
12
+ date: 2023-04-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: redis
@@ -98,7 +98,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
98
98
  requirements:
99
99
  - - ">="
100
100
  - !ruby/object:Gem::Version
101
- version: 2.6.0
101
+ version: 3.0.6
102
102
  required_rubygems_version: !ruby/object:Gem::Requirement
103
103
  requirements:
104
104
  - - ">="