pears 0.1.4 → 0.3.1

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: 84ea4b16d1dcfcc4911f20b9acfb4053aec840216e682e02364877648a979e9e
4
- data.tar.gz: 6851286da9c77ae22c7bfef5418a84400b1663f763f450e7115a89a1cc1c0b1a
3
+ metadata.gz: 10cdea0d2eb59b303dab2dac91edbe3a3f37d6bb833fcbccaf7176ab28cc6813
4
+ data.tar.gz: aef9803cf0c1c022fdb21615c0d76c228c3eabbaf160bd5742d6fee244315301
5
5
  SHA512:
6
- metadata.gz: '085fe231ed469af60f246a03d69555713334c00c80f39a0e22c753a3d8c901f46412328f20579497ac852732c000173390c7f6a60f09ddb57446d5f5c73f744b'
7
- data.tar.gz: 3ff07d4ebc6354a61b5dfc84d1a463ba7a43c4fc69d472b7570754bc181a6d94ebdd000da83fd00459f6724ca386fd0facaba6060a73574667dcca018c47ffe9
6
+ metadata.gz: 12365453b2bbdb8fc612180dd3d7723811ff3068c8c78d46c2b957824ce44cc477438d76d5a2086d7c252772a2d3ea11101fbef44e0df56e70222c0e3e3adb2f
7
+ data.tar.gz: aefbc749ea3806cec18b20c948cf93253ac9d87c1de579c06a2ea757bbd8ddd8e1b0d483222e5dfe7e78160c264b735cb05387606c2687d9ddedab280fa9308c
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pears (0.1.3)
4
+ pears (0.3.1)
5
5
  activesupport
6
6
  dry-configurable (>= 0.13.0)
7
7
  redis
@@ -9,7 +9,7 @@ PATH
9
9
  GEM
10
10
  remote: https://rubygems.org/
11
11
  specs:
12
- activesupport (7.0.2.3)
12
+ activesupport (7.0.3.1)
13
13
  concurrent-ruby (~> 1.0, >= 1.0.2)
14
14
  i18n (>= 1.6, < 2)
15
15
  minitest (>= 5.1)
@@ -17,16 +17,16 @@ GEM
17
17
  byebug (11.1.3)
18
18
  concurrent-ruby (1.1.10)
19
19
  diff-lcs (1.4.4)
20
- dry-configurable (0.14.0)
20
+ dry-configurable (0.15.0)
21
21
  concurrent-ruby (~> 1.0)
22
22
  dry-core (~> 0.6)
23
- dry-core (0.7.1)
23
+ dry-core (0.8.0)
24
24
  concurrent-ruby (~> 1.0)
25
- i18n (1.10.0)
25
+ i18n (1.12.0)
26
26
  concurrent-ruby (~> 1.0)
27
- minitest (5.15.0)
27
+ minitest (5.16.2)
28
28
  rake (13.0.6)
29
- redis (4.6.0)
29
+ redis (4.7.1)
30
30
  rspec (3.10.0)
31
31
  rspec-core (~> 3.10.0)
32
32
  rspec-expectations (~> 3.10.0)
@@ -42,7 +42,7 @@ GEM
42
42
  diff-lcs (>= 1.2.0, < 2.0)
43
43
  rspec-support (~> 3.10.0)
44
44
  rspec-support (3.10.3)
45
- tzinfo (2.0.4)
45
+ tzinfo (2.0.5)
46
46
  concurrent-ruby (~> 1.0)
47
47
 
48
48
  PLATFORMS
@@ -0,0 +1,14 @@
1
+ module Pears
2
+ module Commands
3
+ class << self
4
+ def purge_subjects
5
+ Pears.subjects.each(&:purge)
6
+ Pears.instance_variable_set('@subjects', [])
7
+ end
8
+
9
+ def reboot
10
+ Pears.subjects.each(&:reboot)
11
+ end
12
+ end
13
+ end
14
+ end
data/lib/pears/errors.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  module Pears
2
2
  class Error < StandardError; end
3
3
  class RedisHasLeftTheBuilding < Error; end
4
+ class SubjectNotfound < Error; end
4
5
  class Pears::InvalidProviderData < Error; end
5
6
  end
@@ -19,6 +19,10 @@ module Pears
19
19
  @data[key]
20
20
  end
21
21
 
22
+ def purge
23
+ true
24
+ end
25
+
22
26
  private
23
27
 
24
28
  def parse_yaml(yaml_string)
@@ -7,13 +7,23 @@ module Pears
7
7
  def self.enable_provider(provider, yielder: false)
