coder_decorator 1.0.3 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
 - data/lib/coder_decorator/builder.rb +46 -0
 - data/lib/coder_decorator/name_converter.rb +31 -0
 - data/lib/coder_decorator.rb +9 -1
 - metadata +4 -2
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA1:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: a423e4cf25574fb8fc664fb87cfd4aeb6141db36
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: a7a5deaa59f369953372cc6503fb4ad2d0123a76
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 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
         
     | 
    
        data/lib/coder_decorator.rb
    CHANGED
    
    | 
         @@ -1,3 +1,11 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            # frozen_string_literal: true
         
     | 
| 
       2 
     | 
    
         
            -
            module CoderDecorator 
     | 
| 
      
 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 
     | 
| 
      
 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- 
     | 
| 
      
 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
         
     |