casted_hash 0.0.2

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 901edabae24fed434f5c0a5c6e83845831ea0163
4
+ data.tar.gz: 9cc31472995f046c5bbecc736a952a2fdf7b26a4
5
+ SHA512:
6
+ metadata.gz: b6c011883db3dc8da0212b322be8657d6d4b7c89f3833e955d9a85a16fe3d1cc520f70f7e0b4f8703b53e043409132b600df81d0a2a479cc2cd2034d959393b4
7
+ data.tar.gz: 63d783be719a14ad5317be58abaa0250850be1d7ddb8a4de5e0e7c8227bde4b7c030651727d6f657ee9a6090e9ea59f6db3a22ae2a7a2843e3095dae130b1bc3
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in casted_hash.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Stephan Kaag
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,17 @@
1
+ [![Build Status](https://travis-ci.org/stephankaag/cached_hash.png?branch=master)](https://travis-ci.org/stephankaag/cached_hash)
2
+
3
+ # CastedHash
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'casted_hash'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install casted_hash
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |test|
5
+ test.libs << 'test'
6
+ test.pattern = 'test/**/test_*.rb'
7
+ end
8
+
9
+ task :default => :test
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'casted_hash/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "casted_hash"
8
+ spec.version = CastedHash::VERSION
9
+ spec.authors = ["Stephan Kaag"]
10
+ spec.email = ["stephan@ka.ag"]
11
+ spec.description = spec.summary = "A casted hash"
12
+
13
+ spec.files = `git ls-files`.split($/)
14
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
15
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
16
+ spec.require_paths = ["lib"]
17
+ spec.add_dependency "equalizer", ">= 0.0.7"
18
+ spec.add_dependency "activesupport"
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.3"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "minitest", "~> 5"
23
+ spec.add_development_dependency "coveralls"
24
+ end
@@ -0,0 +1,27 @@
1
+ class CastedHash
2
+ class Value
3
+ include Equalizer.new(:value)
4
+ attr_reader :value
5
+
6
+ def initialize(value, cast_proc)
7
+ @value = value
8
+ @cast_proc = cast_proc
9
+ end
10
+
11
+ def casted_value
12
+ cast!
13
+ @value
14
+ end
15
+
16
+ def casted?
17
+ !!@casted
18
+ end
19
+
20
+ def cast!
21
+ return if casted?
22
+
23
+ @value = @cast_proc.call(value)
24
+ @casted = true
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ class CastedHash
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,110 @@
1
+ require "casted_hash/version"
2
+ require "equalizer"
3
+ require "casted_hash/value"
4
+ require "active_support/hash_with_indifferent_access"
5
+
6
+ class CastedHash
7
+ include Equalizer.new(:raw, :cast_proc)
8
+ extend Forwardable
9
+ attr_reader :cast_proc
10
+
11
+ def_delegators :@hash, *[:keys, :inject, :key?, :include?]
12
+ def_delegators :casted_hash, *[:collect]
13
+
14
+ def initialize(constructor = {}, cast_proc = lambda { |x| x })
15
+ @cast_proc = cast_proc
16
+ @hash = HashWithIndifferentAccess.new(pack_hash(constructor))
17
+ end
18
+
19
+ def each
20
+ @hash.each do |key, value|
21
+ yield key, value.casted_value
22
+ end
23
+ end
24
+ alias_method :each_pair, :each
25
+
26
+ def values
27
+ @hash.values.map(&:casted_value)
28
+ end
29
+
30
+ def fetch(key, *args)
31
+ val = @hash[key]
32
+
33
+ if val.nil?
34
+ @hash.fetch(key, *args)
35
+ else
36
+ cast(key, val).value
37
+ end
38
+ end
39
+
40
+ def [](key)
41
+ val = @hash[key]
42
+ val.casted_value unless val.nil?
43
+ end
44
+
45
+ def []=(key, value)
46
+ @hash[key] = pack(value)
47
+ end
48
+ alias_method :store, :[]=
49
+
50
+ def delete(key)
51
+ @hash.delete(key)
52
+ end
53
+
54
+ def to_hash
55
+ @hash
56
+ end
57
+
58
+ def merge(other)
59
+ other = other.to_hash
60
+ self.class.new(@hash.merge(other), cast_proc)
61
+ end
62
+
63
+ def merge!(other)
64
+ other = pack_hash(other)
65
+ @hash.merge!(other)
66
+ end
67
+
68
+ def casted?(key)
69
+ val = @hash[key]
70
+ val.casted? if val
71
+ end
72
+
73
+ def inspect
74
+ "#<#{self.class.name} hash=#{@hash.inject({}){|hash, (k, v)|hash.merge(k => casted?(k) ? v.value : "<#{v.value}>")}}>"
75
+ end
76
+
77
+ def casted_hash
78
+ cast_all!
79
+
80
+ @hash.inject({}) do |hash, (key, value)|
81
+ hash.merge(key => value.value)
82
+ end
83
+ end
84
+
85
+ private
86
+
87
+ def raw
88
+ @hash
89
+ end
90
+
91
+ def cast_all!
92
+ @hash.each do |key, value|
93
+ value.cast!
94
+ end
95
+ end
96
+
97
+ def pack(value)
98
+ if value.is_a?(Value)
99
+ value
100
+ else
101
+ Value.new(value, cast_proc)
102
+ end
103
+ end
104
+
105
+ def pack_hash(hash)
106
+ hash.inject({}) do |hash, (key, value)|
107
+ hash.merge key => pack(value)
108
+ end if hash
109
+ end
110
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,24 @@
1
+ $TESTING = true
2
+ require 'coveralls'
3
+ Coveralls.wear! do
4
+ add_filter "/test/"
5
+ end
6
+
7
+ ENV['RACK_ENV'] = ENV['RAILS_ENV'] = 'test'
8
+
9
+ if ENV.has_key?("SIMPLECOV")
10
+ require 'simplecov'
11
+ SimpleCov.start do
12
+ add_filter "/test/"
13
+ end
14
+ end
15
+
16
+ begin
17
+ require 'pry'
18
+ rescue LoadError
19
+ end
20
+
21
+ require 'casted_hash'
22
+
23
+ require 'minitest/autorun'
24
+ require 'minitest/pride'
@@ -0,0 +1,159 @@
1
+ require 'helper'
2
+
3
+ class TestCastedHash < Minitest::Test
4
+ describe CastedHash do
5
+
6
+ it "should be able to define a cast method" do
7
+ hash = CastedHash.new({:foo => 1}, lambda { |x| x.to_s })
8
+
9
+ hash[:bar] = 1
10
+ assert_equal "1", hash[:bar]
11
+
12
+ hash[:bar] = 2
13
+ assert_equal "2", hash[:bar]
14
+
15
+ hash.store :bar, 3
16
+ assert_equal "3", hash[:bar]
17
+ end
18
+
19
+ it "should cast when expected" do
20
+ hash = CastedHash.new({:a => 1, :b => 2}, lambda {|x| x + 10 })
21
+ assert !hash.casted?(:a)
22
+ assert !hash.casted?(:b)
23
+
24
+ assert hash[:a]
25
+
26
+ assert hash.casted?(:a)
27
+ assert !hash.casted?(:b)
28
+ end
29
+
30
+ it "should only cast once" do
31
+ hash = CastedHash.new({:foo => 1}, lambda { |x| x + 1 })
32
+
33
+ assert_equal 2, hash[:foo]
34
+ assert_equal 2, hash[:foo]
35
+
36
+ hash[:bar] = 123
37
+ assert_equal 124, hash[:bar]
38
+ assert_equal 124, hash[:bar]
39
+ end
40
+
41
+ it "should be able to fetch all casted values" do
42
+ hash = CastedHash.new({:a => 1, :b => 10, :c => 100}, lambda { |x| x * 10 })
43
+ assert_equal [10, 100, 1000], hash.values
44
+ end
45
+
46
+ it "should inspect casted values" do
47
+ hash = CastedHash.new(nil, lambda { |x| "foobar" })
48
+
49
+ assert_equal("#<CastedHash hash={}>", hash.inspect)
50
+
51
+ hash[:bar] = "foo"
52
+ assert_equal("#<CastedHash hash={\"bar\"=>\"<foo>\"}>", hash.inspect) # not yet casted
53
+ assert_equal("foobar", hash[:bar])
54
+ assert_equal("#<CastedHash hash={\"bar\"=>\"foobar\"}>", hash.inspect)
55
+
56
+ hash = CastedHash.new({:foo => "bar"}, lambda { |x| "foobar" })
57
+ assert_equal("#<CastedHash hash={\"foo\"=>\"<bar>\"}>", hash.inspect) # not yet casted
58
+ assert_equal("foobar", hash[:foo])
59
+ assert_equal("#<CastedHash hash={\"foo\"=>\"foobar\"}>", hash.inspect)
60
+ end
61
+
62
+ it "should loop through casted values" do
63
+ hash = CastedHash.new({:a => 1, :b => 2}, lambda { |x| "processed: #{x}" })
64
+ map = []
65
+
66
+ hash.each do |key, value|
67
+ map << value
68
+ end
69
+
70
+ assert_equal ["processed: 1", "processed: 2"], map
71
+ end
72
+
73
+ it "should delete values" do
74
+ hash = CastedHash.new({:a => 1, :b => 2}, lambda {|x| x + 10 })
75
+ hash.delete(:b)
76
+ assert_equal({"a" => 11}, hash.casted_hash)
77
+ end
78
+
79
+ it "should merge values" do
80
+ hash = CastedHash.new({:a => 1, :b => 2}, lambda {|x| x + 10 })
81
+ assert_equal 11, hash[:a]
82
+ assert_equal 12, hash[:b]
83
+
84
+ hash = hash.merge({:a => 3})
85
+
86
+ assert_equal 13, hash[:a]
87
+ assert_equal 12, hash[:b]
88
+ end
89
+
90
+ it "should merge! values" do
91
+ hash = CastedHash.new({:a => 1, :b => 2}, lambda {|x| x + 10 })
92
+ assert_equal 11, hash[:a]
93
+ assert_equal 12, hash[:b]
94
+
95
+ hash.merge!({:a => 3})
96
+
97
+ assert_equal 13, hash[:a]
98
+ assert_equal 12, hash[:b]
99
+ end
100
+
101
+ it "should not cast all values when merging hashes" do
102
+ hash = CastedHash.new({:a => 1, :b => 2}, lambda {|x| x + 10 })
103
+ hash = hash.merge :c => 3
104
+
105
+ assert !hash.casted?(:a)
106
+ assert !hash.casted?(:b)
107
+ assert !hash.casted?(:c)
108
+
109
+ hash.merge! :c => 3
110
+
111
+ assert !hash.casted?(:a)
112
+ assert !hash.casted?(:b)
113
+ assert !hash.casted?(:c)
114
+
115
+ other_hash = CastedHash.new({:c => 1, :d => 2}, lambda {|x| x + 10 })
116
+ hash = hash.merge other_hash
117
+
118
+ assert !hash.casted?(:a)
119
+ assert !hash.casted?(:b)
120
+ assert !hash.casted?(:c)
121
+ assert !other_hash.casted?(:c)
122
+ assert !other_hash.casted?(:d)
123
+
124
+ other_hash = CastedHash.new({:c => 1, :d => 2}, lambda {|x| x + 10 })
125
+ hash.merge! other_hash
126
+
127
+ assert !hash.casted?(:a)
128
+ assert !hash.casted?(:b)
129
+ assert !hash.casted?(:c)
130
+ assert !other_hash.casted?(:c)
131
+ assert !other_hash.casted?(:d)
132
+ end
133
+
134
+ it "should define a hash method" do
135
+ l = lambda {|x| x + 10 }
136
+ hash1 = CastedHash.new({:a => 1, :b => 2}, l)
137
+ hash2 = CastedHash.new({:a => 1, :b => 2}, l)
138
+ hash3 = CastedHash.new({:a => 1, :b => 2}, lambda {|x| x + 11 })
139
+
140
+ assert_equal hash1.hash, hash2.hash
141
+ assert hash1.hash != hash3.hash
142
+ end
143
+
144
+ it "should not add all requested values" do
145
+ hash = CastedHash.new({:a => 1, :b => 2}, lambda {|x|x})
146
+ assert_nil hash[:undefined_key]
147
+
148
+ assert_equal ["a", "b"], hash.keys
149
+
150
+ assert_raises(KeyError) do
151
+ hash.fetch('another_undefined_key')
152
+ end
153
+
154
+ assert_equal '123', hash.fetch('another_undefined_key', '123')
155
+ assert_equal ["a", "b"], hash.keys
156
+ end
157
+
158
+ end
159
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: casted_hash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Stephan Kaag
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: equalizer
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.7
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.7
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '5'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '5'
83
+ - !ruby/object:Gem::Dependency
84
+ name: coveralls
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: A casted hash
98
+ email:
99
+ - stephan@ka.ag
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - casted_hash.gemspec
110
+ - lib/casted_hash.rb
111
+ - lib/casted_hash/value.rb
112
+ - lib/casted_hash/version.rb
113
+ - test/helper.rb
114
+ - test/test_casted_hash.rb
115
+ homepage:
116
+ licenses: []
117
+ metadata: {}
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - '>='
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 2.0.3
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: A casted hash
138
+ test_files:
139
+ - test/helper.rb
140
+ - test/test_casted_hash.rb