hash_wia 0.7.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 +7 -0
- data/.version +1 -0
- data/lib/hash_wia.rb +3 -0
- data/lib/hash_wia/class.rb +4 -0
- data/lib/hash_wia/module.rb +68 -0
- data/lib/hash_wia/pollute.rb +19 -0
- metadata +62 -0
    
        checksums.yaml
    ADDED
    
    | @@ -0,0 +1,7 @@ | |
| 1 | 
            +
            ---
         | 
| 2 | 
            +
            SHA256:
         | 
| 3 | 
            +
              metadata.gz: aeccc594bd04c928359e6e4edfedce07ec39795512f934ed2cb244d0df5d1e1e
         | 
| 4 | 
            +
              data.tar.gz: dd3726a0d81b2f0bd61815cc587ced21dbe3fd5c575aa343fe80e79da282b9a7
         | 
| 5 | 
            +
            SHA512:
         | 
| 6 | 
            +
              metadata.gz: b8f552a63fad26766191bdf60baed5a9f75628b55f85af832ba795e9ff51f326c6e960bc4e6a9f654aa6152dcea912e43f2a421a7dd61aaabfb9a62f64df65fd
         | 
| 7 | 
            +
              data.tar.gz: c7744391d5fb655c553959de259e93af354af855a9d62a3276a2dd323a00150c1a1c62908b840cb3d49d391490c5f5de0655ad087dfa51d5305156d338131dc1
         | 
    
        data/.version
    ADDED
    
    | @@ -0,0 +1 @@ | |
| 1 | 
            +
            0.7.0
         | 
    
        data/lib/hash_wia.rb
    ADDED
    
    
| @@ -0,0 +1,68 @@ | |
| 1 | 
            +
            module HashWiaModule
         | 
| 2 | 
            +
              def initialize hash=nil
         | 
| 3 | 
            +
                if hash
         | 
| 4 | 
            +
                  hash.each { |k,v| self[k] = v }
         | 
| 5 | 
            +
                end
         | 
| 6 | 
            +
              end
         | 
| 7 | 
            +
             | 
| 8 | 
            +
              def [] key
         | 
| 9 | 
            +
                data = super key
         | 
| 10 | 
            +
                data = super key.to_s if data.nil?
         | 
| 11 | 
            +
                data = super key.to_sym if key.respond_to?(:to_sym) && data.nil?
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                # if we are returning hash as a value, just include with wia methods hash
         | 
| 14 | 
            +
                data.extend HashWiaModule if data.is_a?(Hash)
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                data
         | 
| 17 | 
            +
              end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
              def []= key, value
         | 
| 20 | 
            +
                delete key
         | 
| 21 | 
            +
                delete key.to_s
         | 
| 22 | 
            +
                delete key.to_sym if key.respond_to?(:to_sym)
         | 
| 23 | 
            +
             | 
| 24 | 
            +
                super key, value
         | 
| 25 | 
            +
              end
         | 
| 26 | 
            +
             | 
| 27 | 
            +
              # we never return array from hash, ruby internals
         | 
| 28 | 
            +
              def to_ary
         | 
| 29 | 
            +
                nil
         | 
| 30 | 
            +
              end
         | 
| 31 | 
            +
             | 
| 32 | 
            +
              # key is common id direct access
         | 
| 33 | 
            +
              # allow direct get or fuction the same if name given
         | 
| 34 | 
            +
              def key name=nil
         | 
| 35 | 
            +
                name ? self[name] : self[:key]
         | 
| 36 | 
            +
              end
         | 
| 37 | 
            +
             | 
| 38 | 
            +
              # true clone of the hash with 0 references to the old one
         | 
| 39 | 
            +
              def clone
         | 
| 40 | 
            +
                Marshal.load(Marshal.dump(self))
         | 
| 41 | 
            +
              end
         | 
| 42 | 
            +
             | 
| 43 | 
            +
              def method_missing name, *args, &block
         | 
| 44 | 
            +
                strname = name.to_s
         | 
| 45 | 
            +
             | 
| 46 | 
            +
                if strname.sub!(/\?$/, '')
         | 
| 47 | 
            +
                  # h.foo?
         | 
| 48 | 
            +
                  !!self[strname]
         | 
| 49 | 
            +
                elsif strname.sub!(/=$/, '')
         | 
| 50 | 
            +
                  # h.foo = :bar
         | 
| 51 | 
            +
                  self[strname.to_sym] = args.first
         | 
| 52 | 
            +
                else
         | 
| 53 | 
            +
                  value = self[strname]
         | 
| 54 | 
            +
             | 
| 55 | 
            +
                  if value.nil?
         | 
| 56 | 
            +
                    if block
         | 
| 57 | 
            +
                      # h.foo { rand }
         | 
| 58 | 
            +
                      self[name] = block
         | 
| 59 | 
            +
                    else
         | 
| 60 | 
            +
                      # h.foo
         | 
