hash_chain 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.
Files changed (7) hide show
  1. data/.document +5 -0
  2. data/Gemfile +6 -0
  3. data/LICENSE.txt +20 -0
  4. data/README.rdoc +11 -0
  5. data/Rakefile +33 -0
  6. data/lib/hash_chain.rb +69 -0
  7. metadata +75 -0
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "http://rubygems.org"
2
+
3
+ group :development do
4
+ gem "bundler", "~> 1.0.0"
5
+ gem "jeweler", "~> 1.6.4"
6
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Scott Tadman, The Working Group
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,11 @@
1
+ = hash_chain
2
+
3
+ This wrapper serves to combine several hashes into what functions like a
4
+ single Hash. This is useful when trying to fetch from a series of hashes in
5
+ some kind of priority sequence without duplicating the structure.
6
+
7
+ == Copyright
8
+
9
+ Copyright (c) 2011 Scott Tadman, The Working Group See LICENSE.txt for
10
+ further details.
11
+
@@ -0,0 +1,33 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "hash_chain"
18
+ gem.homepage = "http://github.com/twg/hash_chain"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Simple Multiple Hash Access}
21
+ gem.description = %Q{Chains multiple hashes together into a singular data-structure}
22
+ gem.email = "github@tadman.ca"
23
+ gem.authors = [ "Scott Tadman" ]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
@@ -0,0 +1,69 @@
1
+ class Hash::Chain
2
+ # == Constants ============================================================
3
+
4
+ # == Class Methods ========================================================
5
+
6
+ def self.[](*hashes)
7
+ new(*hashes)
8
+ end
9
+
10
+ # == Instance Methods =====================================================
11
+
12
+ def initialize(*hashes)
13
+ @hashes = hashes.flatten
14
+ end
15
+
16
+ def [](key)
17
+ @hashes.each do |hash|
18
+ if (hash.key?(key) or !hash[key].nil?)
19
+ return hash[key]
20
+ end
21
+ end
22
+
23
+ nil
24
+ end
25
+
26
+ def keys
27
+ @hashes.inject([ ]) { |a, h| a += h.keys }.uniq
28
+ end
29
+
30
+ def values
31
+ self.to_h.values
32
+ end
33
+
34
+ def key?(key)
35
+ !!@hashes.find { |hash| hash.key?(key) }
36
+ end
37
+
38
+ def empty?
39
+ !@hashes.find { |hash| !hash.empty? }
40
+ end
41
+
42
+ def length
43
+ self.keys.length
44
+ end
45
+
46
+ def each
47
+ self.keys.each do |key|
48
+ yield(key, self[key])
49
+ end
50
+ end
51
+
52
+ def to_h
53
+ combined = { }
54
+
55
+ @hashes.each do |hash|
56
+ hash.each do |key, value|
57
+ next if (combined.key?(key))
58
+
59
+ combined[key] = value
60
+ end
61
+ end
62
+
63
+ combined
64
+ end
65
+
66
+ def to_a
67
+ self.to_h.to_a
68
+ end
69
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hash_chain
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Scott Tadman
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-13 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: &2154612160 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.0.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *2154612160
25
+ - !ruby/object:Gem::Dependency
26
+ name: jeweler
27
+ requirement: &2154611160 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 1.6.4
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2154611160
36
+ description: Chains multiple hashes together into a singular data-structure
37
+ email: github@tadman.ca
38
+ executables: []
39
+ extensions: []
40
+ extra_rdoc_files:
41
+ - LICENSE.txt
42
+ - README.rdoc
43
+ files:
44
+ - .document
45
+ - Gemfile
46
+ - LICENSE.txt
47
+ - README.rdoc
48
+ - Rakefile
49
+ - lib/hash_chain.rb
50
+ homepage: http://github.com/twg/hash_chain
51
+ licenses:
52
+ - MIT
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubyforge_project:
71
+ rubygems_version: 1.8.10
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Simple Multiple Hash Access
75
+ test_files: []