document_hash 0.0.13 → 0.0.14
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.
- data/document_hash.gemspec +1 -0
- data/lib/document_hash/core.rb +51 -0
- data/lib/document_hash/version.rb +1 -1
- data/spec/lib/document_spec.rb +45 -0
- data/spec/lib/nil_spec.rb +9 -0
- metadata +20 -2
    
        data/document_hash.gemspec
    CHANGED
    
    
    
        data/lib/document_hash/core.rb
    CHANGED
    
    | @@ -1,5 +1,43 @@ | |
| 1 1 | 
             
            module DocumentHash
         | 
| 2 | 
            +
             | 
| 3 | 
            +
              class Nil
         | 
| 4 | 
            +
                attr_accessor :parent
         | 
| 5 | 
            +
                def path
         | 
| 6 | 
            +
                  @path ||= []
         | 
| 7 | 
            +
                end
         | 
| 8 | 
            +
             | 
| 9 | 
            +
                def initialize parent = nil, hash_path = nil
         | 
| 10 | 
            +
                  @parent = parent
         | 
| 11 | 
            +
                  @path = hash_path 
         | 
| 12 | 
            +
                end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                def nil?
         | 
| 15 | 
            +
                  true
         | 
| 16 | 
            +
                end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                def == val
         | 
| 19 | 
            +
                  val == false 
         | 
| 20 | 
            +
                end
         | 
| 21 | 
            +
             | 
| 22 | 
            +
                def method_missing method, *args
         | 
| 23 | 
            +
                  if method =~ /^(.*)=/
         | 
| 24 | 
            +
                    parent.__send__ :create_path, (self.path << $1.to_sym), args.pop
         | 
| 25 | 
            +
                  else
         | 
| 26 | 
            +
                    return self.class.new( self.parent, self.path << method)
         | 
| 27 | 
            +
                  end
         | 
| 28 | 
            +
                end
         | 
| 29 | 
            +
              end
         | 
| 30 | 
            +
             | 
| 2 31 | 
             
              class Core < Hash
         | 
| 32 | 
            +
             | 
| 33 | 
            +
                def method_missing method, *args
         | 
| 34 | 
            +
                  if method =~ /^(.*)=$/
         | 
| 35 | 
            +
                    self.__send__(:[]=, method[0..-2], args.pop)
         | 
| 36 | 
            +
                  else
         | 
| 37 | 
            +
                    self.__send__(:[], method) || DocumentHash::Nil.new(self, [method])
         | 
| 38 | 
            +
                  end
         | 
| 39 | 
            +
                end
         | 
| 40 | 
            +
             | 
| 3 41 | 
             
                def self.[] hash, parent = nil, parent_key = nil
         | 
| 4 42 | 
             
                  super(hash).tap do|new|
         | 
| 5 43 | 
             
                    new.__send__ :parent=, parent if parent
         | 
| @@ -101,6 +139,19 @@ module DocumentHash | |
| 101 139 | 
             
                  @changed ||= ::Set.new
         | 
| 102 140 | 
             
                end
         | 
| 103 141 |  | 
| 142 | 
            +
                def create_path path, value
         | 
| 143 | 
            +
                  curr = self
         | 
| 144 | 
            +
                  path.each_with_index do |key, index|
         | 
| 145 | 
            +
                    unless index == path.size - 1
         | 
| 146 | 
            +
                      curr[key] = self.class.new
         | 
| 147 | 
            +
                      curr = curr[key]
         | 
| 148 | 
            +
                    else
         | 
| 149 | 
            +
                      curr[key] = value
         | 
| 150 | 
            +
                    end
         | 
| 151 | 
            +
                  end
         | 
| 152 | 
            +
                  curr = value
         | 
| 153 | 
            +
                end
         | 
| 154 | 
            +
             | 
| 104 155 | 
             
                def self.symbolize_keys hash
         | 
| 105 156 | 
             
                  hash.keys.each do |key|
         | 
| 106 157 | 
             
                    hash[(key.to_sym rescue key) || key] = hash.delete(key)
         | 
    
        data/spec/lib/document_spec.rb
    CHANGED
    
    | @@ -170,4 +170,49 @@ describe DocumentHash::Core do | |
| 170 170 | 
             
                hash = subject.to_hash
         | 
| 171 171 | 
             
                hash[:test].should be_an_instance_of Hash
         | 
| 172 172 | 
             
              end
         | 
| 173 | 
            +
             | 
| 174 | 
            +
              it "can access values thru methods" do
         | 
| 175 | 
            +
                subject = DocumentHash::Core[{ test: "value" }]
         | 
| 176 | 
            +
                subject.test.should == "value"
         | 
| 177 | 
            +
              end
         | 
| 178 | 
            +
             | 
| 179 | 
            +
              it "can access values thru methods at deeper levels" do
         | 
| 180 | 
            +
                subject = DocumentHash::Core[{ test: { inner: "value" } }]
         | 
| 181 | 
            +
                subject.test.inner.should == "value"
         | 
| 182 | 
            +
              end
         | 
| 183 | 
            +
             | 
| 184 | 
            +
              it "returns nil for unknown values" do
         | 
