gluer 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +19 -0
- data/.ruby-version +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/bin/autospec +16 -0
- data/bin/coderay +16 -0
- data/bin/htmldiff +16 -0
- data/bin/ldiff +16 -0
- data/bin/pry +16 -0
- data/bin/rake +16 -0
- data/bin/rdebug +16 -0
- data/bin/rspec +16 -0
- data/gluer.gemspec +26 -0
- data/lib/gluer/api.rb +30 -0
- data/lib/gluer/configuration.rb +38 -0
- data/lib/gluer/dsl.rb +39 -0
- data/lib/gluer/file.rb +55 -0
- data/lib/gluer/file_pool.rb +37 -0
- data/lib/gluer/ordered_set.rb +23 -0
- data/lib/gluer/registration.rb +51 -0
- data/lib/gluer/registration_collection.rb +31 -0
- data/lib/gluer/registration_definition.rb +21 -0
- data/lib/gluer/registration_hook.rb +13 -0
- data/lib/gluer/registration_pool.rb +32 -0
- data/lib/gluer/version.rb +3 -0
- data/lib/gluer.rb +2 -0
- data/spec/integration/registration_spec.rb +133 -0
- data/spec/spec_helper.rb +2 -0
- metadata +172 -0
data/.gitignore
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-1.9.3
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 rcabralc
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Gluer
|
2
|
+
|
3
|
+
A configuration reloader lib.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'gluer'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install gluer
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/autospec
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'autospec' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('rspec-core', 'autospec')
|
data/bin/coderay
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'coderay' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('gluer', 'coderay')
|
data/bin/htmldiff
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'htmldiff' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('gluer', 'htmldiff')
|
data/bin/ldiff
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'ldiff' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('gluer', 'ldiff')
|
data/bin/pry
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'pry' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('pry', 'pry')
|
data/bin/rake
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'rake' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('gluer', 'rake')
|
data/bin/rdebug
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'rdebug' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('gluer', 'rdebug')
|
data/bin/rspec
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'rspec' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('rspec-core', 'rspec')
|
data/gluer.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'gluer/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "gluer"
|
8
|
+
spec.version = Gluer::VERSION
|
9
|
+
spec.authors = ["rcabralc"]
|
10
|
+
spec.email = ["rcabralc@gmail.com"]
|
11
|
+
spec.description = %q{Configuration reloader}
|
12
|
+
spec.summary = %q{Reloads configuration code in a per-file basis}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "pry-debugger"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "simplecov"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
end
|
data/lib/gluer/api.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require "gluer/file_pool"
|
2
|
+
require "gluer/registration_collection"
|
3
|
+
|
4
|
+
module Gluer
|
5
|
+
class << self
|
6
|
+
def setup(context=nil, &block)
|
7
|
+
path = block.binding.eval('__FILE__')
|
8
|
+
file = file_pool.get(path)
|
9
|
+
collect_registrations(context, block) do |registration|
|
10
|
+
file.add_registration(registration)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def reload
|
15
|
+
file_pool.update
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def collect_registrations(context, block)
|
21
|
+
RegistrationCollection.new(context, block).each do |registration|
|
22
|
+
yield(registration)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def file_pool
|
27
|
+
@file_pool ||= FilePool.new
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Gluer
|
2
|
+
def self.config
|
3
|
+
@configuration ||= Configuration.new
|
4
|
+
end
|
5
|
+
|
6
|
+
def self.configure
|
7
|
+
yield(config)
|
8
|
+
end
|
9
|
+
|
10
|
+
class Configuration
|
11
|
+
attr_accessor :base_path
|
12
|
+
attr_accessor :file_loader
|
13
|
+
attr_accessor :file_filter
|
14
|
+
attr_accessor :magic_signature
|
15
|
+
|
16
|
+
def initialize(options={})
|
17
|
+
@base_path = options.fetch(:base_path, '.')
|
18
|
+
@file_loader = options.fetch(:file_loader, Proc.new { |f| load(f) })
|
19
|
+
@file_filter = options.fetch(:file_filter, default_file_filter)
|
20
|
+
@magic_signature = options.fetch(:magic_signature, default_signature)
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def default_signature
|
26
|
+
"#{self.class.name.split('::').first}.setup"
|
27
|
+
end
|
28
|
+
|
29
|
+
def default_file_filter
|
30
|
+
Proc.new do |base_path, magic_signature|
|
31
|
+
output = %x{cd '#{base_path}' && grep -IlFr '#{magic_signature}' --exclude-dir 'spec' .}
|
32
|
+
output.lines.map do |line|
|
33
|
+
::File.expand_path(line.chomp, base_path)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/lib/gluer/dsl.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require "gluer/registration"
|
2
|
+
require "gluer/registration_definition"
|
3
|
+
|
4
|
+
module Gluer
|
5
|
+
def self.define_registration(name)
|
6
|
+
definition = RegistrationDefinition.new(name)
|
7
|
+
yield definition
|
8
|
+
DSL.add_registration_definition(name, definition)
|
9
|
+
end
|
10
|
+
|
11
|
+
class DSL
|
12
|
+
class << self
|
13
|
+
def add_registration_definition(name, definition)
|
14
|
+
defined_registrations[name] = definition
|
15
|
+
|
16
|
+
define_method(name) do |*args, &block|
|
17
|
+
definition = DSL.get_registration_definition(name)
|
18
|
+
registration = Registration.new(definition, @context, args, block)
|
19
|
+
@registration_collection.add(registration)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def get_registration_definition(name)
|
24
|
+
defined_registrations.fetch(name)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def defined_registrations
|
30
|
+
@defined_registrations ||= {}
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def initialize(context, registration_collection)
|
35
|
+
@context = context
|
36
|
+
@registration_collection = registration_collection
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/gluer/file.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require "gluer/configuration"
|
2
|
+
require "gluer/registration_pool"
|
3
|
+
|
4
|
+
module Gluer
|
5
|
+
class File
|
6
|
+
attr_reader :path
|
7
|
+
|
8
|
+
def initialize(path)
|
9
|
+
@path = path
|
10
|
+
end
|
11
|
+
|
12
|
+
def touch
|
13
|
+
@touched = true
|
14
|
+
end
|
15
|
+
|
16
|
+
def touched?
|
17
|
+
!!@touched
|
18
|
+
end
|
19
|
+
|
20
|
+
def add_registration(registration)
|
21
|
+
new_registration_pool.add(registration)
|
22
|
+
touch
|
23
|
+
end
|
24
|
+
|
25
|
+
def rollback_all
|
26
|
+
registration_pool.rollback
|
27
|
+
registration_pool.clear
|
28
|
+
end
|
29
|
+
|
30
|
+
def reload
|
31
|
+
load_from_source
|
32
|
+
if touched?
|
33
|
+
registration_pool.rollback
|
34
|
+
new_registration_pool.commit
|
35
|
+
registration_pool.replace(new_registration_pool)
|
36
|
+
new_registration_pool.clear
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def registration_pool
|
43
|
+
@registration_pool ||= RegistrationPool.new
|
44
|
+
end
|
45
|
+
|
46
|
+
def new_registration_pool
|
47
|
+
@new_registration_pool ||= RegistrationPool.new
|
48
|
+
end
|
49
|
+
|
50
|
+
def load_from_source
|
51
|
+
@touched = false
|
52
|
+
Gluer.config.file_loader.call(path)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'gluer/configuration'
|
2
|
+
require 'gluer/file'
|
3
|
+
require 'gluer/ordered_set'
|
4
|
+
|
5
|
+
module Gluer
|
6
|
+
class FilePool
|
7
|
+
def initialize
|
8
|
+
clear
|
9
|
+
end
|
10
|
+
|
11
|
+
def get(path)
|
12
|
+
files.detect { |file| file.path == path }
|
13
|
+
end
|
14
|
+
|
15
|
+
def clear
|
16
|
+
@files = OrderedSet.new
|
17
|
+
end
|
18
|
+
|
19
|
+
def update
|
20
|
+
new_files = OrderedSet.new(collect)
|
21
|
+
diff = files - new_files
|
22
|
+
diff.each(&:rollback_all)
|
23
|
+
self.files = new_files
|
24
|
+
files.each(&:reload)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
attr_accessor :files
|
29
|
+
|
30
|
+
def collect
|
31
|
+
base_path = Gluer.config.base_path
|
32
|
+
signature = Gluer.config.magic_signature
|
33
|
+
filtered_file_paths = Gluer.config.file_filter.call(base_path, signature)
|
34
|
+
filtered_file_paths.map { |path| File.new(path) }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Gluer
|
2
|
+
class OrderedSet
|
3
|
+
include Enumerable
|
4
|
+
|
5
|
+
def initialize(initial=[])
|
6
|
+
@collection = initial
|
7
|
+
end
|
8
|
+
|
9
|
+
def each
|
10
|
+
@collection.each { |item| yield(item) }
|
11
|
+
end
|
12
|
+
|
13
|
+
def add(new)
|
14
|
+
@collection.delete_if { |existing| existing == new }
|
15
|
+
@collection << new
|
16
|
+
end
|
17
|
+
|
18
|
+
def -(other)
|
19
|
+
raise ArgumentError unless other.is_a?(OrderedSet)
|
20
|
+
self.class.new(@collection - other.instance_eval { @collection })
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require "gluer/registration_hook"
|
2
|
+
|
3
|
+
module Gluer
|
4
|
+
class Registration
|
5
|
+
attr_accessor :name
|
6
|
+
|
7
|
+
def initialize(definition, context, args, block)
|
8
|
+
@definition = definition
|
9
|
+
@context = context
|
10
|
+
@args = args
|
11
|
+
@block = block
|
12
|
+
@committed = false
|
13
|
+
end
|
14
|
+
|
15
|
+
def commit
|
16
|
+
commit_hook.call(registry, context, *args, &block)
|
17
|
+
committed
|
18
|
+
end
|
19
|
+
|
20
|
+
def on_rollback(&hook)
|
21
|
+
@rollback_hook = RegistrationHook.new(hook)
|
22
|
+
end
|
23
|
+
|
24
|
+
def rollback
|
25
|
+
rollback_hook.call(registry, context, *args, &block) if committed?
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
attr_reader :definition, :context, :args, :block
|
30
|
+
|
31
|
+
def committed
|
32
|
+
@committed = true
|
33
|
+
end
|
34
|
+
|
35
|
+
def committed?
|
36
|
+
@committed
|
37
|
+
end
|
38
|
+
|
39
|
+
def commit_hook
|
40
|
+
definition.commit_hook
|
41
|
+
end
|
42
|
+
|
43
|
+
def rollback_hook
|
44
|
+
definition.rollback_hook
|
45
|
+
end
|
46
|
+
|
47
|
+
def registry
|
48
|
+
definition.registry_factory.call
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require "gluer/dsl"
|
2
|
+
|
3
|
+
module Gluer
|
4
|
+
class RegistrationCollection
|
5
|
+
def initialize(context, block)
|
6
|
+
@context = context
|
7
|
+
@block = block
|
8
|
+
end
|
9
|
+
|
10
|
+
def each
|
11
|
+
registrations.clear
|
12
|
+
dsl.instance_eval(&block)
|
13
|
+
registrations.each { |registration| yield(registration) }
|
14
|
+
end
|
15
|
+
|
16
|
+
def add(registration)
|
17
|
+
registrations << registration
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
attr_reader :context, :block
|
22
|
+
|
23
|
+
def registrations
|
24
|
+
@registrations ||= []
|
25
|
+
end
|
26
|
+
|
27
|
+
def dsl
|
28
|
+
@dsl ||= DSL.new(context, self)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Gluer
|
2
|
+
class RegistrationDefinition
|
3
|
+
def initialize(name)
|
4
|
+
@name = name
|
5
|
+
end
|
6
|
+
|
7
|
+
def on_commit(&block)
|
8
|
+
@commit_hook = RegistrationHook.new(block)
|
9
|
+
end
|
10
|
+
|
11
|
+
def on_rollback(&block)
|
12
|
+
@rollback_hook = RegistrationHook.new(block)
|
13
|
+
end
|
14
|
+
|
15
|
+
def registry(&block)
|
16
|
+
@registry_factory = block
|
17
|
+
end
|
18
|
+
|
19
|
+
attr_reader :commit_hook, :rollback_hook, :registry_factory
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'gluer/ordered_set'
|
2
|
+
|
3
|
+
module Gluer
|
4
|
+
class RegistrationPool
|
5
|
+
def initialize
|
6
|
+
clear
|
7
|
+
end
|
8
|
+
|
9
|
+
def commit
|
10
|
+
registrations.each(&:commit)
|
11
|
+
end
|
12
|
+
|
13
|
+
def rollback
|
14
|
+
registrations.each(&:rollback)
|
15
|
+
end
|
16
|
+
|
17
|
+
def add(registration)
|
18
|
+
registrations.add(registration)
|
19
|
+
end
|
20
|
+
|
21
|
+
def replace(registration_pool)
|
22
|
+
@registrations = registration_pool.registrations
|
23
|
+
end
|
24
|
+
|
25
|
+
def clear
|
26
|
+
@registrations = OrderedSet.new
|
27
|
+
end
|
28
|
+
|
29
|
+
protected
|
30
|
+
attr_reader :registrations
|
31
|
+
end
|
32
|
+
end
|
data/lib/gluer.rb
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
require 'gluer'
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe "Default usage" do
|
5
|
+
before(:all) do
|
6
|
+
class RegistryClass
|
7
|
+
def add(context, foo, bar, &block)
|
8
|
+
registration = reg_class.new(context, foo, bar, block)
|
9
|
+
registrations << registration
|
10
|
+
end
|
11
|
+
|
12
|
+
def del(context, foo, bar, block)
|
13
|
+
to_delete = reg_class.new(context, foo, bar, block)
|
14
|
+
registrations.delete_if do |registration|
|
15
|
+
registration.context == to_delete.context &&
|
16
|
+
registration.foo == to_delete.foo &&
|
17
|
+
registration.bar == to_delete.bar
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def run(context, foo, bar)
|
22
|
+
get(context, foo, bar).each do |registration|
|
23
|
+
registration.block.call(context)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def get(context, foo, bar)
|
28
|
+
registrations.select do |registration|
|
29
|
+
registration.context == context &&
|
30
|
+
registration.foo == foo &&
|
31
|
+
registration.bar == bar
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def registrations
|
36
|
+
@registrations ||= []
|
37
|
+
end
|
38
|
+
|
39
|
+
def reg_class
|
40
|
+
Struct.new(:context, :foo, :bar, :block)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
Registry = RegistryClass.new
|
45
|
+
|
46
|
+
Gluer.define_registration(:smart_registration) do |registration|
|
47
|
+
registration.on_commit do |reg, context, foo, bar, &block|
|
48
|
+
reg.add(context, foo, bar, &block)
|
49
|
+
end
|
50
|
+
registration.on_rollback do |reg, context, foo, bar, &block|
|
51
|
+
reg.del(context, foo, bar, block)
|
52
|
+
end
|
53
|
+
registration.registry { Registry }
|
54
|
+
end
|
55
|
+
|
56
|
+
f = File.open("temp_code.rb", "w")
|
57
|
+
f.write(<<-CODE)
|
58
|
+
require 'gluer'
|
59
|
+
Context = Object.new
|
60
|
+
Gluer.setup(Context) do
|
61
|
+
smart_registration 'foo', 'bar' do |target|
|
62
|
+
target.registration_ran
|
63
|
+
end
|
64
|
+
end
|
65
|
+
CODE
|
66
|
+
f.close
|
67
|
+
end
|
68
|
+
|
69
|
+
after(:all) do
|
70
|
+
Object.send(:remove_const, :Registry)
|
71
|
+
Object.send(:remove_const, :RegistryClass)
|
72
|
+
File.unlink('temp_code.rb')
|
73
|
+
end
|
74
|
+
|
75
|
+
after do
|
76
|
+
Object.send(:remove_const, :Context) if defined?(Context)
|
77
|
+
end
|
78
|
+
|
79
|
+
it "registers stuff within registries" do
|
80
|
+
Gluer.reload
|
81
|
+
Context.should_receive(:registration_ran)
|
82
|
+
Registry.run(Context, 'foo', 'bar')
|
83
|
+
end
|
84
|
+
|
85
|
+
context "when the file changes and is reloaded" do
|
86
|
+
before do
|
87
|
+
Gluer.reload
|
88
|
+
|
89
|
+
f = File.open("temp_code.rb", "w")
|
90
|
+
f.write(<<-CODE)
|
91
|
+
require 'gluer'
|
92
|
+
Context = Object.new
|
93
|
+
Gluer.setup(Context) do
|
94
|
+
smart_registration 'foo', 'bar' do |target|
|
95
|
+
target.new_registration_ran
|
96
|
+
end
|
97
|
+
end
|
98
|
+
CODE
|
99
|
+
f.close
|
100
|
+
|
101
|
+
Object.send(:remove_const, :Context)
|
102
|
+
end
|
103
|
+
|
104
|
+
it "reloads the registration, replacing the old one" do
|
105
|
+
Gluer.reload
|
106
|
+
|
107
|
+
Context.should_not_receive(:registration_ran)
|
108
|
+
Context.should_receive(:new_registration_ran)
|
109
|
+
|
110
|
+
Registry.run(Context, 'foo', 'bar')
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
context "when the file changes and is reloaded without any registration" do
|
115
|
+
before do
|
116
|
+
Gluer.reload
|
117
|
+
|
118
|
+
f = File.open("temp_code.rb", "w")
|
119
|
+
f.write(<<-CODE)
|
120
|
+
require 'gluer'
|
121
|
+
CODE
|
122
|
+
f.close
|
123
|
+
|
124
|
+
@old_context = Object::Context
|
125
|
+
Object.send(:remove_const, :Context)
|
126
|
+
end
|
127
|
+
|
128
|
+
it "reloads the file and unregisters the old registration" do
|
129
|
+
Gluer.reload
|
130
|
+
Registry.get(@old_context, 'foo', 'bar').should be_empty
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,172 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gluer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- rcabralc
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-29 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: pry-debugger
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: simplecov
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rake
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
description: Configuration reloader
|
95
|
+
email:
|
96
|
+
- rcabralc@gmail.com
|
97
|
+
executables:
|
98
|
+
- autospec
|
99
|
+
- coderay
|
100
|
+
- htmldiff
|
101
|
+
- ldiff
|
102
|
+
- pry
|
103
|
+
- rake
|
104
|
+
- rdebug
|
105
|
+
- rspec
|
106
|
+
extensions: []
|
107
|
+
extra_rdoc_files: []
|
108
|
+
files:
|
109
|
+
- .gitignore
|
110
|
+
- .ruby-version
|
111
|
+
- Gemfile
|
112
|
+
- LICENSE.txt
|
113
|
+
- README.md
|
114
|
+
- Rakefile
|
115
|
+
- bin/autospec
|
116
|
+
- bin/coderay
|
117
|
+
- bin/htmldiff
|
118
|
+
- bin/ldiff
|
119
|
+
- bin/pry
|
120
|
+
- bin/rake
|
121
|
+
- bin/rdebug
|
122
|
+
- bin/rspec
|
123
|
+
- gluer.gemspec
|
124
|
+
- lib/gluer.rb
|
125
|
+
- lib/gluer/api.rb
|
126
|
+
- lib/gluer/configuration.rb
|
127
|
+
- lib/gluer/dsl.rb
|
128
|
+
- lib/gluer/file.rb
|
129
|
+
- lib/gluer/file_pool.rb
|
130
|
+
- lib/gluer/ordered_set.rb
|
131
|
+
- lib/gluer/registration.rb
|
132
|
+
- lib/gluer/registration_collection.rb
|
133
|
+
- lib/gluer/registration_definition.rb
|
134
|
+
- lib/gluer/registration_hook.rb
|
135
|
+
- lib/gluer/registration_pool.rb
|
136
|
+
- lib/gluer/version.rb
|
137
|
+
- spec/integration/registration_spec.rb
|
138
|
+
- spec/spec_helper.rb
|
139
|
+
homepage: ''
|
140
|
+
licenses:
|
141
|
+
- MIT
|
142
|
+
post_install_message:
|
143
|
+
rdoc_options: []
|
144
|
+
require_paths:
|
145
|
+
- lib
|
146
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
147
|
+
none: false
|
148
|
+
requirements:
|
149
|
+
- - ! '>='
|
150
|
+
- !ruby/object:Gem::Version
|
151
|
+
version: '0'
|
152
|
+
segments:
|
153
|
+
- 0
|
154
|
+
hash: 1458923697644564205
|
155
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
156
|
+
none: false
|
157
|
+
requirements:
|
158
|
+
- - ! '>='
|
159
|
+
- !ruby/object:Gem::Version
|
160
|
+
version: '0'
|
161
|
+
segments:
|
162
|
+
- 0
|
163
|
+
hash: 1458923697644564205
|
164
|
+
requirements: []
|
165
|
+
rubyforge_project:
|
166
|
+
rubygems_version: 1.8.25
|
167
|
+
signing_key:
|
168
|
+
specification_version: 3
|
169
|
+
summary: Reloads configuration code in a per-file basis
|
170
|
+
test_files:
|
171
|
+
- spec/integration/registration_spec.rb
|
172
|
+
- spec/spec_helper.rb
|