hamstar 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6834ded7d8ce8c335b09b7c063611dc7a08ee9e2
4
+ data.tar.gz: 23e93acaa517ad80aecc7defac3c92abacc2e58a
5
+ SHA512:
6
+ metadata.gz: 20440b52f938840a7e474e399e2af48f373af8f570253308b957ba9cc29afc5e786f122b81ab08440f984228a7c24c62eed4f33f2e860cdb5036036568e2dead
7
+ data.tar.gz: ab66aaedd99211d99418038bfd91d7d244ac1c7d337f0e37f35c29a1f35f49ccf649f02b0d9eb3ad65e29457bcbe75afcdc4f4cc3204aa510267fba959e588b1
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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hamstar.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Bill Burcham
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,59 @@
1
+ # Hamstar
2
+
3
+
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'hamstar'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install hamstar
18
+
19
+ ## Usage
20
+
21
+ `Hamstar.update_having` is a `module_function` that works just like [Hamster update_in()](https://github.com/hamstergem/hamster#transformations) but with two additional ways to select container elements:
22
+
23
+ 1. the associative selector denoted by an array containing a key and a value e.g. `[:name,'Chris']`
24
+ 2. the Kleen star operator denoted by '*'
25
+
26
+ With plain old `update_in()` you can:
27
+
28
+ ```ruby
29
+ require 'hamster'
30
+ x = [{name:'Chris', hobbies:['clarinet']},{name:'Pat',hobbies:['bird watching','rugby']}]
31
+ Hamster.from(x).update_in(1,:name){|name| 'Patsy'}
32
+ => Hamster::Vector[Hamster::Hash[:hobbies => Hamster::Vector["clarinet"], :name => "Chris"], Hamster::Hash[:hobbies => Hamster::Vector["bird watching", "rugby"], :name => "Patsy"]]
33
+ ```
34
+
35
+ With Hamstar you can write code that is a little more flexibly:
36
+
37
+ ```ruby
38
+ require 'hamstar'
39
+ Hamstar.update_having( Hamster.from(x), 1,:name){|name| 'Patsy'}
40
+ => (same result as before)
41
+ ```
42
+
43
+ And you can go further, and replace the vector offset `1` with the Kleene star `'*'` to operate on all elements of the vector:
44
+
45
+ ```ruby
46
+ Hamstar.update_having( Hamster.from(x), '*',:name){|name| 'Patsy'}
47
+ => (same result as before)
48
+ ```
49
+
50
+ See [`hamstar_spec.rb`](file://spec/hamstar_spec.rb) for more examples.
51
+
52
+
53
+ ## Contributing
54
+
55
+ 1. Fork it ( http://github.com/Bill/hamstar/fork )
56
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
57
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
58
+ 4. Push to the branch (`git push origin my-new-feature`)
59
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/hamstar.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hamstar/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hamstar"
8
+ spec.version = Hamstar::VERSION
9
+ spec.authors = ["Bill Burcham"]
10
+ spec.email = ["bill.burcham@gmail.com"]
11
+ spec.summary = %q{Hamstar Transforms Immutable Ruby Collections Better}
12
+ spec.description = %q{Hamstar.update_having() lets you transform deep amalgamations of Hamster (immutable) Hash and Vector with all the features of update_in() plus associative selection [key,val] and Kleene star '*'}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency 'hamster', '~> 2'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.5"
24
+ spec.add_development_dependency "byebug"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ end
data/lib/hamstar.rb ADDED
@@ -0,0 +1,51 @@
1
+ require 'hamstar/version'
2
+ require 'hamstar/initializers'
3
+ require 'hamster'
4
+
5
+ module Hamstar
6
+
7
+ KLEENE_STAR = '*'
8
+
9
+ module_function
10
+
11
+ def update_having(c, *key_path, &block)
12
+ if key_path.empty?
13
+ raise ArgumentError, "must have at least one key in path"
14
+ end
15
+ key = key_path[0]
16
+ case key
17
+ when KLEENE_STAR; kleene_star c, *key_path, &block
18
+ when Array, Hamster::Vector; association c, *key_path, &block
19
+ else
20
+ if key_path.size == 1
21
+ new_value = block.call c.fetch(key,nil)
22
+ else
23
+ value = c.fetch key, Hamster::EmptyHash
24
+ new_value = update_having value, *key_path[1..-1], &block
25
+ end
26
+ c.put key, new_value
27
+ end
28
+ end
29
+
30
+ def kleene_star(c, *key_path, &block)
31
+ kp_rest = Hamster.from(key_path)[1..-1] # drop kleene star
32
+ c.keys.each do |key|
33
+ kp = kp_rest.unshift key # put key where kleene star was
34
+ c = update_having c, *kp, &block
35
+ end
36
+ c
37
+ end
38
+
39
+ def association(c, *key_path, &block)
40
+ key,value = key_path[0]
41
+ kp_rest = Hamster.from(key_path)[1..-1] # drop assoc
42
+ c.each_pair do |k,v|
43
+ if v[key] == value
44
+ kp = kp_rest.unshift k # put key where assoc was
45
+ c = update_having c, *kp, &block
46
+ end
47
+ end
48
+ c
49
+ end
50
+
51
+ end
@@ -0,0 +1 @@
1
+ require 'hamstar/initializers/hamster'
@@ -0,0 +1,18 @@
1
+ require 'hamster'
2
+
3
+ module Hamster
4
+ class Vector
5
+ # Vectors act a lot like associations. Why not give 'em a keys method?
6
+ def keys
7
+ (0..(size-1))
8
+ end
9
+ def values
10
+ self
11
+ end
12
+ def each_pair
13
+ each_with_index do |v,i|
14
+ yield i,v
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module Hamstar
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ require 'hamstar'
4
+ require 'hamster'
5
+
6
+ RSpec.describe Hamstar do
7
+ examples_update_in_regression = [
8
+ [ {a:1}, [:b], ->(v){2}, {a:1,b: 2}, 'adds values for keys not already in Hash'],
9
+ [ [1], [1], ->(v){2}, [1,2], 'adds values for offsets not already in Vector'],
10
+ [ {a:1,b:[1,2]},[:b,1], ->(v){v+1}, {a:1,b:[1,3]}, 'traverses Hash of Vector'],
11
+ [ [1,{b:2}], [1,:b], ->(v){v+1}, [1,{b:3}], 'traverses Vector of Hash'],
12
+ ]
13
+ examples_kleene_star = [
14
+ [ {a:1,b:2}, ['*'], ->(v){v+1}, {a:2,b:3}, 'top level Hash'],
15
+ [ [1,2], ['*'], ->(v){v+1}, [2,3], 'top level Vector'],
16
+ [ {a:[1],b:[2]},['*', 0], ->(v){v+1}, {a:[2],b:[3]}, 'Vector underneath Hash'],
17
+ [ [{a:1},{a:2}],['*', :a], ->(v){v+1}, [{a:2},{a:3}], 'Hash underneath Vector']
18
+ ]
19
+
20
+ examples_associative = [
21
+ [ [{name:'Chris'},{name:'Pat'}], [ [:name, 'Pat'], :name ], ->(name){name<<'sy'}, [{name:'Chris'},{name:'Patsy'}], 'modify matching Hash']
22
+ ]
23
+
24
+ def self.write_examples(examples)
25
+ examples.each do |e|
26
+ it e[4] do
27
+ x = Hamster.from( e[0] )
28
+ y = Hamstar.update_having(x, *e[1], &e[2])
29
+ expect(Hamster.to_ruby y).to eq(e[3])
30
+ end
31
+ end
32
+ end
33
+
34
+ describe 'Inherited Hamster update_in functionality' do
35
+ write_examples examples_update_in_regression
36
+ end
37
+ describe 'New kleene star' do
38
+ write_examples examples_kleene_star
39
+ end
40
+ describe 'New association matching' do
41
+ write_examples examples_associative
42
+ end
43
+
44
+ end
@@ -0,0 +1,19 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
18
+
19
+ require 'byebug'
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hamstar
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Bill Burcham
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: hamster
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
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: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Hamstar.update_having() lets you transform deep amalgamations of Hamster
84
+ (immutable) Hash and Vector with all the features of update_in() plus associative
85
+ selection [key,val] and Kleene star '*'
86
+ email:
87
+ - bill.burcham@gmail.com
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".rspec"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - hamstar.gemspec
99
+ - lib/hamstar.rb
100
+ - lib/hamstar/initializers.rb
101
+ - lib/hamstar/initializers/hamster.rb
102
+ - lib/hamstar/version.rb
103
+ - spec/hamstar_spec.rb
104
+ - spec/spec_helper.rb
105
+ homepage: ''
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.2.2
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: Hamstar Transforms Immutable Ruby Collections Better
129
+ test_files:
130
+ - spec/hamstar_spec.rb
131
+ - spec/spec_helper.rb