coder_decorator 1.0.3 → 1.1.0

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
  SHA1:
3
- metadata.gz: bd01c6c455219c669861df754a058c8a01ad5815
4
- data.tar.gz: caf592360fa166d310733957549d2615dd0e9d26
3
+ metadata.gz: a423e4cf25574fb8fc664fb87cfd4aeb6141db36
4
+ data.tar.gz: a7a5deaa59f369953372cc6503fb4ad2d0123a76
5
5
  SHA512:
6
- metadata.gz: 732e3a464d8d76e90e0782714423403d4ed941dae69198a82a9d4010cdbbc8deffd9e0349dec58a79629d9c28eb54391f41fbf633ad4fca04a235830331002be
7
- data.tar.gz: 593c66383229d8f924e1509de5d432876cc1578376eb391af1191f8eb75ee69c340beddb9340ac4cf2d7baede41f192d94cbd5c63492ceb113b1bb347a7fe085
6
+ metadata.gz: 7cea41ab6d8ce21c5e5ae102a840bc6322aedc8c9ebff76eeb7fbb0797c2a9c5282628cc3fa4af48ebcb7a12bca71a8d8e56739bc545848eeb0abf143c463a3c
7
+ data.tar.gz: 4e9a7ced2ee8742b3111a6ace93c60fdb8db2ad5bbab87c466b4ad1f289880223e18418208f1d639822d6f40777c1d3612011ff1536eec33f4a1b12d53656a56
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+ require 'coder_decorator/name_converter'
3
+ module CoderDecorator
4
+ # It provides a convenient interface to build a coder by passing arguments
5
+ # instead of tediously initializing and wrapping, for example:
6
+ #
7
+ # require 'coder_decorator/builder'
8
+ # CoderDecorator::Builder.build(:marshal, :base64)
9
+ #
10
+ # is equivalent to:
11
+ #
12
+ # require 'coder_decorator/coders/marshal'
13
+ # require 'coder_decorator/coders/base64'
14
+ # CoderDecorator::Coders::Marshal.new(CoderDecorator::Coders::Base64.new)
15
+ #
16
+ # To pass arguments to a coder, use array:
17
+ #
18
+ # CoderDecorator::Builder.build(:marshal, [:base64, {strict: true}])
19
+ #
20
+ class Builder
21
+ # equivalent to +new(*coder_names).build+
22
+ def self.build(*coder_names)
23
+ new(*coder_names).build
24
+ end
25
+
26
+ # all coder names are listed as filenames under +lib/coder_decorator/coders+,
27
+ def initialize(*coder_names)
28
+ @coder_names = coder_names
29
+ end
30
+
31
+ def build
32
+ coder = nil
33
+ @coder_names.each do |coder_name|
34
+ case coder_name
35
+ when String, Symbol, Array
36
+ name, *arguments = Array(coder_name)
37
+ coder_class = NameConverter.new(name).constantize
38
+ coder = coder_class.new(coder, *arguments)
39
+ else
40
+ raise ArgumentError, "Each argument should be one of Symbol, String or Array instance, but #{coder_name.inspect} was given."
41
+ end
42
+ end
43
+ coder
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+ module CoderDecorator
3
+ # It converts coder names into class name or constant, for example:
4
+ #
5
+ # NameConverter.new('base64').to_class_name # => 'Base64'
6
+ # NameConverter.new('base64').constantize # => Coders::Base64
7
+ # NameConverter.new('json').to_class_name # => 'JSON'
8
+ # NameConverter.new('json').to_class_name # => Coders::JSON
9
+ #
10
+ class NameConverter
11
+ def initialize(coder_name)
12
+ @coder_name = coder_name.to_s
13
+ end
14
+
15
+ def constantize
16
+ @constantize ||= begin
17
+ require_relative "coders/#{@coder_name}"
18
+ CoderDecorator.const_get("Coders::#{to_class_name}")
19
+ rescue LoadError
20
+ raise "The coder \"#{@coder_name}\" doesn't exist, use `CoderDecorator.coder_names` to see all available coders."
21
+ end
22
+ end
23
+
24
+ def to_class_name
25
+ case @coder_name
26
+ when 'hmac', 'json' then @coder_name.upcase
27
+ else @coder_name.to_s.split('_').map!(&:capitalize!).join
28
+ end
29
+ end
30
+ end
31
+ end
@@ -1,3 +1,11 @@
1
1
  # frozen_string_literal: true
2
- module CoderDecorator; end # :nodoc:
2
+ module CoderDecorator # :nodoc:
3
+ def self.coder_names
4
+ @coder_names ||= begin
5
+ coder_names = Dir["#{__dir__}/coder_decorator/coders/*"].map! { |name| File.basename(name, '.rb') }
6
+ coder_names.delete('coder')
7
+ coder_names
8
+ end
9
+ end
10
+ end
3
11
  Dir[File.join(__dir__, 'coder_decorator', '**', '*.rb')].each { |path| require path }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coder_decorator
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jian Weihang
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-09 00:00:00.000000000 Z
11
+ date: 2017-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -59,6 +59,7 @@ extensions: []
59
59
  extra_rdoc_files: []
60
60
  files:
61
61
  - lib/coder_decorator.rb
62
+ - lib/coder_decorator/builder.rb
62
63
  - lib/coder_decorator/coders/base64.rb
63
64
  - lib/coder_decorator/coders/cipher.rb
64
65
  - lib/coder_decorator/coders/coder.rb
@@ -69,6 +70,7 @@ files:
69
70
  - lib/coder_decorator/coders/rescue.rb
70
71
  - lib/coder_decorator/coders/zip.rb
71
72
  - lib/coder_decorator/errors.rb
73
+ - lib/coder_decorator/name_converter.rb
72
74
  homepage:
73
75
  licenses:
74
76
  - MIT