registrable 0.1.0 → 0.2.0

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: c29679b74ce08b4f0ec4d4209b8237640d41d4fd
4
- data.tar.gz: 76d0958c355a06fe83b375441a9fe775b96ec172
3
+ metadata.gz: db1c9da7835bba2bfb17418c3b5847a28164a7e7
4
+ data.tar.gz: e6b10644ff0365a2093f586ee84f219d321ad1ea
5
5
  SHA512:
6
- metadata.gz: 8680da778b1c30dcc46387b386294970b42121ffcc0d5b9bfdd876883b1a99efc2c359fd26eedbf2468bffb4184eee7ab334b8e86394e5b269b6503af218647c
7
- data.tar.gz: 19e9d6df57708f87593a60cce771f75f60b4686246194a9993a812e51cb8396c0c1acbe8ae7c4c90175bb30712201e8840ed5e0af62eb144174f1801aa66f88b
6
+ metadata.gz: e36aca6480903518c0e190a6cca6094aa4dfe42f46647d9a1ce74f5670ffdfeb6d247e4933c5de07c05b6120f1099fe36b6d442126e3bebb987058e071cd4176
7
+ data.tar.gz: 2576bfb84a0d318b701a09e8e5512bb1a2ea7ed9305e6104bf3eeec0044f7797b8c01aae89dd80e6dd36153f3503e5aa156abbf15e42433ef37cb115587240a8
data/Guardfile CHANGED
@@ -18,7 +18,10 @@ guard :rubocop, all_on_start: false, cli: '--config .rubocop.yml --auto-correct
18
18
  end
19
19
 
20
20
  guard :yard, server: false do
21
- watch(/^(README|LICENSE|CODE_OF_CONDUCT)/)
22
21
  watch(%r{lib\/.+\.rb})
23
22
  watch(/.yardopts/)
24
23
  end
24
+
25
+ guard :shell do
26
+ watch(%r{templates/(.+)\.erb}) { |match| `rake #{match[1]}` }
27
+ end
data/README.md CHANGED
@@ -6,14 +6,51 @@
6
6
 
7
7
  ### RubyGems: `gem install registrable`
8
8
 
9
- ## Usage
9
+ ## Examples
10
+
11
+ ### Plugin
12
+
13
+ ```rb
14
+ require 'registrable'
15
+
16
+ module Plugin
17
+
18
+ extend Registrable
19
+
20
+ class Base
21
+ def self.inherited(subclass)
22
+ identifier = subclass.to_s
23
+ .split('::').last
24
+ .gsub(/([a-z0-9])([A-Z])/, '\1_\2')
25
+ .downcase.to_sym
26
+
27
+ Plugin.register(identifier, subclass)
28
+ end
29
+ end
30
+
31
+ end
32
+
33
+ class MyPlugin < Plugin::Base; end
34
+ class AnotherPlugin < Plugin::Base; end
35
+ ```
36
+
37
+ ```rb
38
+ p Plugin.registry.keys # => [:my_plugin, :another_plugin]
39
+ p Plugin[:my_plugin] == MyPlugin # => true
40
+ p Plugin[:another_plugin] == AnotherPlugin # => true
41
+ ```
42
+
43
+ ### Role
10
44
 
11
45
  ```rb
12
46
  require 'registrable'
13
47
 
14
48
  class Role
49
+
50
+ extend Registrable
51
+
15
52
  def initialize(read, modify)
16
- @read, @modify = create, modify
53
+ @read, @modify = read, modify
17
54
  end
18
55
 
19
56
  def read?
@@ -23,10 +60,10 @@ class Role
23
60
  def modify?
24
61
  @modify
25
62
  end
63
+
26
64
  end
27
65
 
28
66
  class User
29
- extend Registrable
30
67
 
31
68
  def initialize(role)
32
69
  self.role = role
@@ -35,15 +72,16 @@ class User
35
72
  attr_reader :role
36
73
 
37
74
  def role=(role)
38
- role = Role.registry[identifier] || Role.registry[:other] unless role.is_a?(Role)
75
+ role = Role.registry[role] || Role.registry[:other] unless role.is_a?(Role)
39
76
 
40
77
  @role = role
41
78
  end
79
+
42
80
  end
43
81
 
44
- Role.register(:other, false, false)
45
- Role.register(:user, true, false)
46
- Role.register(:admin, true, true)
82
+ Role.register(:other, Role.new(false, false))
83
+ Role.register(:user, Role.new(true, false))
84
+ Role.register(:admin, Role.new(true, true))
47
85
  ```
