functional_hash 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9ac3aea4adecabe1573026105b6c4c9eba8cc5af
4
+ data.tar.gz: 0c59f6a42ff424048494f496d45228098a676460
5
+ SHA512:
6
+ metadata.gz: 7852189e563fd171d37601138a16a33a84292d4862abefb27b18902fa3e37f436d8fd5a4ad951d9004efb8dcd086bb586a66d1af1a7f6c73be03b43b598efb24
7
+ data.tar.gz: fadaa41f78e0489fe73a331100af3e98a8eb450d9f6824845a216cb3e3a42f5fbe1cdfc7f76263659f05e9bdad755159b965b05f7df89ed0b4167932b5a21dad
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.14.4
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in functional_hash.gemspec
4
+ gemspec
@@ -0,0 +1,93 @@
1
+ # FunctionalHash
2
+
3
+ `FunctionalHash` is a hash that allows you to set values as Procs that are called automatically when fetched with the square bracket syntax `[]`.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'functional_hash'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install functional_hash
20
+
21
+ ## Usage
22
+
23
+ **Example: Set a hash value to be a function of others.**
24
+ ```ruby
25
+ stats = FunctionalHash.new
26
+ stats[:batting_average] = -> (s) { (1.0 * s[:hits] / s[:at_bats]).round(3) }
27
+ stats[:at_bats] = 25
28
+ stats[:hits] = 10
29
+ stats[:batting_average]
30
+ # => 0.4
31
+ stats[:at_bats] += 1
32
+ stats[:batting_average]
33
+ # => 0.385
34
+ stats[:at_bats] += 1
35
+ stats[:hits] += 1
36
+ stats[:batting_average]
37
+ => 0.407
38
+ ```
39
+
40
+
41
+ **Example: Set one hash value to be a function that takes additional parameters.**
42
+ ```ruby
43
+ stats = FunctionalHash.new
44
+ stats[:batting_average] = -> (s, decimal_places) { (1.0 * s[:hits] / s[:at_bats]).round(decimal_places) }
45
+ stats[:at_bats] = 3
46
+ stats[:hits] = 1
47
+ stats[:batting_average, 6]
48
+ # => 0.333333
49
+ ```
50
+
51
+ **Example: Set one hash value to be a function that takes parameters as options.**
52
+ ```ruby
53
+ stats = FunctionalHash.new
54
+ stats[:batting_average] = -> (s, decimal_places:) { (1.0 * s[:hits] / s[:at_bats]).round(decimal_places) }
55
+ stats[:at_bats] = 3
56
+ stats[:hits] = 1
57
+ stats[:batting_average, decimal_places: 6]
58
+ # => 0.333333
59
+ ```
60
+
61
+ **Example: Use functional hash values in other functional hash values.**
62
+ ```ruby
63
+ stats = FunctionalHash.new
64
+ stats[:total_bases] = -> (s) { s[:singles] + 2 * s[:doubles] + 3 * s[:triples] + 4 * s[:homeruns] }
65
+ stats[:slugging_avg] = -> (s) { 1.0 * s[:total_bases] / s[:at_bats] }
66
+
67
+ stats[:singles] = 13
68
+ stats[:doubles] = 10
69
+ stats[:triples] = 2
70
+ stats[:homeruns] = 4
71
+ stats[:at_bats] = 90
72
+
73
+ stats[:slugging_avg].round(3)
74
+ # => 0.611
75
+
76
+ stats[:singles] += 1
77
+ stats[:at_bats] += 1
78
+
79
+ stats[:slugging_avg].round(3)
80
+ # => 0.615
81
+ ```
82
+
83
+
84
+ ## Development
85
+
86
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
87
+
88
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
89
+
90
+ ## Contributing
91
+
92
+ Bug reports and pull requests are welcome on GitHub at https://github.com/afred/functional_hash.
93
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "functional_hash"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'functional_hash/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "functional_hash"
8
+ spec.version = FunctionalHash::VERSION
9
+ spec.authors = ["Andrew Myers"]
10
+ spec.email = ["afredmyers@gmail.com"]
11
+
12
+ spec.summary = %q{Easily set hash values to be functions of other values in the same hash.}
13
+ spec.homepage = "https://github.com/afred/functional_hash"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.14"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ end
@@ -0,0 +1,15 @@
1
+ class FunctionalHash < Hash
2
+ def [](*args)
3
+ key = args.shift
4
+ if fetch(key).is_a? Proc
5
+ if fetch(key).arity == 0
6
+ fetch(key).call
7
+ else
8
+ args.unshift(self)
9
+ fetch(key).call(*args)
10
+ end
11
+ else
12
+ super(key)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ class FunctionalHash < Hash
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: functional_hash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Myers
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-10-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - afredmyers@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - functional_hash.gemspec
71
+ - lib/functional_hash.rb
72
+ - lib/functional_hash/version.rb
73
+ homepage: https://github.com/afred/functional_hash
74
+ licenses: []
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.5.1
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Easily set hash values to be functions of other values in the same hash.
96
+ test_files: []