8
8
  method_name = provider.name.split('::').last.underscore.to_sym
9
9
  if yielder
10
- define_method(method_name) do |*args, &block|
10
+ define_method(method_name) do |*args, **opts, &block|
11
+ # Skip if Rails environment should not use this perticular setting
12
+ if defined?(Rails)
13
+ return if Array.wrap(opts[:skip_env]).include?(Rails.env.to_sym)
14
+ end
15
+
11
16
  pr = provider.new(builder: self, &block)
12
17
  push pr
13
18
  pr
14
19
  end
15
20
  else
16
21
  define_method(method_name) do |*args, **opts, &block|
22
+ # Skip if Rails environment should not use this perticular setting
23
+ if defined?(Rails)
24
+ return if Array.wrap(opts[:skip_env]).include?(Rails.env.to_sym)
25
+ end
26
+
17
27
  pr = provider.new(*args, **opts, &block)
18
28
  push pr
19
29
  pr
@@ -26,6 +36,10 @@ module Pears
26
36
  enable_provider RemoteFile
27
37
  enable_provider Subscription, yielder: true
28
38
 
39
+ if defined?(Rails) == "constant"
40
+ enable_provider RailsConfig
41
+ end
42
+
29
43
  delegate :name, to: :subject
30
44
 
31
45
  def initialize(subject)
@@ -0,0 +1,14 @@
1
+ module Pears
2
+ # Move this one into Pears, its really handy!
3
+ module Provider
4
+ class Debuggy < Pears::Provider::Base
5
+ def data
6
+ puts "Pears Debugger was called."
7
+ puts @data
8
+ @data
9
+ end
10
+ end
11
+ Provider::Builder.enable_provider Pears::Provider::Debuggy
12
+ end
13
+ end
14
+
@@ -0,0 +1,23 @@
1
+ require 'yaml'
2
+ require 'active_support/core_ext/hash/indifferent_access'
3
+
4
+ module Pears
5
+ module Provider
6
+ # Used for loading simple YAML file locally.
7
+ class RailsConfig < Base
8
+ def initialize(*path_parts, on_failure: :raise)
9
+ file = path_parts.pop
10
+ file_path = Rails.root.join('config', *path_parts, "#{file}.yml")
11
+ yaml_data = File.read(file_path)
12
+ @data = parse_yaml(yaml_data)
13
+ rescue Errno::ENOENT,
14
+ Pears::InvalidProviderData => error
15
+ if on_failure == :null
16
+ @data = {}
17
+ else
18
+ raise error
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -44,16 +44,24 @@ module Pears
44
44
  @subscription.terminate
45
45
  end
46
46
 
47
+ def purge
48
+ unsubscribe
49
+ true
50
+ end
51
+
47
52
  private
48
53
 
49
54
  def subscribe
55
+ nth = 0
50
56
  @subscription = Thread.new do
51
- connection.subscribe(channel) do |on|
52
- on.message do |channel, message|
53
- load_config
57
+ loop do
58
+ connection.subscribe(channel) do |on|
59
+ on.message { |_channel, _message| load_config }
54
60
  end
61
+
62
+ nth += 1
63
+ puts "Pears connection was lost for the #{nth}th time"
55
64
  end
56
- puts "Redis connection died!"
57
65
  end
58
66
  end
59
67
 
@@ -1,11 +1,13 @@
1
1
  require_relative "provider/base"
2
2
  require_relative "provider/local_file"
3
3
  require_relative "provider/remote_file"
4
+ require_relative "provider/rails_config"
4
5
  require_relative "provider/subscription"
5
6
  require_relative "provider/env"
6
7
  require_relative "provider/loco"
7
8
  require_relative "provider/hash"
8
9
  require_relative "provider/builder"
10
+ require_relative "provider/debugger"
9
11
 
10
12
  module Pears
11
13
  module Provider
data/lib/pears/subject.rb CHANGED
@@ -3,15 +3,16 @@ require 'active_support/core_ext/hash/indifferent_access'
3
3
  module Pears
4
4
  # A subject is a collection of settings.
5
5
  # You could different Pears::Subjects in your application for different parts
6
- # of its domain. for instance: application_settings, theming or translations.
6
+ # of its domain. for instance: application_settings, theming or translaions.
7
7
  class Subject
8
8
  include Enumerable
9
9
  attr_reader :name
10
10
 
11
- def initialize(name, *layers)
11
+ def initialize(name, *layers, loader: ->(builder){} )
12
12
  @name = name
13
13
  @layers = layers
14
- Pears.subjects << self
14
+ @loader = loader
15
+ boot
15
16
  end
16
17
 