48
86
 
49
87
  ```rb
@@ -79,7 +117,7 @@ p admin.role.modifiable? # => true
79
117
 
80
118
  ## Contributing
81
119
 
82
- Bug reports and pull requests are welcome on GitHub at https://github.com/RyanScottLewis/striker.
120
+ Bug reports and pull requests are welcome on GitHub at https://github.com/RyanScottLewis/registrable.
83
121
 
84
122
  This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
85
123
 
data/Rakefile CHANGED
@@ -14,9 +14,8 @@ RuboCop::RakeTask.new(:lint)
14
14
  YARD::Rake::YardocTask.new(:docs)
15
15
 
16
16
  TEMPLATES_PATH = Pathname.new('templates')
17
- TEMPLATE_PATHS = FileList[TEMPLATES_PATH.join('**', '*').to_s]
18
- README_INPUT = TEMPLATES_PATH.join('README.md.erb')
19
- README_OUTPUT = Pathname.new('README.md')
17
+ TEMPLATE_PATHS = Pathname.glob(TEMPLATES_PATH.join('**', '*').to_s)
18
+ template_output_paths = []
20
19
 
21
20
  class RenderContext
22
21
 
@@ -25,7 +24,8 @@ class RenderContext
25
24
  end
26
25
 
27
26
  def initialize(path)
28
- @erb = ERB.new(path.read)
27
+ @path = path
28
+ @erb = ERB.new(@path.read)
29
29
  end
30
30
 
31
31
  def file(path)
@@ -35,13 +35,23 @@ class RenderContext
35
35
  def render(path)
36
36
  contents = @erb.result(binding)
37
37
 
38
- README_OUTPUT.open('w+') { |file| file.write(contents) }
38
+ path.open('w+') { |file| file.write(contents) }
39
+
40
+ puts "Rendered #{@path} to #{path}"
39
41
  end
40
42
 
41
43
  end
42
44
 
43
- file README_OUTPUT => [README_INPUT, TEMPLATE_PATHS] do
44
- RenderContext.render(README_INPUT, README_OUTPUT)
45
+ TEMPLATE_PATHS.each do |input_path|
46
+ output_path = input_path.sub('templates/', '').sub_ext('')
47
+ template_output_paths << output_path.to_s
48
+
49
+ file output_path => input_path do
50
+ RenderContext.render(input_path, output_path)
51
+ end
45
52
  end
46
53
 
47
- task default: [:spec, 'lint:auto_correct', :docs, README_OUTPUT]
54
+ desc 'Render templates'
55
+ task templates: template_output_paths
56
+
57
+ task default: [:spec, 'lint:auto_correct', :templates, :docs]
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -1,21 +1,21 @@
1
1
  require 'registrable'
2
2
 
3
- class Plugin
3
+ module Plugin
4
4
 
5
5
  extend Registrable
6
6
 
7
- def initialize(name)
8
- @name = name
9
- end
7
+ class Base
8
+ def self.inherited(subclass)
9
+ identifier = subclass.to_s
10
+ .split('::').last
11
+ .gsub(/([a-z0-9])([A-Z])/, '\1_\2')
12
+ .downcase.to_sym
10
13
 
11
- attr_reader :name
14
+ Plugin.register(identifier, subclass)
15
+ end
16
+ end
12
17
 
13
18
  end
14
19
 
15
- unless ENV['RSPEC'] # Not loading this file from tests
16
- p Plugin.register(:my_plugin, 'My Plugin') # => #<Plugin:0x0001 @name="My Plugin">
17
- p Plugin.registered?(:my_plugin) # => true
18
- p Plugin.registry[:my_plugin] # => #<Plugin:0x0001 @name="My Plugin">
19
- p Plugin.unregister(:my_plugin) # => #<Plugin:0x0001 @name="My Plugin">
20
- p Plugin.registered?(:my_plugin) # => false
21
- end
20
+ class MyPlugin < Plugin::Base; end
21
+ class AnotherPlugin < Plugin::Base; end
@@ -1,22 +1,25 @@
1
1
  require 'registrable'
2
2
 
3
3
  class Role
4
+
4
5
  extend Registrable
5
6
 
6
- def initialize(readable, modifiable)
7
- @readable, @modifiable = readable, modifiable
7
+ def initialize(read, modify)
8
+ @read, @modify = read, modify
8
9
  end
9
10
 
10
11
  def readable?
11
- @readable
12
+ @read
12
13
  end
13
14
 
14
15
  def modifiable?
15
- @modifiable
16
+ @modify
16
17
  end
18
+
17
19
  end
18
20
 
19
21
  class User
22
+
20
23
  def initialize(role)
21
24
  self.role = role
22
25
  end
@@ -28,8 +31,9 @@ class User
28
31
 
29
32
  @role = role
30
33
  end
34
+
31
35
  end
32
36
 
33
- Role.register(:other, false, false)
34
- Role.register(:user, true, false)
35
- Role.register(:admin, true, true)
37
+ Role.register(:other, Role.new(false, false))
38
+ Role.register(:user, Role.new(true, false))
39
+ Role.register(:admin, Role.new(true, true))
@@ -1,46 +1,44 @@
1
1
  require 'registrable/version'
2
2
 
3
- # Allow a class to hold a registry of instances
3
+ # Allow a class to hold a registry, which is a frozen Hash which can only be modified using the #register and
4
+ # #unregister methods
4
5
  module Registrable
5
6
 
6
- # All registered instances
7
+ # All registered objects
7
8
  #
8
9
  # @return [Hash]
9
10
  def registry
10
11
  @registry ||= {}.freeze
11
12
  end
12
13
 
13
- # Register an instance
14
+ # Register an object
14
15
  #
15
16
  # @param [Object] identifier
16
- # @param [Array] arguments The arguments to pass to #initialize
17
- # @param [Proc] block The block to pass to #initialize
18
- # @return [Object] The instance
19
- def register(identifier, *arguments, &block)
17
+ # @param [Class] registrant The object to register
18
+ # @return [Class] The registrant
19
+ def register(identifier, registrant)
20
20
  @registry = @registry ? registry.dup : {}
21
21
 
22
- instance = new(*arguments, &block)
23
-
24
- @registry[identifier] = instance
22
+ @registry[identifier] = registrant
25
23
  @registry.freeze
26
24
 
27
- instance
25
+ registrant
28
26
  end
29
27
 
30
- # Unregister an instance
28
+ # Unregister an object
31
29
  #
32
30
  # @param [Object] identifier
33
- # @return [Object] The instance
31
+ # @return [Object] The registrant
34
32
  def unregister(identifier)
35
33
  @registry = @registry ? registry.dup : {}
36
34
 
37
- instance = @registry.delete(identifier)
35
+ registrant = @registry.delete(identifier)
38
36
  @registry.freeze
39
37
 
40
- instance
38
+ registrant
41
39
  end
42
40
 
43
- # Get whether an instance is registered
41
+ # Get whether an object is registered
44
42
  #
45
43
  # @param [Object] identifier
46
44
  # @return [Boolean]
@@ -10,8 +10,8 @@ Gem::Specification.new do |spec|
10
10
  spec.authors = ['Ryan Scott Lewis']
11
11
  spec.email = ['ryanscottlewis@gmail.com']
12
12
 
13
- spec.summary = "Allows a class to hold a registry of it's instances"
14
- spec.description = "Extend in a class to allow it to hold a registry of it's instances"
13
+ spec.summary = 'Allow a class to hold a registry'
14
+ spec.description = 'Allow a class to hold a registry, which is a frozen Hash which can only be modified using the #register and #unregister methods'
15
15
  spec.homepage = 'https://github.com/RyanScottLewis/registrable'
16
16
  spec.license = 'MIT'
17
17
 
@@ -26,6 +26,7 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency 'fuubar', '~> 2.2'
27
27
  spec.add_development_dependency 'guard-rubocop', '~> 1.2'
28
28
  spec.add_development_dependency 'guard-rspec', '~> 4.7'
29
+ spec.add_development_dependency 'guard-shell', '~> 0.7.1'
29
30
  spec.add_development_dependency 'guard-yard', '~> 2.2'
30
31
  spec.add_development_dependency 'rake', '~> 10.0'
31
32
  spec.add_development_dependency 'rspec', '~> 3.0'
@@ -6,15 +6,41 @@
6
6
 
7
7
  ### RubyGems: `gem install registrable`
8
8
 
9
- ## Usage
9
+ ## Examples
10
+
11
+ ### Plugin
12
+
13
+ ```rb
14
+ <%= file('examples/plugin.rb') %>
15
+ ```
16
+
17
+ ```rb
18
+ p Plugin.registry.keys # => [:my_plugin, :another_plugin]
19
+ p Plugin[:my_plugin] == MyPlugin # => true
20
+ p Plugin[:another_plugin] == AnotherPlugin # => true
21
+ ```
22
+
23
+ ### Role
10
24
 
11
25
  ```rb
