block_party 0.1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +8 -0
- data/block_party.gemspec +25 -0
- data/lib/block_party.rb +43 -0
- data/lib/block_party/configurable.rb +101 -0
- data/lib/block_party/configuration.rb +111 -0
- data/lib/block_party/configuration/comparison.rb +33 -0
- data/lib/block_party/configuration/conversion.rb +97 -0
- data/lib/block_party/configuration/enumeration.rb +15 -0
- data/lib/block_party/configuration/identity.rb +18 -0
- data/lib/block_party/extensions/hash.rb +13 -0
- data/lib/block_party/extensions/rails/active_record.rb +6 -0
- data/lib/block_party/extensions/rails/railtie.rb +6 -0
- data/lib/block_party/version.rb +3 -0
- data/test/block_party/configurable_test.rb +221 -0
- data/test/block_party/configuration_test.rb +8 -0
- data/test/block_party/extensions/hash_test.rb +5 -0
- data/test/block_party_test.rb +23 -0
- data/test/test_helper.rb +9 -0
- metadata +128 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7082052cc6fb6dc34fc13315b87ce26e149f88d6
|
4
|
+
data.tar.gz: 8c4c0c8c5132bfa444d517860307694b7eec0be8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7ec8721d21b5613f64f80a9a789815d496ccd4fea2ee44c0914fca24040fa003385326b148d695db9e9eb4ed074e5242f4ffae11b48f7747515753f3afdb4a27
|
7
|
+
data.tar.gz: 0f901fd3cd122ce3ea62a48b6c1cb7193044d28e084fbba3b9bd52bc4d59b4ead10fbfe830758a1036d263c83c7fc7929b605848b53d74b25a018f86bf9a2e76
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Christopher Keele
|
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
|
+
# BlockParty
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'block_party'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install block_party
|
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
data/block_party.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'block_party/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "block_party"
|
8
|
+
spec.version = BlockParty::VERSION
|
9
|
+
spec.authors = ["Christopher Keele"]
|
10
|
+
spec.email = ["dev@chriskeele.com"]
|
11
|
+
spec.summary = "block_party-#{BlockParty::VERSION}"
|
12
|
+
spec.description = "Make any object configurable; quickly, cleanly, easily."
|
13
|
+
spec.homepage = "http://github.com/christhekeele/block_party"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(/^bin\//) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(/^test\//)
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake", "> 10.0"
|
23
|
+
spec.add_development_dependency "minitest", "> 5.0"
|
24
|
+
spec.add_development_dependency "simplecov", "> 0.7"
|
25
|
+
end
|
data/lib/block_party.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require "block_party/configuration"
|
2
|
+
require "block_party/configurable"
|
3
|
+
|
4
|
+
module BlockParty
|
5
|
+
include Configurable
|
6
|
+
configure_with Class.new(BlockParty::Configuration)
|
7
|
+
once_configured do
|
8
|
+
if self.configuration.global
|
9
|
+
Object.const_set 'Configurable', BlockParty::Configurable
|
10
|
+
Object.const_set 'Configuration', BlockParty::Configuration
|
11
|
+
end
|
12
|
+
BlockParty::Configurable.configure do |config|
|
13
|
+
defaults = [
|
14
|
+
:default_configuration_accessor,
|
15
|
+
:default_configuration_callbacks,
|
16
|
+
:default_initial_configuration_callbacks,
|
17
|
+
:default_configuration_class
|
18
|
+
]
|
19
|
+
defaults.each do |default|
|
20
|
+
config.send :"#{default}=", BlockParty.configuration.send(default)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
def self.new
|
25
|
+
self.configuration.default_configuration_class.new
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
BlockParty.configure do |config|
|
30
|
+
config.global = true
|
31
|
+
config.default_configuration_accessor = :configuration
|
32
|
+
config.default_configuration_callbacks = []
|
33
|
+
config.default_initial_configuration_callbacks = []
|
34
|
+
config.default_configuration_class = BlockParty::Configuration
|
35
|
+
end
|
36
|
+
|
37
|
+
require 'block_party/extensions/rails/railtie' if defined?(Rails)
|
38
|
+
if defined?(ActiveRecord)
|
39
|
+
require 'block_party/extensions/rails/active_record'
|
40
|
+
::ActiveRecord::Base.extend BlockParty::Rails::ActiveRecord
|
41
|
+
end
|
42
|
+
|
43
|
+
require "block_party/version"
|
@@ -0,0 +1,101 @@
|
|
1
|
+
require "block_party/configuration"
|
2
|
+
|
3
|
+
module BlockParty
|
4
|
+
module Configurable
|
5
|
+
|
6
|
+
def self.inheritable_settings
|
7
|
+
[
|
8
|
+
:__configuration_class__,
|
9
|
+
:__configuration_callbacks__,
|
10
|
+
:__initial_configuration_callbacks__
|
11
|
+
]
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.included(base)
|
15
|
+
class << base
|
16
|
+
accessor = Configurable.configuration.default_configuration_accessor
|
17
|
+
unless method_defined? accessor
|
18
|
+
attr_accessor accessor
|
19
|
+
end
|
20
|
+
attr_accessor *Configurable.inheritable_settings
|
21
|
+
end
|
22
|
+
base.instance_variable_set :@__configuration_class__,
|
23
|
+
Configurable.configuration.default_configuration_class
|
24
|
+
base.instance_variable_set :@__configuration_callbacks__,
|
25
|
+
Configurable.configuration.default_configuration_callbacks
|
26
|
+
base.instance_variable_set :@__initial_configuration_callbacks__,
|
27
|
+
Configurable.configuration.default_initial_configuration_callbacks
|
28
|
+
base.extend ConfigurationMethods
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.extended(base)
|
32
|
+
included(base)
|
33
|
+
end
|
34
|
+
|
35
|
+
module ConfigurationMethods
|
36
|
+
def configure
|
37
|
+
accessor = Configurable.configuration.default_configuration_accessor
|
38
|
+
configuration = send(accessor)
|
39
|
+
initial_configuration = !configuration
|
40
|
+
|
41
|
+
if initial_configuration
|
42
|
+
configuration = send(:"#{accessor}=", self.__configuration_class__.new)
|
43
|
+
end
|
44
|
+
|
45
|
+
yield(configuration) if block_given?
|
46
|
+
|
47
|
+
if initial_configuration
|
48
|
+
self.__initial_configuration_callbacks__.each do |callback|
|
49
|
+
self.class_eval &callback
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
self.__configuration_callbacks__.each do |callback|
|
54
|
+
self.class_eval &callback
|
55
|
+
end
|
56
|
+
|
57
|
+
configuration
|
58
|
+
end
|
59
|
+
|
60
|
+
def configure_with(klass)
|
61
|
+
self.__configuration_class__ = klass
|
62
|
+
end
|
63
|
+
|
64
|
+
def after_configuration(&block)
|
65
|
+
self.__configuration_callbacks__ += [block] if block_given?
|
66
|
+
end
|
67
|
+
|
68
|
+
def once_configured(&block)
|
69
|
+
self.__initial_configuration_callbacks__ += [block] if block_given?
|
70
|
+
end
|
71
|
+
|
72
|
+
def inherited(base)
|
73
|
+
Configurable.inheritable_settings.each do |setting|
|
74
|
+
base.instance_variable_set :"@#{setting}", send(setting)
|
75
|
+
end
|
76
|
+
super
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
####
|
81
|
+
# An example of what using configurations would be like without `Configurable`
|
82
|
+
# since `Configurable` is configurable.
|
83
|
+
##
|
84
|
+
class << self
|
85
|
+
attr_accessor :configuration
|
86
|
+
attr_accessor *Configurable.inheritable_settings
|
87
|
+
end
|
88
|
+
self.instance_variable_set :@__configuration_class__,
|
89
|
+
BlockParty::Configuration
|
90
|
+
self.instance_variable_set :@__configuration_callbacks__, []
|
91
|
+
self.instance_variable_set :@__initial_configuration_callbacks__, []
|
92
|
+
|
93
|
+
self.instance_variable_set :@configuration,
|
94
|
+
Class.new(BlockParty::Configuration).new
|
95
|
+
self.configuration.default_configuration_accessor = :configuration
|
96
|
+
self.configuration.default_configuration_class = BlockParty::Configuration
|
97
|
+
self.configuration.default_configuration_callbacks = []
|
98
|
+
self.configuration.default_initial_configuration_callbacks = []
|
99
|
+
extend ConfigurationMethods
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
require 'block_party/configuration/conversion'
|
2
|
+
require 'block_party/configuration/identity'
|
3
|
+
require 'block_party/configuration/comparison'
|
4
|
+
require 'block_party/configuration/enumeration'
|
5
|
+
|
6
|
+
module BlockParty
|
7
|
+
class Configuration
|
8
|
+
|
9
|
+
####
|
10
|
+
# INTERFACE
|
11
|
+
##
|
12
|
+
def configure
|
13
|
+
yield(self) if block_given?
|
14
|
+
self
|
15
|
+
end
|
16
|
+
|
17
|
+
def method_missing(setting, *args, &block)
|
18
|
+
setter, getter = derive_setting_names(setting)
|
19
|
+
create_accessors setter, getter
|
20
|
+
if setter? setting
|
21
|
+
set setter, *args, &block
|
22
|
+
else
|
23
|
+
set_nested_configuration setter, *args, &block
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
def instance_variables
|
29
|
+
super.reject do |var|
|
30
|
+
[:"@__hash_representation__", :"@__hash_representation__="].include? var
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def settings
|
35
|
+
instance_variables.map do |var|
|
36
|
+
var.to_s.gsub('@', '').to_sym
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def unset(setting)
|
41
|
+
setting = setting.to_sym
|
42
|
+
setting = :"@#{setting}" unless setting.to_s =~ /^@/
|
43
|
+
remove_instance_variable setting
|
44
|
+
end
|
45
|
+
|
46
|
+
protected
|
47
|
+
|
48
|
+
####
|
49
|
+
# IMPLEMENTATION
|
50
|
+
##
|
51
|
+
def derive_setting_names(setting_name)
|
52
|
+
if setter? setting_name
|
53
|
+
[ setting_name, setter_to_getter(setting_name) ]
|
54
|
+
else
|
55
|
+
[ getter_to_setter(setting_name), setting_name ]
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def setter?(setting)
|
60
|
+
setting =~ /=$/
|
61
|
+
end
|
62
|
+
|
63
|
+
def create_accessors(setter, getter)
|
64
|
+
create_setter(setter)
|
65
|
+
create_getter(getter)
|
66
|
+
end
|
67
|
+
|
68
|
+
def create_setter(setter)
|
69
|
+
define_singleton_method setter do |value|
|
70
|
+
instance_variable_set :"@#{setter_to_getter(setter)}", value
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def create_getter(getter)
|
75
|
+
define_singleton_method getter do
|
76
|
+
instance_variable_get :"@#{getter}"
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def setter_to_getter(setter)
|
81
|
+
:"#{setter.to_s.gsub('=','')}"
|
82
|
+
end
|
83
|
+
|
84
|
+
def getter_to_setter(getter)
|
85
|
+
:"#{getter}="
|
86
|
+
end
|
87
|
+
|
88
|
+
def set(setter, *args, &block)
|
89
|
+
if block_given?
|
90
|
+
send setter, *args, &block
|
91
|
+
else
|
92
|
+
send setter, *args
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def set_nested_configuration(setter, *args, &block)
|
97
|
+
configuration = send setter, Configuration.new
|
98
|
+
yield(configuration) if block_given?
|
99
|
+
configuration
|
100
|
+
end
|
101
|
+
|
102
|
+
####
|
103
|
+
# COMPONENTS
|
104
|
+
##
|
105
|
+
include Identity
|
106
|
+
include Conversion
|
107
|
+
include Comparison
|
108
|
+
include Enumeration
|
109
|
+
|
110
|
+
end
|
111
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require "block_party/configuration"
|
2
|
+
require 'block_party/configuration/conversion'
|
3
|
+
|
4
|
+
module BlockParty
|
5
|
+
class Configuration
|
6
|
+
module Comparison
|
7
|
+
|
8
|
+
# Compare values only, class doesn't matter
|
9
|
+
def == other
|
10
|
+
if other.respond_to?(:to_hash)
|
11
|
+
to_hash == other.to_hash
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
# Compare values only, class matters
|
16
|
+
def eql?(other)
|
17
|
+
if other.respond_to?(:to_hash)
|
18
|
+
as_hash == other.to_hash
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Compare classes or instances for case statements
|
23
|
+
def === other
|
24
|
+
unless other.is_a? Class
|
25
|
+
to_hash(false) == other.to_hash if other.respond_to? :to_hash
|
26
|
+
else
|
27
|
+
super
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require "block_party/configuration"
|
2
|
+
require "block_party/extensions/hash"
|
3
|
+
|
4
|
+
module BlockParty
|
5
|
+
class Configuration
|
6
|
+
module Conversion
|
7
|
+
|
8
|
+
def self.included(base)
|
9
|
+
base.extend ClassMethods
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
|
14
|
+
def from_hash(source={})
|
15
|
+
initialize_from_hash source
|
16
|
+
end
|
17
|
+
alias_method :[], :from_hash
|
18
|
+
|
19
|
+
def load(source)
|
20
|
+
initialize_from_hash (JSON.load(source) or {})
|
21
|
+
end
|
22
|
+
|
23
|
+
def dump(configuration)
|
24
|
+
configuration.dump if configuration
|
25
|
+
end
|
26
|
+
|
27
|
+
protected
|
28
|
+
|
29
|
+
def initialize_from_hash(source)
|
30
|
+
klass = if klass_name = source.delete(:__configuration_class__)
|
31
|
+
Object.const_get klass_name
|
32
|
+
else; self; end
|
33
|
+
|
34
|
+
klass.new.load_hash source
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
def load_hash(source={})
|
40
|
+
source.each do |key, value|
|
41
|
+
if value.is_a? Hash
|
42
|
+
send :"#{key}=", self.class.from_hash(value)
|
43
|
+
else
|
44
|
+
send :"#{key}=", value
|
45
|
+
end
|
46
|
+
end
|
47
|
+
self
|
48
|
+
end
|
49
|
+
|
50
|
+
def to_hash
|
51
|
+
hash_representation.keep_if do |key|
|
52
|
+
settings.include? key
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def dump
|
57
|
+
to_hash.to_json.to_s
|
58
|
+
end
|
59
|
+
|
60
|
+
def to_s
|
61
|
+
to_hash.inspect.to_s
|
62
|
+
end
|
63
|
+
|
64
|
+
# protected
|
65
|
+
|
66
|
+
def as_hash
|
67
|
+
@__hash_representation__ ||= hash_representation
|
68
|
+
end
|
69
|
+
|
70
|
+
def hash_representation
|
71
|
+
unless empty?
|
72
|
+
Hash[
|
73
|
+
instance_variables.map do |instance_variable_name|
|
74
|
+
key = instance_variable_name.to_s.gsub('@', '').to_sym
|
75
|
+
value = instance_variable_get(instance_variable_name)
|
76
|
+
value = value.hash_representation if value.is_a?(Configuration)
|
77
|
+
[key, value]
|
78
|
+
end
|
79
|
+
]
|
80
|
+
else
|
81
|
+
{}
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def invalidate_hash_representation!
|
86
|
+
@__hash_representation__ = nil
|
87
|
+
end
|
88
|
+
|
89
|
+
def create_setter(setter)
|
90
|
+
define_singleton_method setter do |value|
|
91
|
+
invalidate_hash_representation!
|
92
|
+
instance_variable_set :"@#{setter_to_getter(setter)}", value
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "block_party/configuration"
|
2
|
+
require 'block_party/configuration/conversion'
|
3
|
+
|
4
|
+
module BlockParty
|
5
|
+
class Configuration
|
6
|
+
module Enumeration
|
7
|
+
|
8
|
+
include Enumerable
|
9
|
+
def each(*args, &block)
|
10
|
+
to_hash.each(*args, &block)
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "block_party/configuration"
|
2
|
+
require 'block_party/configuration/conversion'
|
3
|
+
|
4
|
+
module BlockParty
|
5
|
+
class Configuration
|
6
|
+
module Identity
|
7
|
+
|
8
|
+
def hash
|
9
|
+
as_hash.hash
|
10
|
+
end
|
11
|
+
|
12
|
+
def empty?
|
13
|
+
instance_variables.empty?
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'block_party/configuration'
|
2
|
+
|
3
|
+
class Hash
|
4
|
+
|
5
|
+
def self.from_config(configuration, keep_classes=false)
|
6
|
+
configuration.to_hash keep_classes
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_config(keep_classes=true)
|
10
|
+
BlockParty::Configuration.from_hash self, keep_classes
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
@@ -0,0 +1,221 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ConfigurableTest < MiniTest::Test
|
4
|
+
def setup
|
5
|
+
@configurable = Class.new{ include Configurable }
|
6
|
+
end
|
7
|
+
|
8
|
+
####
|
9
|
+
# Configure Method
|
10
|
+
##
|
11
|
+
def test_nonconfigured_classes_have_no_configuration
|
12
|
+
assert_nil @configurable.configuration
|
13
|
+
end
|
14
|
+
def test_configured_classes_have_a_configuration
|
15
|
+
@configurable.configure
|
16
|
+
assert @configurable.configuration
|
17
|
+
end
|
18
|
+
def test_configured_classes_instanciate_configurations
|
19
|
+
@configurable.configure
|
20
|
+
assert_instance_of Configuration, @configurable.configuration
|
21
|
+
end
|
22
|
+
def test_configuration_blocks_are_consumed
|
23
|
+
@configurable.configure do |config|
|
24
|
+
config.is_set = true
|
25
|
+
end
|
26
|
+
assert @configurable.configuration.is_set
|
27
|
+
end
|
28
|
+
def test_inherited_classes_dont_preserve_configurations
|
29
|
+
@configurable.configure
|
30
|
+
inherited = Class.new(@configurable)
|
31
|
+
refute_equal @configurable.configuration, inherited.configuration
|
32
|
+
end
|
33
|
+
|
34
|
+
####
|
35
|
+
# Configuration Class
|
36
|
+
##
|
37
|
+
def test_config_class_defaults_to_Configuration
|
38
|
+
assert_equal Configuration, @configurable.__configuration_class__
|
39
|
+
end
|
40
|
+
def test_configure_with_sets_custom_configuration_classes
|
41
|
+
@configurable.configure_with BasicObject
|
42
|
+
assert_equal BasicObject, @configurable.__configuration_class__
|
43
|
+
end
|
44
|
+
def test_inherited_classes_preserve_configuration_class
|
45
|
+
@configurable.configure_with BasicObject
|
46
|
+
inherited = Class.new(@configurable)
|
47
|
+
assert_equal BasicObject, inherited.__configuration_class__
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
####
|
52
|
+
# Configuration Callbacks
|
53
|
+
##
|
54
|
+
def test_callbacks_run_more_than_once_per_configuring
|
55
|
+
class << @configurable
|
56
|
+
attr_accessor :times_ran
|
57
|
+
end
|
58
|
+
@configurable.after_configuration do
|
59
|
+
self.times_ran ||= 0
|
60
|
+
self.times_ran += 1
|
61
|
+
end
|
62
|
+
@configurable.configure
|
63
|
+
@configurable.configure
|
64
|
+
assert_equal 2, @configurable.times_ran
|
65
|
+
end
|
66
|
+
# Then run the normal callback gauntlet
|
67
|
+
def test_callbacks_default_to_none
|
68
|
+
assert_empty @configurable.__configuration_callbacks__
|
69
|
+
end
|
70
|
+
def test_after_configuration_sets_a_callback
|
71
|
+
@configurable.after_configuration {}
|
72
|
+
refute_empty @configurable.__configuration_callbacks__
|
73
|
+
end
|
74
|
+
def test_after_configuration_only_adds_blocks_to_callbacks
|
75
|
+
@configurable.after_configuration
|
76
|
+
assert_empty @configurable.__configuration_callbacks__
|
77
|
+
end
|
78
|
+
def test_after_configuration_sets_a_callback_proc
|
79
|
+
@configurable.after_configuration {}
|
80
|
+
assert_instance_of Proc, @configurable.__configuration_callbacks__.first
|
81
|
+
end
|
82
|
+
def test_callbacks_get_run
|
83
|
+
@configurable.after_configuration do
|
84
|
+
self.configuration.ran = true
|
85
|
+
end
|
86
|
+
@configurable.configure
|
87
|
+
assert @configurable.configuration.ran
|
88
|
+
end
|
89
|
+
def test_multiple_callbacks_get_run_in_context_of_their_class
|
90
|
+
@configurable.after_configuration do
|
91
|
+
self.configuration.ran_in_class = name
|
92
|
+
end
|
93
|
+
@configurable.configure
|
94
|
+
assert_equal @configurable.name, @configurable.configuration.ran_in_class
|
95
|
+
end
|
96
|
+
def test_multiple_callbacks_get_set
|
97
|
+
@configurable.after_configuration {}
|
98
|
+
@configurable.after_configuration {}
|
99
|
+
assert_equal 2, @configurable.__configuration_callbacks__.length
|
100
|
+
end
|
101
|
+
def test_multiple_callbacks_get_run
|
102
|
+
@configurable.after_configuration do
|
103
|
+
self.configuration.first_ran = true
|
104
|
+
end
|
105
|
+
@configurable.after_configuration do
|
106
|
+
self.configuration.second_ran = true
|
107
|
+
end
|
108
|
+
@configurable.configure
|
109
|
+
assert @configurable.configuration.first_ran
|
110
|
+
assert @configurable.configuration.second_ran
|
111
|
+
end
|
112
|
+
def test_multiple_callbacks_get_run_in_order
|
113
|
+
@configurable.after_configuration do
|
114
|
+
self.configuration.run = 1
|
115
|
+
end
|
116
|
+
@configurable.after_configuration do
|
117
|
+
self.configuration.run = 2
|
118
|
+
end
|
119
|
+
@configurable.configure
|
120
|
+
assert_equal 2, @configurable.configuration.run
|
121
|
+
end
|
122
|
+
def test_inherited_classes_preserve_callbacks
|
123
|
+
@configurable.after_configuration do
|
124
|
+
define_singleton_method(:callback) { true }
|
125
|
+
end
|
126
|
+
inherited = Class.new(@configurable)
|
127
|
+
inherited.configure
|
128
|
+
assert inherited.callback
|
129
|
+
end
|
130
|
+
def test_inherited_classes_dont_add_to_parent_callbacks
|
131
|
+
inherited = Class.new(@configurable)
|
132
|
+
inherited.after_configuration {}
|
133
|
+
assert_empty @configurable.__configuration_callbacks__
|
134
|
+
end
|
135
|
+
|
136
|
+
####
|
137
|
+
# Initial Configuration Callbacks
|
138
|
+
##
|
139
|
+
def test_inital_callbacks_only_run_once_per_configuring
|
140
|
+
class << @configurable
|
141
|
+
attr_accessor :times_ran
|
142
|
+
end
|
143
|
+
@configurable.once_configured do
|
144
|
+
self.times_ran ||= 0
|
145
|
+
self.times_ran += 1
|
146
|
+
end
|
147
|
+
@configurable.configure
|
148
|
+
@configurable.configure
|
149
|
+
assert_equal 1, @configurable.times_ran
|
150
|
+
end
|
151
|
+
# Then run the normal callback gauntlet
|
152
|
+
def test_initial_callbacks_default_to_none
|
153
|
+
assert_empty @configurable.__initial_configuration_callbacks__
|
154
|
+
end
|
155
|
+
def test_after_configuration_sets_a_initial_callback
|
156
|
+
@configurable.once_configured {}
|
157
|
+
refute_empty @configurable.__initial_configuration_callbacks__
|
158
|
+
end
|
159
|
+
def test_once_configured_only_adds_blocks_to_initial_callbacks
|
160
|
+
@configurable.once_configured
|
161
|
+
assert_empty @configurable.__initial_configuration_callbacks__
|
162
|
+
end
|
163
|
+
def test_once_configured_sets_a_initial_callback_proc
|
164
|
+
@configurable.once_configured {}
|
165
|
+
assert_instance_of Proc, @configurable.__initial_configuration_callbacks__.first
|
166
|
+
end
|
167
|
+
def test_initial_callbacks_get_run
|
168
|
+
@configurable.once_configured do
|
169
|
+
self.configuration.ran = true
|
170
|
+
end
|
171
|
+
@configurable.configure
|
172
|
+
assert @configurable.configuration.ran
|
173
|
+
end
|
174
|
+
def test_initial_callbacks_get_run_in_context_of_their_class
|
175
|
+
@configurable.once_configured do
|
176
|
+
self.configuration.ran_in_class = name
|
177
|
+
end
|
178
|
+
@configurable.configure
|
179
|
+
assert_equal @configurable.name, @configurable.configuration.ran_in_class
|
180
|
+
end
|
181
|
+
def test_multiple_initial_callbacks_get_set
|
182
|
+
@configurable.once_configured {}
|
183
|
+
@configurable.once_configured {}
|
184
|
+
assert_equal 2, @configurable.__initial_configuration_callbacks__.length
|
185
|
+
end
|
186
|
+
def test_multiple_initial_callbacks_get_run
|
187
|
+
@configurable.once_configured do
|
188
|
+
self.configuration.first_ran = true
|
189
|
+
end
|
190
|
+
@configurable.once_configured do
|
191
|
+
self.configuration.second_ran = true
|
192
|
+
end
|
193
|
+
@configurable.configure
|
194
|
+
assert @configurable.configuration.first_ran
|
195
|
+
assert @configurable.configuration.second_ran
|
196
|
+
end
|
197
|
+
def test_multiple_initial_callbacks_get_run_in_order
|
198
|
+
@configurable.once_configured do
|
199
|
+
self.configuration.run = 1
|
200
|
+
end
|
201
|
+
@configurable.once_configured do
|
202
|
+
self.configuration.run = 2
|
203
|
+
end
|
204
|
+
@configurable.configure
|
205
|
+
assert_equal 2, @configurable.configuration.run
|
206
|
+
end
|
207
|
+
def test_inherited_classes_preserve_initial_callbacks
|
208
|
+
@configurable.once_configured do
|
209
|
+
define_singleton_method(:initial_callback) { true }
|
210
|
+
end
|
211
|
+
inherited = Class.new(@configurable)
|
212
|
+
inherited.configure
|
213
|
+
assert inherited.initial_callback
|
214
|
+
end
|
215
|
+
def test_inherited_classes_dont_add_to_parent_initial_callbacks
|
216
|
+
inherited = Class.new(@configurable)
|
217
|
+
inherited.once_configured {}
|
218
|
+
assert_empty @configurable.__initial_configuration_callbacks__
|
219
|
+
end
|
220
|
+
|
221
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class BlockPartyTest < MiniTest::Test
|
4
|
+
|
5
|
+
def test_that_libray_is_preconfigured
|
6
|
+
assert BlockParty.configuration
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_that_libray_is_preconfigured_to_load_globally
|
10
|
+
assert BlockParty.configuration.global
|
11
|
+
end
|
12
|
+
def test_that_configuration_is_globally_preloaded
|
13
|
+
assert defined?(::Configuration)
|
14
|
+
end
|
15
|
+
def test_that_configurable_is_globally_preloaded
|
16
|
+
assert defined?(::Configurable)
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_that_new_returns_a_new_configuration
|
20
|
+
assert_instance_of BlockParty::Configuration, BlockParty.new
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: block_party
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Christopher Keele
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-08-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>'
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>'
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>'
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>'
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: simplecov
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>'
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.7'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>'
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.7'
|
69
|
+
description: Make any object configurable; quickly, cleanly, easily.
|
70
|
+
email:
|
71
|
+
- dev@chriskeele.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- block_party.gemspec
|
82
|
+
- lib/block_party.rb
|
83
|
+
- lib/block_party/configurable.rb
|
84
|
+
- lib/block_party/configuration.rb
|
85
|
+
- lib/block_party/configuration/comparison.rb
|
86
|
+
- lib/block_party/configuration/conversion.rb
|
87
|
+
- lib/block_party/configuration/enumeration.rb
|
88
|
+
- lib/block_party/configuration/identity.rb
|
89
|
+
- lib/block_party/extensions/hash.rb
|
90
|
+
- lib/block_party/extensions/rails/active_record.rb
|
91
|
+
- lib/block_party/extensions/rails/railtie.rb
|
92
|
+
- lib/block_party/version.rb
|
93
|
+
- test/block_party/configurable_test.rb
|
94
|
+
- test/block_party/configuration_test.rb
|
95
|
+
- test/block_party/extensions/hash_test.rb
|
96
|
+
- test/block_party_test.rb
|
97
|
+
- test/test_helper.rb
|
98
|
+
homepage: http://github.com/christhekeele/block_party
|
99
|
+
licenses:
|
100
|
+
- MIT
|
101
|
+
metadata: {}
|
102
|
+
post_install_message:
|
103
|
+
rdoc_options: []
|
104
|
+
require_paths:
|
105
|
+
- lib
|
106
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - '>='
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
requirements: []
|
117
|
+
rubyforge_project:
|
118
|
+
rubygems_version: 2.0.3
|
119
|
+
signing_key:
|
120
|
+
specification_version: 4
|
121
|
+
summary: block_party-0.1.0.1
|
122
|
+
test_files:
|
123
|
+
- test/block_party/configurable_test.rb
|
124
|
+
- test/block_party/configuration_test.rb
|
125
|
+
- test/block_party/extensions/hash_test.rb
|
126
|
+
- test/block_party_test.rb
|
127
|
+
- test/test_helper.rb
|
128
|
+
has_rdoc:
|