scash 0.0.4 → 0.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/scash.rb +35 -14
- data/lib/scash/version.rb +1 -1
- data/test/test_scash.rb +28 -1
- metadata +2 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: eab17874bb39585b1908492ff3167973886daa1a
         | 
| 4 | 
            +
              data.tar.gz: 8d79365ffce1fb0712e2dbdb0d196e87f165f558
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 4b648c6503c28de69b49dfdd2876d0954481ad61aceceeb66995c453abfb669182de5d68671c99158711a6c5854a95de8b68344ac2c9a7a13283568947aab60f
         | 
| 7 | 
            +
              data.tar.gz: a20b50700b57ef2d86ea35ac172685de49210a858151a38fb83861949a1ee4a13f2bd1814e4cac86c1ddcd62c36712afbfaa6ad02365ff782fee181ae3806983
         | 
    
        data/lib/scash.rb
    CHANGED
    
    | @@ -10,27 +10,34 @@ class Scash | |
| 10 10 |  | 
| 11 11 | 
             
              attr_reader :stack
         | 
| 12 12 |  | 
| 13 | 
            -
              def initialize(variables = {})
         | 
| 14 | 
            -
                @ | 
| 13 | 
            +
              def initialize(variables = {}, klass = HashWithIndifferentAccess)
         | 
| 14 | 
            +
                @klass = klass
         | 
| 15 | 
            +
                @stack = [convert(variables)]
         | 
| 16 | 
            +
                @hashes = [build_hash]
         | 
| 17 | 
            +
                @inverse_hashes = [build_inverse_hash]
         | 
| 18 | 
            +
                @global_variables = {}
         | 
| 15 19 | 
             
              end
         | 
| 16 20 |  | 
| 17 21 | 
             
              def to_hash
         | 
| 18 | 
            -
                @ | 
| 19 | 
            -
                  variables.merge hash
         | 
| 20 | 
            -
                end
         | 
| 22 | 
            +
                @hashes.first.merge(@global_variables)
         | 
| 21 23 | 
             
              end
         | 
| 22 24 |  | 
| 23 25 | 
             
              def to_inverse_hash
         | 
| 24 | 
            -
                @ | 
| 25 | 
            -
                  hash.merge variables
         | 
| 26 | 
            -
                end
         | 
| 26 | 
            +
                @inverse_hashes.first.merge(@global_variables)
         | 
| 27 27 | 
             
              end
         | 
| 28 28 |  | 
| 29 29 | 
             
              def scope(variables)
         | 
| 30 | 
            -
                @stack.unshift variables | 
| 30 | 
            +
                @stack.unshift convert(variables)
         | 
| 31 | 
            +
                added = true
         | 
| 32 | 
            +
                @hashes.unshift build_hash
         | 
| 33 | 
            +
                @inverse_hashes.unshift build_inverse_hash
         | 
| 31 34 | 
             
                yield
         | 
| 32 35 | 
             
              ensure
         | 
| 33 36 | 
             
                @stack.shift
         | 
| 37 | 
            +
                if added
         | 
| 38 | 
            +
                  @hashes.shift
         | 
| 39 | 
            +
                  @inverse_hashes.shift
         | 
| 40 | 
            +
                end
         | 
| 34 41 | 
             
              end
         | 
| 35 42 | 
             
              alias :with :scope
         | 
| 36 43 |  | 
| @@ -47,16 +54,30 @@ class Scash | |
| 47 54 | 
             
              end
         | 
| 48 55 |  | 
| 49 56 | 
             
              def define_global_variables(variables)
         | 
| 50 | 
            -
                 | 
| 51 | 
            -
                  delete_key(key)
         | 
| 52 | 
            -
                end
         | 
| 53 | 
            -
             | 
| 54 | 
            -
                @stack.push variables.with_indifferent_access
         | 
| 57 | 
            +
                @global_variables.merge! convert(variables)
         | 
| 55 58 | 
             
              end
         | 
| 56 59 |  | 
| 57 60 | 
             
              private
         | 
| 58 61 |  | 
| 62 | 
            +
              def build_hash(index = 0)
         | 
| 63 | 
            +
                @stack[index..-1].inject(@klass.new) do |hash, variables|
         | 
| 64 | 
            +
                  variables.merge hash
         | 
| 65 | 
            +
                end
         | 
| 66 | 
            +
              end
         | 
| 67 | 
            +
             | 
| 68 | 
            +
              def build_inverse_hash(index = 0)
         | 
| 69 | 
            +
                @stack[index..-1].inject(@klass.new) do |hash, variables|
         | 
| 70 | 
            +
                  hash.merge variables
         | 
| 71 | 
            +
                end
         | 
| 72 | 
            +
              end
         | 
| 73 | 
            +
             | 
