adapter-registry 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: eb5d6f841887d5b267b73b91cf09784d71037c6b
4
+ data.tar.gz: 26a72a8a54287a5a81bdf5a61277e70db25e0822
5
+ SHA512:
6
+ metadata.gz: 8ce7cdf9e0634da117e95f31249f42a0eff85fef8975d4925f17797326fe7e5308ba1efa7dd9745680f2a181437c546b9120ef843ea16d53cf53c344d9d1000d
7
+ data.tar.gz: b2a823568648950a46cbcd6239ad99d180bd21e18d4fbba3d590833e8cc1924dccf743c2b9a4f76543ad316633a9a89934c1ea8d3f7e8479cad38362f8940858
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright (c) 2013 RadarServices Smart IT-Security <gems@radarservices.com>
2
+
3
+ Permission to use, copy, modify, and/or distribute this software for any
4
+ purpose with or without fee is hereby granted, provided that the above
5
+ copyright notice and this permission notice appear in all copies.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
9
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
12
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13
+ PERFORMANCE OF THIS SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # Adapter Registry
2
+
3
+ provides a generic registry for traits and classes, where a class adapter can
4
+ be stored and recalled in the correct context.
5
+
6
+ ## Installation
7
+
8
+ ```
9
+ $ gem install adapter-registry
10
+ ```
11
+
12
+ ```ruby
13
+ require 'adapter-registry'
14
+ ```
15
+
16
+ or use ``gem 'adapter-registry'`` in your Gemfile when using bundler.
17
+
18
+ ##Examples
19
+
20
+ ### Implementing Adapters
21
+
22
+ ```ruby
23
+ require 'adapter-registry'
24
+
25
+ class SomeClass
26
+ end
27
+
28
+ class SomeOtherClass < SomeClass
29
+ end
30
+
31
+ class SomeAdapter
32
+
33
+ include AdapterRegistry::Implementation
34
+
35
+ implements :something # trait, defines the lookup context
36
+ adapts_instance SomeClass # stores the trait/class combination in the registry
37
+
38
+ #...
39
+
40
+ end
41
+
42
+ class SomeClassAdapter
43
+
44
+ include AdapterRegistry::Implementation
45
+
46
+ implements :something
47
+ adapts_class SomeClass # stores the trait/class or ancestor combination in the registry
48
+
49
+ #...
50
+
51
+ end
52
+ ```
53
+
54
+ ### Initialization
55
+
56
+ ```ruby
57
+ require 'adapter-registry'
58
+
59
+ AdapterRegistry.load_directories(['./lib/some_adapters', './lib/some_other_adapters'])
60
+ ```
61
+
62
+ The ``load`` section should be used in an initializer when using Rails. ``adapter-registry`` uses ``require_dependency`` in favour of ``require`` when included in a Rails context.
63
+
64
+ ### Usage
65
+
66
+ ```ruby
67
+ obj = SomeClass.new
68
+ adapter = AdapterRegistry.get(:something, obj)
69
+
70
+ class_adapter = AdapterRegistry.get(:something, SomeOtherClass)
71
+
72
+ some_other_instance = class_adapter.new # uses the same adapter because SomeClass is an ancestor
73
+ ```
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.join(File.dirname(__FILE__), 'lib', 'adapter-registry', 'version')
4
+
5
+ require 'date'
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "adapter-registry"
9
+ s.version = AdapterRegistry::VERSION
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+ s.authors = ["Andreas Kopecky <andreas.kopecky@radarservices.com>", "Anton Bangratz <anton.bangratz@radarservices.com>", "Martin Natano <martin.natano@radarservices.com"]
13
+ s.date = Date.today.strftime
14
+ s.description = "Simple adapter registry"
15
+ s.email = "gems [a] radarservices [d] com"
16
+ s.files = `git ls-files`.split("\n").reject { |file| file == '.gitignore' }
17
+ s.extra_rdoc_files = %w[LICENSE README.md]
18
+
19
+ s.homepage = "http://github.com/rs-dev/adapter-registry"
20
+ s.require_paths = ["lib"]
21
+ # s.rubygems_version = "1.8.24"
22
+ s.summary = "Simple adapter registry for enhancing classes via adapters"
23
+ s.license = "ISC"
24
+ end
@@ -0,0 +1,45 @@
1
+ module AdapterRegistry
2
+
3
+ module Implementation
4
+
5
+ def self.included(base)
6
+ base.extend(ClassMethods)
7
+ end
8
+
9
+ module ClassMethods
10
+ attr_accessor :traits
11
+ def implements(*traits)
12
+ self.traits = traits
13
+ end
14
+
15
+ def adapts_instance(*klasses)
16
+ adapts do |context|
17
+ klasses.any? { |klass| context.is_a?(klass) }
18
+ end
19
+ end
20
+
21
+ def adapts_class(*klasses)
22
+ adapts do |context|
23
+ context.is_a?(Class) && klass_is_a?(context, *klasses)
24
+ end
25
+ end
26
+
27
+ def adapts(&block)
28
+ self.traits.each do |trait|
29
+ Adapter.register(trait, self, &block)
30
+ end if (self.traits && block)
31
+ end
32
+
33
+ def instance_for(context)
34
+ self.new(context)
35
+ end
36
+
37
+ def klass_is_a?(klass, *klasses)
38
+ (klasses.map(&:to_s) & klass.ancestors.map(&:name)).any?
39
+ end
40
+
41
+ end
42
+
43
+ end
44
+
45
+ end
@@ -0,0 +1,27 @@
1
+ module AdapterRegistry
2
+
3
+ class Registry < Hash
4
+ def get_adapter(trait, context)
5
+ if trait_registry = self[trait]
6
+ trait_registry.select do |entry|
7
+ entry[:for].call(context)
8
+ end.first.try(:[], :adapter)
9
+ end
10
+ end
11
+
12
+ def get(trait, context)
13
+ if adapter = get_adapter(trait, context)
14
+ adapter.instance_for(context)
15
+ end
16
+ end
17
+
18
+ def set(trait, adapter, &block)
19
+ self[trait] ||= Set.new
20
+ self[trait] << {
21
+ adapter: adapter,
22
+ for: block,
23
+ }
24
+ end
25
+ end
26
+
27
+ end
@@ -0,0 +1,5 @@
1
+ module AdapterRegistry
2
+
3
+ VERSION = '0.0.1'
4
+
5
+ end
@@ -0,0 +1,34 @@
1
+ require 'adapter-registry/registry'
2
+ require 'adapter-registry/implementation'
3
+
4
+ module AdapterRegistry
5
+
6
+ def self.registry
7
+ @@registry ||= Registry.new
8
+ end
9
+
10
+ def self.get(trait, context)
11
+ registry.get(trait, context)
12
+ end
13
+
14
+ def self.register(trait, adapter, &block)
15
+ registry.set(trait, adapter, &block)
16
+ end
17
+
18
+ def self.load_directory(path)
19
+ Dir[path].each do |file|
20
+ if defined?(Rails)
21
+ require_dependency(file)
22
+ else
23
+ require(file)
24
+ end
25
+ end
26
+ end
27
+
28
+ def self.load_directories(paths)
29
+ paths.each do |path|
30
+ self.load_directory(path)
31
+ end
32
+ end
33
+
34
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: adapter-registry
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andreas Kopecky <andreas.kopecky@radarservices.com>
8
+ - Anton Bangratz <anton.bangratz@radarservices.com>
9
+ - Martin Natano <martin.natano@radarservices.com
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-07-10 00:00:00.000000000 Z
14
+ dependencies: []
15
+ description: Simple adapter registry
16
+ email: gems [a] radarservices [d] com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files:
20
+ - LICENSE
21
+ - README.md
22
+ files:
23
+ - LICENSE
24
+ - README.md
25
+ - adapter-registry.gemspec
26
+ - lib/adapter-registry.rb
27
+ - lib/adapter-registry/implementation.rb
28
+ - lib/adapter-registry/registry.rb
29
+ - lib/adapter-registry/version.rb
30
+ homepage: http://github.com/rs-dev/adapter-registry
31
+ licenses:
32
+ - ISC
33
+ metadata: {}
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 2.0.3
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: Simple adapter registry for enhancing classes via adapters
54
+ test_files: []