power_converter 0.0.1.pre → 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +19 -0
- data/lib/power_converter.rb +151 -1
- data/lib/power_converter/version.rb +3 -1
- data/power_converter.gemspec +6 -3
- metadata +35 -10
- data/.gitignore +0 -14
- data/Gemfile +0 -4
- data/Rakefile +0 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1ffe8e4ed7f1e0bdafe8e06f34abca7936df05eb
|
4
|
+
data.tar.gz: 7be13e121f7797f707da2627fa048a7117d2888c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6b61649071f7d32ed30c86ce4f4dc09a1a442f0d49c6ac8d255a7ab03b1e0c2b1326557e559a5e90a5d4b79fadca90c0e3f9628cd10ed05f6e46095cea677b2f
|
7
|
+
data.tar.gz: a25a41c2d57a8241a307905ba9d9e6264d0133888bc56b181b52bddbc3beb0583b4816c7093234499bcd827b21be73c435a583e3c8353786832f76ca312aea33
|
data/README.md
CHANGED
@@ -1,3 +1,22 @@
|
|
1
1
|
# PowerConverter
|
2
2
|
|
3
|
+
[![Build Status](https://travis-ci.org/jeremyf/power_converter.png?branch=master)](https://travis-ci.org/jeremyf/power_converter)
|
4
|
+
[![Documentation Status](http://inch-ci.org/github/jeremyf/power_converter.svg?branch=master)](http://inch-ci.org/github/jeremyf/power_converter)
|
5
|
+
[![APACHE 2 License](http://img.shields.io/badge/APACHE2-license-blue.svg)](./LICENSE)
|
6
|
+
|
3
7
|
A placeholder for implementing a conversion module pattern.
|
8
|
+
|
9
|
+
Exposing a means of registering conversions. These can be accessed at the module
|
10
|
+
level:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
PowerConverter.define_conversion_for :boolean do |input|
|
14
|
+
case input
|
15
|
+
when false, 0, '0', 'false', 'no', nil then false
|
16
|
+
else
|
17
|
+
true
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
PowerConverter.convert(object, to: :boolean)
|
22
|
+
```
|
data/lib/power_converter.rb
CHANGED
@@ -1,5 +1,155 @@
|
|
1
1
|
require "power_converter/version"
|
2
2
|
|
3
|
+
# PowerConverter is a composition service module. It provides a way to define
|
4
|
+
# conversion methods.
|
5
|
+
#
|
6
|
+
# What is a conversion method?
|
7
|
+
#
|
8
|
+
# > A well-established Ruby idiom for methods which "do the right thing" to
|
9
|
+
# > convery any reasonable input value into a desired class.
|
10
|
+
# >
|
11
|
+
# > http://devblog.avdi.org/2012/05/07/a-ruby-conversion-idiom/
|
12
|
+
#
|
13
|
+
# Why conversion methods?
|
14
|
+
#
|
15
|
+
# Because software is all about addressing a mapping problem. In my experience
|
16
|
+
# it has also exposed a means for easing the movement across application design
|
17
|
+
# boundaries.
|
3
18
|
module PowerConverter
|
4
|
-
#
|
19
|
+
# When you tried to find a named_converter and it did not exist, this is a
|
20
|
+
# reasonable exception to expect.
|
21
|
+
class ConverterNotFoundError < RuntimeError
|
22
|
+
# @param named_converter [#to_s]
|
23
|
+
# @param defined_converter_names [Array]
|
24
|
+
#
|
25
|
+
# @example
|
26
|
+
# raise ConverterNotFoundError.new(:boolean, [:hello, :world])
|
27
|
+
def initialize(named_converter, defined_converter_names)
|
28
|
+
super("Unable to find PowerConverter for #{named_converter} in #{defined_converter_names.inspect}")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
module_function
|
33
|
+
|
34
|
+
# @api public
|
35
|
+
# @since 0.0.1
|
36
|
+
#
|
37
|
+
# Responsible for defining a conversion method and a "shovel-ready" conversion
|
38
|
+
# module; because maybe you want a mixin for convenience reasons.
|
39
|
+
#
|
40
|
+
# @param named_conversion [String,Symbol] the name of the conversion that you
|
41
|
+
# are declaring.
|
42
|
+
# @param converter [#call] the callable object that will perform the
|
43
|
+
# conversion.
|
44
|
+
# @yield [value] A block that will be used to convert the given value
|
45
|
+
# to the named thing.
|
46
|
+
# @yieldreturn returns the named thing.
|
47
|
+
#
|
48
|
+
# @return void
|
49
|
+
#
|
50
|
+
# @example
|
51
|
+
# PowerConverter.define_conversion_for :boolean do |input|
|
52
|
+
# case input
|
53
|
+
# when false, 0, '0', 'false', 'no', nil then false
|
54
|
+
# else
|
55
|
+
# true
|
56
|
+
# end
|
57
|
+
# end
|
58
|
+
# @see http://devblog.avdi.org/2012/05/07/a-ruby-conversion-idiom/ Avdi
|
59
|
+
# Grimm's post on "A Ruby Conversion Idiom"
|
60
|
+
#
|
61
|
+
# @see Kernel#Array for inspiration
|
62
|
+
#
|
63
|
+
# @note The conversion module/method that is created may not adhear to the
|
64
|
+
# exact idiom (a method defined in CamelCase)
|
65
|
+
#
|
66
|
+
# @todo Make sure that the converter requires at least one parameter.
|
67
|
+
def define_conversion_for(named_conversion, &converter)
|
68
|
+
@defined_conversions ||= {}
|
69
|
+
@defined_conversions[named_conversion.to_s] = converter
|
70
|
+
end
|
71
|
+
|
72
|
+
# @api public
|
73
|
+
# @since 0.0.1
|
74
|
+
#
|
75
|
+
# Convert the given `value` via the named `:to` converter.
|
76
|
+
#
|
77
|
+
# @param value [Object] the thing that you will be converting
|
78
|
+
# @param [Hash] options the options used to perform the conversion
|
79
|
+
# @option options [Symbol] :to the named_conversion that has been registered
|
80
|
+
#
|
81
|
+
# @raise [ConverterNotFoundError] if the named converter is not found
|
82
|
+
#
|
83
|
+
# @see PowerConverter.define_conversion_for
|
84
|
+
#
|
85
|
+
# @example
|
86
|
+
# PowerConverter.convert('true', to: :boolean)
|
87
|
+
#
|
88
|
+
# @todo I want to:
|
89
|
+
# * raise an exception if I'm unable to convert an object
|
90
|
+
# * auto-handle :to_<named_conversion> so I don't need to worry about
|
91
|
+
# registering that.
|
92
|
+
def convert(value, options = {})
|
93
|
+
converter_for(options.fetch(:to)).call(value)
|
94
|
+
end
|
95
|
+
|
96
|
+
# @api public
|
97
|
+
# @since 0.0.1
|
98
|
+
#
|
99
|
+
# The means for mixing in a private conversion method; Perhaps as policy you
|
100
|
+
# don't want to expose the public conversion method but instead prefer to
|
101
|
+
# leverage private methods.
|
102
|
+
#
|
103
|
+
# @param named_conversion [String,Symbol] the name of the conversion that you
|
104
|
+
# are requesting be wrapped in a conversion module.
|
105
|
+
#
|
106
|
+
# @return [Module] a conversion module to use for mixing in behavior
|
107
|
+
#
|
108
|
+
# @example
|
109
|
+
# class Foo
|
110
|
+
# attr_accessor :bar
|
111
|
+
# include PowerConverter.module_for(:boolean)
|
112
|
+
# def bar_as_boolean
|
113
|
+
# convert_to_boolean(@bar)
|
114
|
+
# end
|
115
|
+
# end
|
116
|
+
def module_for(named_conversion)
|
117
|
+
converter = converter_for(named_conversion)
|
118
|
+
Module.new do
|
119
|
+
define_method("convert_to_#{named_conversion}", &converter)
|
120
|
+
private "convert_to_#{named_conversion}"
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
# @api public
|
125
|
+
# @since 0.0.1
|
126
|
+
#
|
127
|
+
# Given the `named_conversion` find and retrieve the defined converter.
|
128
|
+
#
|
129
|
+
# @param named_conversion [String,Symbol]
|
130
|
+
#
|
131
|
+
# @return [#call] a registered converter
|
132
|
+
#
|
133
|
+
# @raise [ConverterNotFoundError] if the named converter is not found
|
134
|
+
#
|
135
|
+
# @see PowerConverter.define_conversion_for
|
136
|
+
#
|
137
|
+
# @example
|
138
|
+
# PowerConverter.converter_for(:boolean).call(value)
|
139
|
+
def converter_for(named_conversion)
|
140
|
+
@defined_conversions.fetch(named_conversion.to_s)
|
141
|
+
rescue KeyError
|
142
|
+
raise ConverterNotFoundError.new(named_conversion, defined_converter_names)
|
143
|
+
end
|
144
|
+
|
145
|
+
# @api public
|
146
|
+
# @since 0.0.1
|
147
|
+
#
|
148
|
+
# A convenience method for seeing the names of all converters that have been
|
149
|
+
# defined.
|
150
|
+
#
|
151
|
+
# @return [Array] of the registered converter's names
|
152
|
+
def defined_converter_names
|
153
|
+
@defined_conversions.keys
|
154
|
+
end
|
5
155
|
end
|
data/power_converter.gemspec
CHANGED
@@ -8,16 +8,19 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = PowerConverter::VERSION
|
9
9
|
spec.authors = ["Jeremy Friesen"]
|
10
10
|
spec.email = ["jeremy.n.friesen@gmail.com"]
|
11
|
-
spec.summary = %q{A
|
12
|
-
spec.description = %q{A
|
11
|
+
spec.summary = %q{A lightweight library to define object conversion methods/modules.}
|
12
|
+
spec.description = %q{A lightweight library to define object conversion methods/modules.}
|
13
13
|
spec.homepage = "https://github.com/jeremyf/power_converter"
|
14
14
|
spec.license = "APACHE2"
|
15
15
|
|
16
|
-
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.files = `git ls-files -z -- lib/* bin/* LICENSE README.md CHANGELOG.md power_converter.gemspec`.split("\x0")
|
17
17
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
+
spec.required_ruby_version = '~> 2.0'
|
21
22
|
spec.add_development_dependency "bundler", "~> 1.7"
|
22
23
|
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "minitest", "~> 5.5"
|
25
|
+
spec.add_development_dependency "rubocop", "~> 0.2"
|
23
26
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: power_converter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.1
|
4
|
+
version: 0.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeremy Friesen
|
@@ -38,18 +38,43 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
|
-
|
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.5'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.5'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.2'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.2'
|
69
|
+
description: A lightweight library to define object conversion methods/modules.
|
42
70
|
email:
|
43
71
|
- jeremy.n.friesen@gmail.com
|
44
72
|
executables: []
|
45
73
|
extensions: []
|
46
74
|
extra_rdoc_files: []
|
47
75
|
files:
|
48
|
-
- ".gitignore"
|
49
|
-
- Gemfile
|
50
76
|
- LICENSE
|
51
77
|
- README.md
|
52
|
-
- Rakefile
|
53
78
|
- lib/power_converter.rb
|
54
79
|
- lib/power_converter/version.rb
|
55
80
|
- power_converter.gemspec
|
@@ -63,18 +88,18 @@ require_paths:
|
|
63
88
|
- lib
|
64
89
|
required_ruby_version: !ruby/object:Gem::Requirement
|
65
90
|
requirements:
|
66
|
-
- - "
|
91
|
+
- - "~>"
|
67
92
|
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
93
|
+
version: '2.0'
|
69
94
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
95
|
requirements:
|
71
|
-
- - "
|
96
|
+
- - ">="
|
72
97
|
- !ruby/object:Gem::Version
|
73
|
-
version:
|
98
|
+
version: '0'
|
74
99
|
requirements: []
|
75
100
|
rubyforge_project:
|
76
101
|
rubygems_version: 2.4.3
|
77
102
|
signing_key:
|
78
103
|
specification_version: 4
|
79
|
-
summary: A
|
104
|
+
summary: A lightweight library to define object conversion methods/modules.
|
80
105
|
test_files: []
|
data/.gitignore
DELETED
data/Gemfile
DELETED
data/Rakefile
DELETED