racket-registry 0.1.3 → 0.2.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
  SHA1:
3
- metadata.gz: 6ba4ef30e904cd783dc5d576af5df26022a35f56
4
- data.tar.gz: 7149681f3c7c3f7daea25a681bb5e50d5d3c6224
3
+ metadata.gz: d0ba35e3b8ae749a231ddfb8ebc8c1be932123fc
4
+ data.tar.gz: dd77a4e59f964f4f23cddad580d3942ec5fc8096
5
5
  SHA512:
6
- metadata.gz: a4528d1b71a40ab40fd499adbd05e024a76634d0385654ccd2c209d88cfcf1aec1aecbf946b15d3b6aa24bf311b4f2267c5b16891c2a1b5044a54984a92b5547
7
- data.tar.gz: 8347c39bf045dfc68967740014a716089e9373baf80480f4f7866a4aaba54b335244db0fd2ba68bdd67e6da65c67201ec8262e82cd6994721cd033164ea530d3
6
+ metadata.gz: 61e50e05a2981477850b0f431d8dcfd6c50440829f6b2dd89e78bfe2e3367b38930a0ca7d1404b8fb29d9062aa6bd45da1d728bd4b1b685961843707b4da7413
7
+ data.tar.gz: 585da05068ae97d15b38247fdfe8c1625f97b18eddb8a4cdec8f3fd97cd310f4eccc06683a3dca703882189c0fb3d5449e597870213eee4a3649b4b18f69594f
data/README.md CHANGED
@@ -26,8 +26,8 @@ obj2 = registry.foo
26
26
  registry.singleton(:bar, lambda { Object.new })
27
27
 
28
28
  # obj1 and obj2 will be the same object