17
18
  def [](key)
@@ -30,6 +31,10 @@ module Pears
30
31
  project.dig(*args)
31
32
  end
32
33
 
34
+ def purge
35
+ @layers.each.each(&:purge)
36
+ end
37
+
33
38
  def push_layer(layer)
34
39
  @layers << layer
35
40
  end
@@ -45,8 +50,18 @@ module Pears
45
50
  project.each(&block)
46
51
  end
47
52
 
53
+ def reboot
54
+ purge
55
+ @layers = []
56
+ boot
57
+ end
58
+
48
59
  private
49
60
 
61
+ def boot
62
+ @loader.call(Provider::Builder.new(self))
63
+ end
64
+
50
65
  def layers
51
66
  @layers
52
67
  end
@@ -0,0 +1,24 @@
1
+ module Pears
2
+ module Testing
3
+ module Subject
4
+ def prepend *address, is:
5
+ @layers = @layers.unshift Pears::Provider::Hash.new(hasher(address, is: is))
6
+ end
7
+
8
+ def shift
9
+ @layers.shift
10
+ end
11
+
12
+ private
13
+
14
+ def hasher(address, is:)
15
+ local = address.shift
16
+ if address.empty?
17
+ { local => is }
18
+ else
19
+ { local => hasher(address, is: is) }
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,32 @@
1
+ require 'pears/testing/subject'
2
+
3
+ module Pears
4
+ module Testing
5
+ def self.prepend_subject subject_name, *address, is:
6
+ subject = find_subject!(subject_name)
7
+ subject.prepend *address, is: is
8
+ begin
9
+ yield
10
+ ensure
11
+ subject.shift
12
+ end
13
+ end
14
+
15
+ def self.find_subject!(name)
16
+ Pears.subjects
17
+ .find { |subject| subject.name.to_sym == name.to_sym }.tap do |sub|
18
+ unless sub.present?
19
+ raise SubjectNotfound.new(
20
+ message: "No subject with the name #{subject_name} exists."
21
+ )
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ module Pears
29
+ class Subject
30
+ include Pears::Testing::Subject
31
+ end
32
+ end
data/lib/pears/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Pears
4
- VERSION = "0.1.4"
4
+ VERSION = "0.3.1"
5
5
  end
data/lib/pears.rb CHANGED
@@ -4,6 +4,7 @@ require_relative "pears/version"
4
4
  require_relative "pears/errors"
5
5
  require_relative "pears/provider"
6
6
  require_relative "pears/subject"
7
+ require_relative "pears/commands"
7
8
 
8
9
  module Pears
9
10
  extend Dry::Configurable
@@ -15,11 +16,15 @@ module Pears
15
16
  end
16
17
 
17
18
  # This is the main way of locating a subject.
18
- def self.subject(name)
19
- subjects
20
- Subject.new(name).tap do |subject|
21
- builder = Provider::Builder.new(subject)
22
- @subjects << yield(builder)
19
+ def self.subject(name, &block)
20
+ if subjects.any? { |sub| sub.name == name }
21
+ raise Pears::Error.new("subject name #{name} is already in use.")
23
22
  end
23
+
24
+ Subject.new(name, loader: block).tap { |sub| @subjects.push sub }
25
+ end
26
+
27
+ def self.[](name)
28
+ subjects.find { |subject| subject.name == name }
24
29
  end
25
30
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pears
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steven Kemp
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-03-29 00:00:00.000000000 Z
11
+ date: 2022-07-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-configurable
@@ -84,17 +84,22 @@ files:
84
84
  - bin/console
85
85
  - bin/setup
86
86
  - lib/pears.rb
87
+ - lib/pears/commands.rb
87
88
  - lib/pears/errors.rb
88
89
  - lib/pears/provider.rb
89
90
  - lib/pears/provider/base.rb
90
91
  - lib/pears/provider/builder.rb
92
+ - lib/pears/provider/debugger.rb
91
93
  - lib/pears/provider/env.rb
92
94
  - lib/pears/provider/hash.rb
93
95
  - lib/pears/provider/local_file.rb
94
96
  - lib/pears/provider/loco.rb
97
+ - lib/pears/provider/rails_config.rb
95
98
  - lib/pears/provider/remote_file.rb
96
99
  - lib/pears/provider/subscription.rb
97
100
  - lib/pears/subject.rb
101
+ - lib/pears/testing.rb
102
+ - lib/pears/testing/subject.rb
98
103
  - lib/pears/version.rb
99
104
  - pears.gemspec
100
105
  homepage: https://bitbucket.org/ivaldi/rubygems-pears/src/master/