| 61 | 
            +
                      raise ArgumentError.new('%s not defined' % strname)
         | 
| 62 | 
            +
                    end
         | 
| 63 | 
            +
                  else
         | 
| 64 | 
            +
                    value
         | 
| 65 | 
            +
                  end
         | 
| 66 | 
            +
                end
         | 
| 67 | 
            +
              end
         | 
| 68 | 
            +
            end
         | 
| @@ -0,0 +1,19 @@ | |
| 1 | 
            +
            class Hash
         | 
| 2 | 
            +
              # { foo: :bar }.to_hwia            #
         | 
| 3 | 
            +
              # { foo: :bar }.to_hwia :foo, :bar # create struct and fill
         | 
| 4 | 
            +
              def to_hwia *args
         | 
| 5 | 
            +
                unless args.first
         | 
| 6 | 
            +
                  HashWia.new self
         | 
| 7 | 
            +
                else
         | 
| 8 | 
            +
                  list = args.flatten
         | 
| 9 | 
            +
                  name = 'DynStruct_' + list.join('_')
         | 
| 10 | 
            +
                  HashWia::STRUCTS[name] ||= ::Struct.new(name, *list)
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                  HashWia::STRUCTS[name].new.tap do |o|
         | 
| 13 | 
            +
                    each do |k, v|
         | 
| 14 | 
            +
                      o.send('%s=' % k, v) unless v.nil?
         | 
| 15 | 
            +
                    end
         | 
| 16 | 
            +
                  end
         | 
| 17 | 
            +
                end
         | 
| 18 | 
            +
              end
         | 
| 19 | 
            +
            end
         | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,62 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification
         | 
| 2 | 
            +
            name: hash_wia
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            +
              version: 0.7.0
         | 
| 5 | 
            +
            platform: ruby
         | 
| 6 | 
            +
            authors:
         | 
| 7 | 
            +
            - Dino Reic
         | 
| 8 | 
            +
            autorequire: 
         | 
| 9 | 
            +
            bindir: bin
         | 
| 10 | 
            +
            cert_chain: []
         | 
| 11 | 
            +
            date: 2020-05-28 00:00:00.000000000 Z
         | 
| 12 | 
            +
            dependencies:
         | 
| 13 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 14 | 
            +
              name: hashie
         | 
| 15 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 16 | 
            +
                requirements:
         | 
| 17 | 
            +
                - - ">="
         | 
| 18 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 19 | 
            +
                    version: '0'
         | 
| 20 | 
            +
              type: :runtime
         | 
| 21 | 
            +
              prerelease: false
         | 
| 22 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 23 | 
            +
                requirements:
         | 
| 24 | 
            +
                - - ">="
         | 
| 25 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 26 | 
            +
                    version: '0'
         | 
| 27 | 
            +
            description: Gem provides simple access to common Ruby hash types bundled in one simple
         | 
| 28 | 
            +
              class
         | 
| 29 | 
            +
            email: reic.dino@gmail.com
         | 
| 30 | 
            +
            executables: []
         | 
| 31 | 
            +
            extensions: []
         | 
| 32 | 
            +
            extra_rdoc_files: []
         | 
| 33 | 
            +
            files:
         | 
| 34 | 
            +
            - "./.version"
         | 
| 35 | 
            +
            - "./lib/hash_wia.rb"
         | 
| 36 | 
            +
            - "./lib/hash_wia/class.rb"
         | 
| 37 | 
            +
            - "./lib/hash_wia/module.rb"
         | 
| 38 | 
            +
            - "./lib/hash_wia/pollute.rb"
         | 
| 39 | 
            +
            homepage: https://github.com/dux/hash_wia
         | 
| 40 | 
            +
            licenses:
         | 
| 41 | 
            +
            - MIT
         | 
| 42 | 
            +
            metadata: {}
         | 
| 43 | 
            +
            post_install_message: 
         | 
| 44 | 
            +
            rdoc_options: []
         | 
| 45 | 
            +
            require_paths:
         | 
| 46 | 
            +
            - lib
         | 
| 47 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 48 | 
            +
              requirements:
         | 
| 49 | 
            +
              - - ">="
         | 
| 50 | 
            +
                - !ruby/object:Gem::Version
         | 
| 51 | 
            +
                  version: '0'
         | 
| 52 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 53 | 
            +
              requirements:
         | 
| 54 | 
            +
              - - ">="
         | 
| 55 | 
            +
                - !ruby/object:Gem::Version
         | 
| 56 | 
            +
                  version: '0'
         | 
| 57 | 
            +
            requirements: []
         | 
| 58 | 
            +
            rubygems_version: 3.0.6
         | 
| 59 | 
            +
            signing_key: 
         | 
| 60 | 
            +
            specification_version: 4
         | 
| 61 | 
            +
            summary: Hash with indifferent access + goodies
         | 
| 62 | 
            +
            test_files: []
         |