29
- obj1 = registry.foo
30
- obj2 = registry.foo
29
+ obj1 = registry.bar
30
+ obj2 = registry.bar
31
31
  ```
32
32
 
33
33
  ## Handling dependendencies within the registry
@@ -41,7 +41,7 @@ end
41
41
 
42
42
  class NotSoSimple
43
43
  def initialize(text, simple_first, simple_second)
44
- @text = test
44
+ @text = text
45
45
  @simple_first = simple_first
46
46
  @simple_second = simple_second
47
47
  end
@@ -34,7 +34,8 @@ module Racket
34
34
  # @param [Proc|nil] proc
35
35
  # @return [nil]
36
36
  def register(key, proc = nil, &block)
37
- key, proc, proc_args, = usable?(key, proc, block)
37
+ key, proc, proc_args, =
38
+ ClassMethods.validate_usable([key, self, proc, block, @resolved])
38
39
  singleton_class.instance_eval do
39
40
  define_method(key) { proc.call(*proc_args) }
40
41
  end && nil
@@ -50,7 +51,8 @@ module Racket
50
51
  # @param [Proc|nil] proc
51
52
  # @return [nil]
52
53
  def register_singleton(key, proc = nil, &block)
53
- key, proc, proc_args, resolved = usable?(key, proc, block)
54
+ key, proc, proc_args, resolved =
55
+ ClassMethods.validate_usable([key, self, proc, block, @resolved])
54
56
  singleton_class.instance_eval do
55
57
  define_method(key) do
56
58
  return resolved[key] if resolved.key?(key)
@@ -61,31 +63,36 @@ module Racket
61
63
 
62
64
  alias singleton register_singleton
63
65
 
64
- private
66
+ # Racket Registry class methods
67
+ module ClassMethods
68
+ # Validates that the parameters sent to the registry is usable.
69
+ #
70
+ # @param [Array] args
71
+ # @return [Array]
72
+ def self.validate_usable(args)
73
+ key, obj, proc, block, resolved = args
74
+ key = validate_key(key, obj)
75
+ proc = validate_proc(proc, block)
76
+ [key, proc, proc.arity.zero? ? [] : [obj], resolved]
77
+ end
65
78
 
66
- INVALID_KEYS =
67
- Object.public_methods.concat([:register, :register_singleton, :singleton])
68
- VALID_KEYS = /[\d\w\-\_]+/
79
+ def self.validate_key(key, obj)
80
+ sym = key.to_sym
81
+ insp = key.inspect
82
+ raise "Invalid key #{insp}" if
83
+ Racket::Registry.public_methods.include?(sym) ||
84
+ /^[a-z\_]{1}[\d\w\_]*$/ !~ sym
85
+ raise "Key #{insp} already registered" if obj.respond_to?(key)
86
+ sym
87
+ end
69
88
 
70
- def key?(key)
71
- key = key.to_sym
72
- klass = self.class
73
- raise 'Invalid key' if klass::INVALID_KEYS.include?(key) ||
74
- !klass::VALID_KEYS =~ key.to_s
75
- raise 'Key already registered' if respond_to?(key)
76
- key
77
- end
78
-
79
- def proc?(proc, block)
80
- return proc if proc.respond_to?(:call)
81
- return block if block.respond_to?(:call)
82
- raise 'No block given'
83
- end
89
+ def self.validate_proc(proc, block)
90
+ return proc if proc.respond_to?(:call)
91
+ return block if block.respond_to?(:call)
92
+ raise 'No proc/block given'
93
+ end
84
94
 
85
- def usable?(key, proc, block)
86
- key = key?(key)
87
- proc = proc?(proc, block)
88
- [key, proc, proc.arity.zero? ? [] : [self], @resolved]
95
+ private_class_method :validate_key, :validate_proc
89
96
  end
90
97
  end
91
98
  end
@@ -30,7 +30,7 @@ end
30
30
 
31
31
  require_relative '../lib/racket/registry.rb'
32
32
 
33
- describe 'Racket::Registry' do
33
+ describe 'Racket::Registry registration' do
34
34
  registry = Racket::Registry.new
35
35
 
36
36
  it 'should be able to register non-singleton entries using a proc' do
@@ -65,7 +65,61 @@ describe 'Racket::Registry' do
65
65
  obj1.object_id.should.equal(obj2.object_id)
66
66
  end
67
67
 
68
- it 'should block invalid entries' do
69
- -> { registry.register('invalid') }.should.raise(RuntimeError)
68
+ it 'should block invalid keys' do
69
+ -> { registry.register('inspect') }
70
+ .should.raise(RuntimeError)
71
+ .message.should.equal('Invalid key "inspect"')
72
+ end
73
+
74
+ it 'should block already registered keys' do
75
+ -> { registry.register('one', -> { Object.new }) }
76
+ .should.raise(RuntimeError)
77
+ .message.should.equal('Key "one" already registered')
78
+ end
79
+
80
+ it 'should block invalid block/procs' do
81
+ -> { registry.register('invalid') }
82
+ .should.raise(RuntimeError)
83
+ .message.should.equal('No proc/block given')
84
+ end
85
+ end
86
+
87
+ describe 'Racket::Registry dependency handling' do
88
+ class Simple
89
+ attr_reader :text
90
+
91
+ def initialize(text)
92
+ @text = text
93
+ end
94
+ end
95
+
96
+ class NotSoSimple
97
+ attr_reader :text, :simple_first, :simple_second
98
+
99
+ def initialize(text, simple_first, simple_second)
100
+ @text = text
101
+ @simple_first = simple_first
102
+ @simple_second = simple_second
103
+ end
104
+ end
105
+
106
+ it 'should be able to resolve dependencies regardless of registration order' do
107
+ registry = Racket::Registry.new
108
+
109
+ foo = Simple.new('foo')
110
+ bar = Simple.new('bar')
111
+
112
+ registry.singleton(
113
+ :baz,
114
+ lambda { |r| NotSoSimple.new('baz', r.foo, r.bar) }
115
+ )
116
+ registry.singleton(:bar, lambda { |r| bar })
117
+ registry.singleton(:foo, lambda { |r| foo })
118
+
119
+ baz = registry.baz
120
+ baz.simple_first.object_id.should.equal(foo.object_id)
121
+ baz.simple_second.object_id.should.equal(bar.object_id)
122
+ registry.foo.object_id.should.equal(foo.object_id)
123
+ registry.bar.object_id.should.equal(bar.object_id)
70
124
  end
71
125
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: racket-registry
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lars Olsson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-19 00:00:00.000000000 Z
11
+ date: 2016-06-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bacon