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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +3 -1
- data/lib/rosetta/deserializers.rb +3 -17
- data/lib/rosetta/serializers.rb +3 -18
- data/lib/rosetta/support/registerable.rb +44 -0
- data/lib/rosetta/translation.rb +9 -15
- data/lib/rosetta/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a533462d58d84278dc028e4045c6eaaa31d85bbc57b0d84b973fc65e77ba9f54
|
4
|
+
data.tar.gz: ead94d4194d3f3cf9cc09e3fc4e4330ebe9d8b8f502a09ec89d972176fc1d695
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fab482ccd77877e6b8f9af64c196ef3834840e0263ea3b3bea806f2d7d1613ee64f2029c78efa4e3beec755cbacc19bdf7c28babd631dba54a1b847352003048
|
7
|
+
data.tar.gz: 1b0f519e1f16a1f980b3ca87a3cc0c041745aba1d26abef3c85320d2a0a68516b60d628cb459950c8639c4633645facb814c9da38c33aa2940d8cd4f27c00f78
|
data/Gemfile.lock
CHANGED
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
|
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
|
-
|
6
|
+
extend Support::Registerable
|
6
7
|
|
7
|
-
|
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
|
data/lib/rosetta/serializers.rb
CHANGED
@@ -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
|
-
|
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
|
data/lib/rosetta/translation.rb
CHANGED
@@ -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
|
-
|
6
|
-
|
7
|
-
alias_method :translator?, :translator
|
6
|
+
extend Support::Registerable
|
7
|
+
registerable_as :translator
|
8
8
|
|
9
9
|
class << self
|
10
|
-
|
11
|
-
|
12
|
-
def [](key)
|
13
|
-
@registered[key]
|
14
|
-
end
|
10
|
+
alias_method :register_key, :register
|
15
11
|
|
16
|
-
def register(source, destination,
|
17
|
-
|
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
|
data/lib/rosetta/version.rb
CHANGED
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.
|
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
|