rosetta-stone 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6f49ad30a0b19d92be9bca2d6b43eca56f1843fe9b594d9c46c1401113304c15
4
- data.tar.gz: 5989f92c630e155ea5da9a5191c6a06e5e41ac2f8854d347372f611b814a0b68
3
+ metadata.gz: a533462d58d84278dc028e4045c6eaaa31d85bbc57b0d84b973fc65e77ba9f54
4
+ data.tar.gz: ead94d4194d3f3cf9cc09e3fc4e4330ebe9d8b8f502a09ec89d972176fc1d695
5
5
  SHA512:
6
- metadata.gz: ef07a8fbf976977caff7b8de46ff30df4ff500df9784bda69fb81e146d03123f7208434f5eddf8cacb57663d0113f5a352cd3ded94664ac09cd5f36eee4a8914
7
- data.tar.gz: 12c85f29aebe211372dc362c221bc6f82e0ff8f64524060c2fb679e9ac7185893b0b2e9421e4f4a9dae7331b1095d71c2b5b8809d574d1ffd8531aa059475d74
6
+ metadata.gz: fab482ccd77877e6b8f9af64c196ef3834840e0263ea3b3bea806f2d7d1613ee64f2029c78efa4e3beec755cbacc19bdf7c28babd631dba54a1b847352003048
7
+ data.tar.gz: 1b0f519e1f16a1f980b3ca87a3cc0c041745aba1d26abef3c85320d2a0a68516b60d628cb459950c8639c4633645facb814c9da38c33aa2940d8cd4f27c00f78
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rosetta-stone (0.2.0)
4
+ rosetta-stone (0.2.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # Rosetta
2
+ [![Build Status](https://travis-ci.com/Aquaj/rosetta.svg?branch=master)](https://travis-ci.com/Aquaj/rosetta) [![Gem Version](https://badge.fury.io/rb/rosetta-stone.svg)](https://badge.fury.io/rb/rosetta-stone)
2
3
 
3
4
  A lightweight format-to-format translator.
4
5
 
@@ -142,7 +143,8 @@ Example:
142
143
  # are equivalent.
143
144
 
144
145
  Rosetta::Translation.new(:down, :up).call("you're looking for")
145
- # => "YOU'RE LOOKING FOR3
146
+ # => "YOU'RE LOOKING FOR"
147
+ ```
146
148
 
147
149
  ## Development
148
150
 
@@ -1,24 +1,10 @@
1
1
  require 'rosetta/exceptions'
2
+ require 'rosetta/support/registerable'
2
3
 
3
4
  module Rosetta
4
5
  module Deserializers
5
- @registered = {}
6
+ extend Support::Registerable
6
7
 
7
- class << self
8
- attr_reader :registered
9
-
10
- def [](key)
11
- registered[key]
12
- end
13
-
14
- def register(name, deserializer, &block)
15
- raise ExistingDeserializerError, <<-ERROR.strip if @registered.key? name
16
- Deserializer #{name} is already registered.
17
- ERROR
18
-
19
- raise ArgumentError, "Can't take both deserializer object and block." if deserializer && block
20
- @registered[name] = deserializer
21
- end
22
- end
8
+ registerable_as :deserializer
23
9
  end
24
10
  end
@@ -1,24 +1,9 @@
1
1
  require 'rosetta/exceptions'
2
+ require 'rosetta/support/registerable'
2
3
 
3
4
  module Rosetta
4
5
  module Serializers
5
- @registered = {}
6
-
7
- class << self
8
- attr_reader :registered
9
-
10
- def [](key)
11
- @registered[key]
12
- end
13
-
14
- def register(name, serializer, &block)
15
- raise ExistingSerializerError, <<-ERROR.strip if @registered.key? name
16
- Serializer #{name} is already registered.
17
- ERROR
18
-
19
- raise ArgumentError, "Can't take both serializer object and block." if serializer && block
20
- @registered[name] = serializer
21
- end
22
- end
6
+ extend Support::Registerable
7
+ registerable_as :serializer
23
8
  end
24
9
  end
@@ -0,0 +1,44 @@
1
+ require 'rosetta/exceptions'
2
+
3
+ module Rosetta
4
+ module Support
5
+ module Registerable
6
+ def registerable_as(nature)
7
+ @registered = {}
8
+
9
+ class << self
10
+ attr_reader :registered
11
+
12
+ def [](key)
13
+ registered[key]
14
+ end
15
+ end
16
+
17
+ define_singleton_method :register do |name, object=nil, &block|
18
+ #TODO: Feels clunky, string refinement maybe?
19
+ nature_words = nature.to_s.downcase
20
+ .split('_')
21
+ .map { |word| word[0].upcase + word[1..-1] }
22
+
23
+ camel_nature = nature_words.join
24
+ human_nature = nature_words.join(' ')
25
+
26
+ error_name = :"Existing#{camel_nature}Error"
27
+ error_class = if constants.include?(error_name)
28
+ const_get(error_name)
29
+ else
30
+ RegistrationError
31
+ end
32
+ raise error_class, <<-ERROR.strip if @registered.key? name
33
+ #{human_nature} #{name} is already registered.
34
+ ERROR
35
+
36
+ if object && block
37
+ raise ArgumentError, "Can't take both #{human_nature.downcase} object and block."
38
+ end
39
+ @registered[name] = object || block
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -1,28 +1,22 @@
1
1
  require 'rosetta/exceptions'
2
+ require 'rosetta/support/registerable'
2
3
 
3
4
  module Rosetta
4
5
  class Translation
5
- @registered = {}
6
- attr_reader :serializer, :deserializer, :translator
7
- alias_method :translator?, :translator
6
+ extend Support::Registerable
7
+ registerable_as :translator
8
8
 
9
9
  class << self
10
- attr_reader :registered
11
-
12
- def [](key)
13
- @registered[key]
14
- end
10
+ alias_method :register_key, :register
15
11
 
16
- def register(source, destination, callable = nil, &block)
17
- raise ExistingTranslatorError, <<-ERROR.strip if @registered.key? name
18
- There already is a translator from #{source} to #{destination}.
19
- ERROR
20
-
21
- raise ArgumentError, "Can't take both callable object and block." if callable && block
22
- @registered[source => destination] = callable || block
12
+ def register(source, destination, object=nil, &block)
13
+ register_key({ source => destination }, object, &block)
23
14
  end
24
15
  end
25
16
 
17
+ attr_reader :serializer, :deserializer, :translator
18
+ alias_method :translator?, :translator
19
+
26
20
  def initialize(deserializer, serializer)
27
21
  @translator = Translation[deserializer => serializer]
28
22
  unless @translator
@@ -1,3 +1,3 @@
1
1
  module Rosetta
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rosetta-stone
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jérémie Bonal
@@ -93,6 +93,7 @@ files:
93
93
  - lib/rosetta/serializers.rb
94
94
  - lib/rosetta/serializers/base.rb
95
95
  - lib/rosetta/serializers/csv.rb
96
+ - lib/rosetta/support/registerable.rb
96
97
  - lib/rosetta/translation.rb
97
98
  - lib/rosetta/version.rb
98
99
  - rosetta.gemspec