| 59 74 | 
             
              def delete_key(key)
         | 
| 60 75 | 
             
                @stack.each{|hash|hash.delete(key)}
         | 
| 61 76 | 
             
              end
         | 
| 77 | 
            +
             | 
| 78 | 
            +
              def convert(variables)
         | 
| 79 | 
            +
                raise(ArgumentError, "Variables should respond to `keys`") unless variables.respond_to?("keys")
         | 
| 80 | 
            +
                @klass.new(variables)
         | 
| 81 | 
            +
              end
         | 
| 82 | 
            +
             | 
| 62 83 | 
             
            end
         | 
    
        data/lib/scash/version.rb
    CHANGED
    
    
    
        data/test/test_scash.rb
    CHANGED
    
    | @@ -53,7 +53,7 @@ class TestScash < Minitest::Test | |
| 53 53 |  | 
| 54 54 | 
             
                it "should keep scope instact when an error occurs" do
         | 
| 55 55 | 
             
                  scash = Scash.new
         | 
| 56 | 
            -
                  assert_raises( | 
| 56 | 
            +
                  assert_raises(ArgumentError) do
         | 
| 57 57 | 
             
                    scash.with(["a" => 1]) do
         | 
| 58 58 | 
             
                    end
         | 
| 59 59 | 
             
                  end
         | 
| @@ -74,6 +74,27 @@ class TestScash < Minitest::Test | |
| 74 74 | 
             
                  assert_equal 3, scash[:c]
         | 
| 75 75 | 
             
                end
         | 
| 76 76 |  | 
| 77 | 
            +
                it "should typecast correctly" do
         | 
| 78 | 
            +
                  scash = Scash.new({:a => 1})
         | 
| 79 | 
            +
                  assert_equal({"a" => 1}, scash.to_hash)
         | 
| 80 | 
            +
                  assert_equal HashWithIndifferentAccess, scash.to_hash.class
         | 
| 81 | 
            +
             | 
| 82 | 
            +
                  scash = Scash.new({:a => 1})
         | 
| 83 | 
            +
                  assert_equal({"a" => 1}, scash.to_inverse_hash)
         | 
| 84 | 
            +
                  assert_equal HashWithIndifferentAccess, scash.to_inverse_hash.class
         | 
| 85 | 
            +
             | 
| 86 | 
            +
                  class SubHash < HashWithIndifferentAccess
         | 
| 87 | 
            +
                  end
         | 
| 88 | 
            +
             | 
| 89 | 
            +
                  scash = Scash.new({:a => 1}, SubHash)
         | 
| 90 | 
            +
                  assert_equal({"a" => 1}, scash.to_hash)
         | 
| 91 | 
            +
                  assert_equal SubHash, scash.to_hash.class
         | 
| 92 | 
            +
             | 
| 93 | 
            +
                  scash = Scash.new({:a => 1}, SubHash)
         | 
| 94 | 
            +
                  assert_equal({"a" => 1}, scash.to_inverse_hash)
         | 
| 95 | 
            +
                  assert_equal SubHash, scash.to_inverse_hash.class
         | 
| 96 | 
            +
                end
         | 
| 97 | 
            +
             | 
| 77 98 | 
             
                it "should reuse instances" do
         | 
| 78 99 | 
             
                  class Foo
         | 
| 79 100 | 
             
                    attr_accessor :bar
         | 
| @@ -136,12 +157,18 @@ class TestScash < Minitest::Test | |
| 136 157 |  | 
| 137 158 | 
             
                    scash.with({:b => 2}) do
         | 
| 138 159 | 
             
                      assert_equal 1337, scash[:result]
         | 
| 160 | 
            +
                      assert_equal 2, scash[:b]
         | 
| 139 161 | 
             
                      scash.define_global_variables :result => "foo"
         | 
| 162 | 
            +
                      scash.define_global_variables :b => "bar"
         | 
| 163 | 
            +
                      assert_equal "bar", scash[:b]
         | 
| 140 164 | 
             
                      assert_equal "foo", scash[:result]
         | 
| 141 165 | 
             
                    end
         | 
| 142 166 |  | 
| 143 167 | 
             
                    assert_equal "foo", scash[:result]
         | 
| 144 168 | 
             
                  end
         | 
| 169 | 
            +
             | 
| 170 | 
            +
                  assert_equal "foo", scash[:result]
         | 
| 171 | 
            +
                  assert_equal "bar", scash[:b]
         | 
| 145 172 | 
             
                end
         | 
| 146 173 | 
             
              end
         | 
| 147 174 |  | 
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: scash
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.0 | 
| 4 | 
            +
              version: 0.1.0
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Stephan Kaag
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2013-11- | 
| 11 | 
            +
            date: 2013-11-13 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: activesupport
         |