hash_transformer 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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Trampoline Systems Ltd
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.
data/README.rdoc ADDED
@@ -0,0 +1,28 @@
1
+ = hash_transformer
2
+
3
+ create HashTransformers for Hashes with a simple DSL. a HashTransformer
4
+ applies a sequence of transforms to a Hash to produce a new Hash
5
+
6
+ require 'hash_transformer'
7
+ t = HashTransformer.new do
8
+ delete_key :foo
9
+ map_key :bar, :baz
10
+ map_key :foobar, :foobaz
11
+ end
12
+
13
+ t.transform(:foo=>1, :bar=>2, :foobar=>3)
14
+ => {:baz=>2, :foobaz=>3}
15
+
16
+ == Note on Patches/Pull Requests
17
+
18
+ * Fork the project.
19
+ * Make your feature addition or bug fix.
20
+ * Add tests for it. This is important so I don't break it in a
21
+ future version unintentionally.
22
+ * Commit, do not mess with rakefile, version, or history.
23
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
24
+ * Send me a pull request. Bonus points for topic branches.
25
+
26
+ == Copyright
27
+
28
+ Copyright (c) 2010 Trampoline Systems Ltd. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "hash_transformer"
8
+ gem.summary = %Q{create hash transformers with a simple dsl}
9
+ gem.description = %Q{use a dsl to create hash transformers which transform hashes by applying a sequence of transforms}
10
+ gem.email = "mccraigmccraig@gmail.com"
11
+ gem.homepage = "http://github.com/mccraigmccraig/hash_transformer"
12
+ gem.authors = ["mccraigmccraig of the clan mccraig"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "hash_transformer #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,65 @@
1
+ class HashTransformer
2
+
3
+ class HashTransformerDsl
4
+ attr_reader :transforms
5
+
6
+ def initialize(transforms)
7
+ @transforms = transforms
8
+ end
9
+
10
+ def configure(&proc)
11
+ self.instance_eval(&proc)
12
+ end
13
+
14
+ def delete_key(key)
15
+ transforms << DeleteKeyTransform.new(key)
16
+ end
17
+
18
+ def map_key(key, to)
19
+ transforms << MapKeyTransform.new(key, to)
20
+ end
21
+ end
22
+
23
+ class DeleteKeyTransform
24
+ attr_reader :key
25
+
26
+ def initialize(key)
27
+ @key = key
28
+ end
29
+
30
+ def transform(h)
31
+ h.delete(@key)
32
+ h
33
+ end
34
+ end
35
+
36
+ class MapKeyTransform
37
+ attr_reader :key
38
+ attr_reader :to
39
+
40
+ def initialize(key, to)
41
+ @key = key
42
+ @to = to
43
+ end
44
+
45
+ def transform(h)
46
+ if h.has_key?(@key)
47
+ v = h.delete(@key)
48
+ raise "can't map key #{key.inspect} to #{to.inspect} : target key exists" if h.has_key?(@to)
49
+ h[@to] = v
50
+ end
51
+ h
52
+ end
53
+ end
54
+
55
+ attr_reader :transforms
56
+
57
+ def initialize(&proc)
58
+ @transforms = []
59
+ HashTransformerDsl.new(@transforms).configure(&proc)
60
+ end
61
+
62
+ def transform(h)
63
+ transforms.reduce(h){|r,transform| transform.transform(r) ; r}
64
+ end
65
+ end
@@ -0,0 +1,26 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "HashTransformer" do
4
+ describe "DeleteKeyTransform" do
5
+ it "should delete keys" do
6
+ t = HashTransformer.new{ delete_key(:foo) }
7
+ t.transform(:foo=>1).should == {}
8
+ t.transform(:foo=>1, :bar=>2).should == {:bar=>2}
9
+ end
10
+ end
11
+
12
+ describe "MapKeyTransform" do
13
+ it "should map keys" do
14
+ t = HashTransformer.new{ map_key :foo, :bar }
15
+ t.transform(:foo=>1).should == {:bar=>1}
16
+ t.transform(:foo=>1, :baz=>2).should == {:bar=>1, :baz=>2}
17
+ end
18
+
19
+ it "should bork if target key exists" do
20
+ t = HashTransformer.new{ map_key :foo, :bar }
21
+ lambda {
22
+ t.transform(:foo=>1, :bar=>2)
23
+ }.should raise_error(RuntimeError)
24
+ end
25
+ end
26
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'hash_transformer'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+ config.mock_with :rr
9
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hash_transformer
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - mccraigmccraig of the clan mccraig
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-27 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 1
32
+ - 2
33
+ - 9
34
+ version: 1.2.9
35
+ type: :development
36
+ version_requirements: *id001
37
+ description: use a dsl to create hash transformers which transform hashes by applying a sequence of transforms
38
+ email: mccraigmccraig@gmail.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - LICENSE
45
+ - README.rdoc
46
+ files:
47
+ - .document
48
+ - .gitignore
49
+ - LICENSE
50
+ - README.rdoc
51
+ - Rakefile
52
+ - VERSION
53
+ - lib/hash_transformer.rb
54
+ - spec/hash_transformer_spec.rb
55
+ - spec/spec.opts
56
+ - spec/spec_helper.rb
57
+ has_rdoc: true
58
+ homepage: http://github.com/mccraigmccraig/hash_transformer
59
+ licenses: []
60
+
61
+ post_install_message:
62
+ rdoc_options:
63
+ - --charset=UTF-8
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ hash: 3
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ requirements: []
85
+
86
+ rubyforge_project:
87
+ rubygems_version: 1.3.7
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: create hash transformers with a simple dsl
91
+ test_files:
92
+ - spec/hash_transformer_spec.rb
93
+ - spec/spec_helper.rb