12
26
  <%= file('examples/role.rb') %>
13
27
  ```
14
28
 
15
29
  ```rb
30
+ other = User.new(:other)
31
+
32
+ p other.role.readable? # => true
33
+ p other.role.modifiable? # => true
34
+
35
+ user = User.new(:user)
36
+
37
+ p admin.role.readable? # => true
38
+ p admin.role.modifiable? # => false
39
+
16
40
  admin = User.new(:admin)
17
- p admin.role.modify? unless ENV["RSPEC"] # => true
41
+
42
+ p admin.role.readable? # => true
43
+ p admin.role.modifiable? # => true
18
44
  ```
19
45
 
20
46
  ## Development
@@ -33,7 +59,7 @@ p admin.role.modify? unless ENV["RSPEC"] # => true
33
59
 
34
60
  ## Contributing
35
61
 
36
- Bug reports and pull requests are welcome on GitHub at https://github.com/RyanScottLewis/striker.
62
+ Bug reports and pull requests are welcome on GitHub at https://github.com/RyanScottLewis/registrable.
37
63
 
38
64
  This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
39
65
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: registrable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Scott Lewis
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-04-21 00:00:00.000000000 Z
11
+ date: 2017-04-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '4.7'
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard-shell
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.7.1
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.7.1
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: guard-yard
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -150,7 +164,8 @@ dependencies:
150
164
  - - "~>"
151
165
  - !ruby/object:Gem::Version
152
166
  version: '0.9'
153
- description: Extend in a class to allow it to hold a registry of it's instances
167
+ description: 'Allow a class to hold a registry, which is a frozen Hash which can only
168
+ be modified using the #register and #unregister methods'
154
169
  email:
155
170
  - ryanscottlewis@gmail.com
156
171
  executables: []
@@ -202,5 +217,5 @@ rubyforge_project:
202
217
  rubygems_version: 2.6.11
203
218
  signing_key:
204
219
  specification_version: 4
205
- summary: Allows a class to hold a registry of it's instances
220
+ summary: Allow a class to hold a registry
206
221
  test_files: []