yas 0.2.1 → 0.2.2
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/yas/ext/duplicate.rb +42 -0
- data/lib/yas/schema.rb +1 -0
- data/lib/yas/version.rb +1 -1
- data/lib/yas.rb +1 -0
- data/test/ext/test_duplicate.rb +27 -0
- data/yas.gemspec +1 -1
- metadata +6 -4
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: d2d1f54023a01907f599f48cca90bb9242a62089
         | 
| 4 | 
            +
              data.tar.gz: 07c833f91eea25fcd26c65d07e2aa841a112e8a0
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 55eb0916f52e24c460bb6052694375ffb916f1428b3a700fca8d9c2ed98cf531818b2f2c8ad0638ddd81d41656b2ffe0bf6a46e209d45ee639747b66b1f255d0
         | 
| 7 | 
            +
              data.tar.gz: 45f84dab4db77099310d49bf9236c4ee622bb6f98b113b53d7de1c2b17ad9b7d9f3b264cb25438f0026d6715d7d07f937bdf8c2667aead73f94b54d62a0f9e1d
         | 
| @@ -0,0 +1,42 @@ | |
| 1 | 
            +
            # Duplicate value of a key
         | 
| 2 | 
            +
            #
         | 
| 3 | 
            +
            # Usage:
         | 
| 4 | 
            +
            #
         | 
| 5 | 
            +
            # duplicate :mykey => [:other_key, :another_key]
         | 
| 6 | 
            +
            #
         | 
| 7 | 
            +
             | 
| 8 | 
            +
            class YAS::DuplicateExt
         | 
| 9 | 
            +
             | 
| 10 | 
            +
              module ClassMethods
         | 
| 11 | 
            +
                def duplicate map
         | 
| 12 | 
            +
                  duplicate_keys.merge!(map)
         | 
| 13 | 
            +
                end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                def duplicate_keys
         | 
| 16 | 
            +
                  @duplicate_keys ||= {}
         | 
| 17 | 
            +
                end
         | 
| 18 | 
            +
              end
         | 
| 19 | 
            +
             | 
| 20 | 
            +
             | 
| 21 | 
            +
              def self.when_used schema
         | 
| 22 | 
            +
                schema.extend ClassMethods
         | 
| 23 | 
            +
              end
         | 
| 24 | 
            +
             | 
| 25 | 
            +
             | 
| 26 | 
            +
              def self.when_schema_inherited superschema, subschema
         | 
| 27 | 
            +
                superschema.duplicate_keys.each do |from, to|
         | 
| 28 | 
            +
                  subschema.duplicate_keys[key] = to
         | 
| 29 | 
            +
                end
         | 
| 30 | 
            +
              end
         | 
| 31 | 
            +
             | 
| 32 | 
            +
             | 
| 33 | 
            +
              def self.apply schema, hash
         | 
| 34 | 
            +
                schema.duplicate_keys.each do |from, to|
         | 
| 35 | 
            +
                  if hash.has_key?(from)
         | 
| 36 | 
            +
                    to = Array(to)
         | 
| 37 | 
            +
                    to.each { |t| hash[t] = hash[from] }
         | 
| 38 | 
            +
                  end
         | 
| 39 | 
            +
                end
         | 
| 40 | 
            +
              end
         | 
| 41 | 
            +
             | 
| 42 | 
            +
            end
         | 
    
        data/lib/yas/schema.rb
    CHANGED
    
    
    
        data/lib/yas/version.rb
    CHANGED
    
    
    
        data/lib/yas.rb
    CHANGED
    
    
| @@ -0,0 +1,27 @@ | |
| 1 | 
            +
            require './test/helper.rb'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            class DuplicateSchema < YAS::Schema
         | 
| 4 | 
            +
              duplicate :addr => :address
         | 
| 5 | 
            +
              duplicate 'home' => ['office', :temp]
         | 
| 6 | 
            +
            end
         | 
| 7 | 
            +
             | 
| 8 | 
            +
             | 
| 9 | 
            +
             | 
| 10 | 
            +
            class TestDuplicateExt < Minitest::Test
         | 
| 11 | 
            +
             | 
| 12 | 
            +
              def test_duplicate_ext
         | 
| 13 | 
            +
                hash = {
         | 
| 14 | 
            +
                  addr: "Some address",
         | 
| 15 | 
            +
                  'home' => 'home address',
         | 
| 16 | 
            +
                  untouched: "Nothing"
         | 
| 17 | 
            +
                }
         | 
| 18 | 
            +
                hash.validate! DuplicateSchema
         | 
| 19 | 
            +
                assert_equal "Some address", hash[:addr]
         | 
| 20 | 
            +
                assert_equal "Some address", hash[:address]
         | 
| 21 | 
            +
                assert_equal "home address", hash['home']
         | 
| 22 | 
            +
                assert_equal "home address", hash['office']
         | 
| 23 | 
            +
                assert_equal "home address", hash[:temp]
         | 
| 24 | 
            +
                assert_equal "Nothing", hash[:untouched]
         | 
| 25 | 
            +
              end
         | 
| 26 | 
            +
             | 
| 27 | 
            +
            end
         | 
    
        data/yas.gemspec
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: yas
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.2. | 
| 4 | 
            +
              version: 0.2.2
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Albert Tedja
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date:  | 
| 11 | 
            +
            date: 2016-02-04 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: minitest
         | 
| @@ -64,6 +64,7 @@ files: | |
| 64 64 | 
             
            - lib/yas.rb
         | 
| 65 65 | 
             
            - lib/yas/errors.rb
         | 
| 66 66 | 
             
            - lib/yas/ext/attribute.rb
         | 
| 67 | 
            +
            - lib/yas/ext/duplicate.rb
         | 
| 67 68 | 
             
            - lib/yas/ext/migrate.rb
         | 
| 68 69 | 
             
            - lib/yas/ext/rename.rb
         | 
| 69 70 | 
             
            - lib/yas/ext/symbolize.rb
         | 
| @@ -72,6 +73,7 @@ files: | |
| 72 73 | 
             
            - lib/yas/schema.rb
         | 
| 73 74 | 
             
            - lib/yas/version.rb
         | 
| 74 75 | 
             
            - test/ext/test_attribute.rb
         | 
| 76 | 
            +
            - test/ext/test_duplicate.rb
         | 
| 75 77 | 
             
            - test/ext/test_migrate.rb
         | 
| 76 78 | 
             
            - test/ext/test_rename.rb
         | 
| 77 79 | 
             
            - test/ext/test_symbolize.rb
         | 
| @@ -81,7 +83,7 @@ files: | |
| 81 83 | 
             
            - yas.gemspec
         | 
| 82 84 | 
             
            homepage: https://github.com/atedja/yas
         | 
| 83 85 | 
             
            licenses:
         | 
| 84 | 
            -
            - Apache
         | 
| 86 | 
            +
            - Apache-2.0
         | 
| 85 87 | 
             
            metadata: {}
         | 
| 86 88 | 
             
            post_install_message: 
         | 
| 87 89 | 
             
            rdoc_options: []
         | 
| @@ -99,7 +101,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement | |
| 99 101 | 
             
                  version: '0'
         | 
| 100 102 | 
             
            requirements: []
         | 
| 101 103 | 
             
            rubyforge_project: 
         | 
| 102 | 
            -
            rubygems_version: 2. | 
| 104 | 
            +
            rubygems_version: 2.5.2
         | 
| 103 105 | 
             
            signing_key: 
         | 
| 104 106 | 
             
            specification_version: 4
         | 
| 105 107 | 
             
            summary: Yet Another Schema for Ruby.
         |