| 185 | 
            +
                subject = DocumentHash::Core[{ test: { inner: "value" } }]
         | 
| 186 | 
            +
                subject.unknown.should be_nil
         | 
| 187 | 
            +
              end
         | 
| 188 | 
            +
             | 
| 189 | 
            +
              it "returns nil for unknown values at deeper levels" do
         | 
| 190 | 
            +
                subject = DocumentHash::Core[{ test: { inner: "value" } }]
         | 
| 191 | 
            +
                subject.unknown.inner.should be_nil
         | 
| 192 | 
            +
              end
         | 
| 193 | 
            +
             | 
| 194 | 
            +
              it "assigns a new value if unknown value is received" do
         | 
| 195 | 
            +
                subject = DocumentHash::Core[{ test: { inner: "value" } }]
         | 
| 196 | 
            +
                subject.unknown = 'test'
         | 
| 197 | 
            +
                subject.unknown.should == 'test'
         | 
| 198 | 
            +
              end
         | 
| 199 | 
            +
             | 
| 200 | 
            +
              it "assigns a new value if unknown value is received at deeper levels" do
         | 
| 201 | 
            +
                subject = DocumentHash::Core[{ test: { inner: "value" } }]
         | 
| 202 | 
            +
                subject.unknown.inner = 'test'
         | 
| 203 | 
            +
                subject.unknown.inner.should == 'test'
         | 
| 204 | 
            +
              end
         | 
| 205 | 
            +
             | 
| 206 | 
            +
              it "can assign values thru methods" do
         | 
| 207 | 
            +
                subject = DocumentHash::Core[{ test: "value" }]
         | 
| 208 | 
            +
                subject.test = "new"
         | 
| 209 | 
            +
                subject.test.should == "new"
         | 
| 210 | 
            +
              end
         | 
| 211 | 
            +
             | 
| 212 | 
            +
              it "can assign values thru methods at deeper levels" do
         | 
| 213 | 
            +
                subject = DocumentHash::Core[{ test: { inner: "value" } }]
         | 
| 214 | 
            +
                subject.test.inner = "new"
         | 
| 215 | 
            +
                subject.test.inner.should == "new"
         | 
| 216 | 
            +
              end
         | 
| 217 | 
            +
             | 
| 173 218 | 
             
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: document_hash
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.0. | 
| 4 | 
            +
              version: 0.0.14
         | 
| 5 5 | 
             
              prerelease: 
         | 
| 6 6 | 
             
            platform: ruby
         | 
| 7 7 | 
             
            authors:
         | 
| @@ -9,7 +9,7 @@ authors: | |
| 9 9 | 
             
            autorequire: 
         | 
| 10 10 | 
             
            bindir: bin
         | 
| 11 11 | 
             
            cert_chain: []
         | 
| 12 | 
            -
            date: 2013-11- | 
| 12 | 
            +
            date: 2013-11-13 00:00:00.000000000 Z
         | 
| 13 13 | 
             
            dependencies:
         | 
| 14 14 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 15 15 | 
             
              name: rspec
         | 
| @@ -27,6 +27,22 @@ dependencies: | |
| 27 27 | 
             
                - - ! '>='
         | 
| 28 28 | 
             
                  - !ruby/object:Gem::Version
         | 
| 29 29 | 
             
                    version: '0'
         | 
| 30 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 31 | 
            +
              name: debugger
         | 
| 32 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 33 | 
            +
                none: false
         | 
| 34 | 
            +
                requirements:
         | 
| 35 | 
            +
                - - ! '>='
         | 
| 36 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 37 | 
            +
                    version: '0'
         | 
| 38 | 
            +
              type: :development
         | 
| 39 | 
            +
              prerelease: false
         | 
| 40 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 41 | 
            +
                none: false
         | 
| 42 | 
            +
                requirements:
         | 
| 43 | 
            +
                - - ! '>='
         | 
| 44 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 45 | 
            +
                    version: '0'
         | 
| 30 46 | 
             
            description: Implements a multi-level nested document, that notifies about changes,
         | 
| 31 47 | 
             
              and some other related features
         | 
| 32 48 | 
             
            email:
         | 
| @@ -49,6 +65,7 @@ files: | |
| 49 65 | 
             
            - lib/document_hash/core.rb
         | 
| 50 66 | 
             
            - lib/document_hash/version.rb
         | 
| 51 67 | 
             
            - spec/lib/document_spec.rb
         | 
| 68 | 
            +
            - spec/lib/nil_spec.rb
         | 
| 52 69 | 
             
            - spec/spec_helper.rb
         | 
| 53 70 | 
             
            homepage: ''
         | 
| 54 71 | 
             
            licenses: []
         | 
| @@ -76,4 +93,5 @@ specification_version: 3 | |
| 76 93 | 
             
            summary: Document Object
         | 
| 77 94 | 
             
            test_files:
         | 
| 78 95 | 
             
            - spec/lib/document_spec.rb
         | 
| 96 | 
            +
            - spec/lib/nil_spec.rb
         | 
| 79 97 | 
             
            - spec/spec_helper.rb
         |