mini_tools 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/mini_tools.rb +3 -0
- data/lib/mini_tools/command.rb +26 -0
- data/lib/mini_tools/configuration.rb +56 -0
- data/lib/mini_tools/item_factory.rb +67 -0
- data/lib/mini_tools/response.rb +14 -0
- data/lib/mini_tools/version.rb +3 -0
- data/spec/command_spec.rb +97 -0
- data/spec/configuration_spec.rb +50 -0
- data/spec/item_factory_spec.rb +47 -0
- data/spec/spec_helper.rb +8 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7e2e7b08f84dd78b66b910afdca999cdd290f448
|
4
|
+
data.tar.gz: 6505ac0a2b4ed5da4d0390b2cb144ad28e823600
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f79242651ae5ec09e4a6cd2768d59b5daf8153524216c6eb0aaf9403a4acecff1c7f14049190e32b8f2dba9566406e40a9aa307a91cdf069dae76f5793dd04ef
|
7
|
+
data.tar.gz: caeff60a929d70380562bc18ccb836b621b6378fb513c8eca28bd0844668f1abb029dd247ae724423661314f80de4307830d5ff0d5c0f42c81ce69b449776709
|
data/lib/mini_tools.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'mini_tools/response'
|
2
|
+
|
3
|
+
module MiniTools
|
4
|
+
# Creates a very simple command object using the MiniTools::Response class
|
5
|
+
#
|
6
|
+
# ```
|
7
|
+
# class SimpleCommand
|
8
|
+
# def call value
|
9
|
+
# yield response :success, 'It worked' if value == true
|
10
|
+
# yield response :failure, 'It failed' if value == false
|
11
|
+
# end
|
12
|
+
# end
|
13
|
+
# ```
|
14
|
+
#
|
15
|
+
# Then use with
|
16
|
+
#
|
17
|
+
# SimpleCommand.new.call some_value do |response|
|
18
|
+
# response.on(:success) ->(message) { puts 'Successful'; puts message }
|
19
|
+
# response.on(:failure) ->(message) { puts 'Unsuccessful'; puts message }
|
20
|
+
# end
|
21
|
+
module Command
|
22
|
+
def response result, *args
|
23
|
+
Response.new(result, *args)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module MiniTools
|
2
|
+
# Used to build simple configuration objects.
|
3
|
+
#
|
4
|
+
# ```
|
5
|
+
# class SimpleConfig
|
6
|
+
# include MiniTools::Configuration
|
7
|
+
#
|
8
|
+
# allow :value_1, value_2
|
9
|
+
# allow :value_3
|
10
|
+
# end
|
11
|
+
# ```
|
12
|
+
#
|
13
|
+
# Then we can set the values with
|
14
|
+
#
|
15
|
+
# ```
|
16
|
+
# SimpleConfig.configure do |config|
|
17
|
+
# config.value_1 = 'some-value'
|
18
|
+
# config.value_2 = 'another-value'
|
19
|
+
# config.value_3 = 'a-third-value'
|
20
|
+
# end
|
21
|
+
# ```
|
22
|
+
#
|
23
|
+
# and access the values with
|
24
|
+
#
|
25
|
+
# ```
|
26
|
+
# SimpleConfig.config.value_1 # some-value
|
27
|
+
# ```
|
28
|
+
module Configuration
|
29
|
+
def self.included(base)
|
30
|
+
base.extend ClassMethods
|
31
|
+
end
|
32
|
+
|
33
|
+
module ClassMethods
|
34
|
+
# Adds values to the allowed list of settings. Accepts either a single
|
35
|
+
# settings key or array and can be called multiple times.
|
36
|
+
def allow *values
|
37
|
+
@settings = settings.concat(values).uniq
|
38
|
+
end
|
39
|
+
|
40
|
+
# Returns the configuration object
|
41
|
+
def config
|
42
|
+
@config ||= Struct.new(*settings).new
|
43
|
+
end
|
44
|
+
|
45
|
+
# Accepts a block of configuration values
|
46
|
+
def configure
|
47
|
+
yield config
|
48
|
+
end
|
49
|
+
|
50
|
+
# Returns an array of allowed settings
|
51
|
+
def settings
|
52
|
+
@settings ||= []
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
module MiniTools
|
2
|
+
# The ItemFactory is used for creating instances of classes based on a map
|
3
|
+
# of keys to classes. For example, assume we a list of commands to be run
|
4
|
+
# that looks something like this
|
5
|
+
# ```
|
6
|
+
# commands = [
|
7
|
+
# [:notification_email, 'someone@somewhere.co.uk'],
|
8
|
+
# [:update_account, account_instance],
|
9
|
+
# ]
|
10
|
+
# ```
|
11
|
+
# Each of these commands has an associated Command object.
|
12
|
+
# Then we can create an ItemFactory to dynamically build the instances we
|
13
|
+
# need
|
14
|
+
#
|
15
|
+
# ```
|
16
|
+
# class CommandFactory
|
17
|
+
# include MiniTools::ItemFactory
|
18
|
+
#
|
19
|
+
# map(
|
20
|
+
# notification_email: EmailNotifier,
|
21
|
+
# update_account: AccountUpdater
|
22
|
+
# )
|
23
|
+
# end
|
24
|
+
#
|
25
|
+
# commands.each do |command, args|
|
26
|
+
# CommandFactory.build(command).execute(args)
|
27
|
+
# end
|
28
|
+
# ```
|
29
|
+
#
|
30
|
+
module ItemFactory
|
31
|
+
def self.included(base)
|
32
|
+
base.extend(ClassMethods)
|
33
|
+
end
|
34
|
+
|
35
|
+
module ClassMethods
|
36
|
+
attr_reader :default_klass
|
37
|
+
|
38
|
+
# Builds the appropriate item based on `value`. Any additional args are
|
39
|
+
# passed on to the item's initializer
|
40
|
+
def build value, *args
|
41
|
+
klass_for(value).new(*args)
|
42
|
+
end
|
43
|
+
|
44
|
+
# Defines the map of values to class types
|
45
|
+
def map values
|
46
|
+
@klass_map = values
|
47
|
+
end
|
48
|
+
|
49
|
+
# Sets the default class if there is no match in the map
|
50
|
+
def default klass
|
51
|
+
@default_klass = klass
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def klass_map
|
57
|
+
@klass_map ||= {}
|
58
|
+
end
|
59
|
+
|
60
|
+
def klass_for value
|
61
|
+
value = value.call if value.respond_to? :call
|
62
|
+
return klass_map[value] if klass_map.has_key? value
|
63
|
+
return default_klass
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
require 'mini_tools/command'
|
4
|
+
|
5
|
+
describe MiniTools::Command do
|
6
|
+
class TestCommand
|
7
|
+
include MiniTools::Command
|
8
|
+
|
9
|
+
def call param
|
10
|
+
yield response :success, 'OK' and return if param == 1
|
11
|
+
yield response :error, 'Fail' and return if param == 0
|
12
|
+
yield response :unknown, 'Other' if ![0, 1].include?(param)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
subject { TestCommand.new }
|
17
|
+
|
18
|
+
describe 'response yielding' do
|
19
|
+
describe 'when the interaction is successful' do
|
20
|
+
it 'should yield a success response' do
|
21
|
+
call_with 1
|
22
|
+
assert_equal 'success', @state
|
23
|
+
assert_equal 'OK', @message
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe 'when the interaction errors' do
|
28
|
+
it 'should yield an error response' do
|
29
|
+
call_with 0
|
30
|
+
assert_equal 'error', @state
|
31
|
+
assert_equal 'Fail', @message
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe 'when there is an unknown response' do
|
36
|
+
it 'should yield an unknown response' do
|
37
|
+
call_with 2
|
38
|
+
assert_equal 'unknown', @state
|
39
|
+
assert_equal 'Other', @message
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe 'when handling multiple responses with the same block' do
|
44
|
+
it 'should work with the first case' do
|
45
|
+
subject.call 0 do |response|
|
46
|
+
response.on(:error, :unknown) do
|
47
|
+
@state = 'handled'
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
assert_equal 'handled', @state
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'should work with the second case' do
|
55
|
+
subject.call 2 do |response|
|
56
|
+
response.on(:error, :unknown) do
|
57
|
+
@state = 'handled'
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
assert_equal 'handled', @state
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'should not handle other cases' do
|
65
|
+
subject.call 1 do |response|
|
66
|
+
response.on(:error, :unknown) do
|
67
|
+
@state = 'handled'
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
assert_nil @state
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def call_with param
|
77
|
+
@state = false
|
78
|
+
@message = nil
|
79
|
+
|
80
|
+
subject.call param do |response|
|
81
|
+
response.on(:success) do |m|
|
82
|
+
@state = 'success'
|
83
|
+
@message = m
|
84
|
+
end
|
85
|
+
|
86
|
+
response.on(:error) do |m|
|
87
|
+
@state = 'error'
|
88
|
+
@message = m
|
89
|
+
end
|
90
|
+
|
91
|
+
response.on(:unknown) do |m|
|
92
|
+
@state = 'unknown'
|
93
|
+
@message = m
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
require 'mini_tools/configuration'
|
4
|
+
|
5
|
+
describe MiniTools::Configuration do
|
6
|
+
class TestConfig
|
7
|
+
include MiniTools::Configuration
|
8
|
+
|
9
|
+
allow :setting_1, :setting_2
|
10
|
+
allow :setting_3
|
11
|
+
end
|
12
|
+
|
13
|
+
subject { TestConfig }
|
14
|
+
|
15
|
+
describe 'configuration' do
|
16
|
+
it 'should be configurable using a config block' do
|
17
|
+
TestConfig.configure do |config|
|
18
|
+
config.setting_1 = 'a-setting'
|
19
|
+
config.setting_2 = 'another-setting'
|
20
|
+
config.setting_3 = 'a-third-setting'
|
21
|
+
end
|
22
|
+
|
23
|
+
assert_equal 'a-setting', TestConfig.config.setting_1
|
24
|
+
assert_equal 'another-setting', TestConfig.config.setting_2
|
25
|
+
assert_equal 'a-third-setting', TestConfig.config.setting_3
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should raise an error when an unknown setting is used' do
|
29
|
+
assert_raises NoMethodError do
|
30
|
+
TestConfig.configure do |config|
|
31
|
+
config.unkown_setting = 'unknown-setting'
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'should be updateable once set' do
|
37
|
+
TestConfig.configure do |config|
|
38
|
+
config.setting_1 = 'a-setting'
|
39
|
+
config.setting_2 = 'another-setting'
|
40
|
+
end
|
41
|
+
|
42
|
+
TestConfig.configure do |config|
|
43
|
+
config.setting_1 = 'a-revised-setting'
|
44
|
+
end
|
45
|
+
|
46
|
+
assert_equal 'a-revised-setting', TestConfig.config.setting_1
|
47
|
+
assert_equal 'another-setting', TestConfig.config.setting_2
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
require 'mini_tools/item_factory'
|
4
|
+
|
5
|
+
describe MiniTools::ItemFactory do
|
6
|
+
class TestItemFactory
|
7
|
+
class BaseItem; def initialize(arg1, arg2); end; end
|
8
|
+
class CoolItem; def initialize(arg1, arg2); end; end
|
9
|
+
|
10
|
+
include MiniTools::ItemFactory
|
11
|
+
|
12
|
+
map(
|
13
|
+
cool_item: CoolItem
|
14
|
+
)
|
15
|
+
|
16
|
+
default BaseItem
|
17
|
+
end
|
18
|
+
|
19
|
+
describe 'building items' do
|
20
|
+
it 'should return the default item for unknown values passing on any extra args' do
|
21
|
+
TestItemFactory::BaseItem
|
22
|
+
.stubs(:new)
|
23
|
+
.with(:arg1, :arg2)
|
24
|
+
.returns('base-item')
|
25
|
+
|
26
|
+
assert_equal 'base-item', TestItemFactory.build(:any_item, :arg1, :arg2)
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'should return the specific item if set' do
|
30
|
+
TestItemFactory::CoolItem
|
31
|
+
.stubs(:new)
|
32
|
+
.with(:arg1, :arg2)
|
33
|
+
.returns('cool-item')
|
34
|
+
|
35
|
+
assert_equal 'cool-item', TestItemFactory.build(:cool_item, :arg1, :arg2)
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'should accept a proc that returns the value' do
|
39
|
+
TestItemFactory::CoolItem
|
40
|
+
.stubs(:new)
|
41
|
+
.with(:arg1, :arg2)
|
42
|
+
.returns('cool-item')
|
43
|
+
|
44
|
+
assert_equal 'cool-item', TestItemFactory.build(->{:cool_item}, :arg1, :arg2)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
testdir = File.dirname(__FILE__)
|
2
|
+
$LOAD_PATH.unshift testdir unless $LOAD_PATH.include?(testdir)
|
3
|
+
|
4
|
+
libdir = File.dirname(File.dirname(__FILE__)) + '/lib'
|
5
|
+
$LOAD_PATH.unshift libdir unless $LOAD_PATH.include?(libdir)
|
6
|
+
|
7
|
+
require 'minitest/autorun'
|
8
|
+
require 'mocha/mini_test'
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mini_tools
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Adam Phillips
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: minitest
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: mocha
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: A set of simple lightweight tools to demonstrate some useful object patterns
|
42
|
+
in Ruby
|
43
|
+
email: adam@29ways.co.uk
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- lib/mini_tools.rb
|
49
|
+
- lib/mini_tools/command.rb
|
50
|
+
- lib/mini_tools/configuration.rb
|
51
|
+
- lib/mini_tools/item_factory.rb
|
52
|
+
- lib/mini_tools/response.rb
|
53
|
+
- lib/mini_tools/version.rb
|
54
|
+
- spec/command_spec.rb
|
55
|
+
- spec/configuration_spec.rb
|
56
|
+
- spec/item_factory_spec.rb
|
57
|
+
- spec/spec_helper.rb
|
58
|
+
homepage: https://github.com/adamphillips/mini_tools
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
metadata: {}
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 2.4.3
|
79
|
+
signing_key:
|
80
|
+
specification_version: 4
|
81
|
+
summary: A set of simple lightweight tools to demonstrate patterns in Ruby
|
82
|
+
test_files:
|
83
|
+
- spec/command_spec.rb
|
84
|
+
- spec/configuration_spec.rb
|
85
|
+
- spec/item_factory_spec.rb
|
86
|
+
- spec/spec_helper.rb
|