chickpea 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +59 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/chickpea.gemspec +29 -0
- data/lib/chickpea/stash.rb +44 -0
- data/lib/chickpea/version.rb +3 -0
- data/lib/chickpea.rb +50 -0
- metadata +126 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 739146440bbc01fd856df4b4bd2b4464386b9a82883bb6a75c9571cf80fc65ed
|
4
|
+
data.tar.gz: 3c896a3f9cc9d950127607389e28d024ad1edb97e30b8211a412d6a1ec846c1d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 63b1d663180e3cc38786a6b1008999c952f5fe2f7432d1329c58ce4e2885ecdf7d92784af7f4f7087945f2b48d71ddd68a3c5d19ebd33aa21c377d70ae1a51e2
|
7
|
+
data.tar.gz: 2bbdb71c207995dcf41320f0b3f0361fa5c25c2998047cba29784133165fbc68320bc910ae9c1536bb8db920b1772212516976079bb5522f881d7124eefff6ce
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Stone Tickle
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# Chickpea
|
2
|
+
|
3
|
+
A simple ruby stash. It is basically a nested struct with a few extra
|
4
|
+
features.
|
5
|
+
|
6
|
+
## Features
|
7
|
+
|
8
|
+
+ Immutable structure - the structure you define can't be mutated. Only the
|
9
|
+
value of nodes can.
|
10
|
+
+ Type checking - when you set a node, its type (class) is checked against the
|
11
|
+
old node and an exception is raised if there is a mismatch
|
12
|
+
+ Fast - there is no underlying hash or struct, so if you don't need to mutate
|
13
|
+
data often chickpea is pretty fast (about 2.5 x slower than Hash)
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
gem 'chickpea'
|
21
|
+
```
|
22
|
+
|
23
|
+
And then execute:
|
24
|
+
|
25
|
+
$ bundle
|
26
|
+
|
27
|
+
Or install it yourself as:
|
28
|
+
|
29
|
+
$ gem install chickpea
|
30
|
+
|
31
|
+
## Usage
|
32
|
+
```ruby
|
33
|
+
stash = Chickpea.new(
|
34
|
+
a: {
|
35
|
+
b: {
|
36
|
+
c: {
|
37
|
+
d: 1
|
38
|
+
}
|
39
|
+
},
|
40
|
+
bb: false
|
41
|
+
}
|
42
|
+
)
|
43
|
+
|
44
|
+
stash.a.b.c.d #=> 1
|
45
|
+
stash.a.bb? #=> false
|
46
|
+
stash.a.b.c.d = 4 #=> 4
|
47
|
+
stash.a.b.c.d = false #=> TypeError
|
48
|
+
stash.a.b.c = 1 #=> NoMethodError
|
49
|
+
```
|
50
|
+
|
51
|
+
## Development
|
52
|
+
|
53
|
+
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.
|
54
|
+
|
55
|
+
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).
|
56
|
+
|
57
|
+
## License
|
58
|
+
|
59
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'chickpea'
|
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__)
|
data/bin/setup
ADDED
data/chickpea.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
lib = File.expand_path('lib', __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'chickpea/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'chickpea'
|
7
|
+
spec.version = Chickpea::VERSION
|
8
|
+
spec.authors = ['Stone Tickle']
|
9
|
+
spec.email = ['lattis@mochiro.moe']
|
10
|
+
|
11
|
+
spec.summary = 'A simple stash'
|
12
|
+
spec.license = 'MIT'
|
13
|
+
|
14
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
15
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
16
|
+
f.match(%r{^(test|spec|features)/})
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
spec.bindir = 'exe'
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ['lib']
|
23
|
+
|
24
|
+
spec.add_development_dependency 'benchmark-ips'
|
25
|
+
spec.add_development_dependency 'bundler', '~> 2.0'
|
26
|
+
spec.add_development_dependency 'rake', '~> 12.0'
|
27
|
+
spec.add_development_dependency 'rspec', '~> 3.8'
|
28
|
+
spec.add_development_dependency 'simplecov'
|
29
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Chickpea
|
2
|
+
class Stash
|
3
|
+
class<<self
|
4
|
+
def make_stash(hash)
|
5
|
+
klass = Class.new
|
6
|
+
|
7
|
+
hash.map do |key, val|
|
8
|
+
obj =
|
9
|
+
if val.is_a?(Hash)
|
10
|
+
make_stash(val)
|
11
|
+
else
|
12
|
+
val.tap do |v|
|
13
|
+
case v
|
14
|
+
when true, false
|
15
|
+
klass.define_method(:"#{key}?") { obj }
|
16
|
+
klass.define_method(:"#{key}=") do |nv|
|
17
|
+
unless nv == true || nv == false
|
18
|
+
raise(TypeError, "expected #{nv.inspect} to be a Boolean")
|
19
|
+
end
|
20
|
+
|
21
|
+
define_singleton_method(key) { nv }
|
22
|
+
end
|
23
|
+
else
|
24
|
+
klass.define_method(:"#{key}=") do |nv|
|
25
|
+
unless nv.is_a?(obj.class)
|
26
|
+
raise(TypeError, "expected #{nv.inspect} to be a #{v.class}")
|
27
|
+
end
|
28
|
+
|
29
|
+
define_singleton_method(key) { nv }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
klass.define_method(key) { obj }
|
36
|
+
end
|
37
|
+
|
38
|
+
klass.new
|
39
|
+
end
|
40
|
+
|
41
|
+
alias new make_stash
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/lib/chickpea.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'chickpea/version'
|
2
|
+
|
3
|
+
module Chickpea
|
4
|
+
class TypeError < ::TypeError
|
5
|
+
def initialize(got, expected)
|
6
|
+
super("expected #{got.inspect} to be a #{expected}")
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class<<self
|
11
|
+
def make_stash(hash)
|
12
|
+
klass = Class.new
|
13
|
+
|
14
|
+
hash.map do |key, val|
|
15
|
+
obj =
|
16
|
+
if val.is_a?(Hash)
|
17
|
+
make_stash(val)
|
18
|
+
else
|
19
|
+
val.tap do |v|
|
20
|
+
case v
|
21
|
+
when true, false
|
22
|
+
klass.define_method(:"#{key}?") { obj }
|
23
|
+
klass.define_method(:"#{key}=") do |nv|
|
24
|
+
unless nv == true || nv == false
|
25
|
+
raise(TypeError.new(nv, :Boolean))
|
26
|
+
end
|
27
|
+
|
28
|
+
define_singleton_method(key) { nv }
|
29
|
+
end
|
30
|
+
else
|
31
|
+
klass.define_method(:"#{key}=") do |nv|
|
32
|
+
unless nv.is_a?(obj.class)
|
33
|
+
raise(TypeError.new(nv, obj.class.name))
|
34
|
+
end
|
35
|
+
|
36
|
+
define_singleton_method(key) { nv }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
klass.define_method(key) { obj }
|
43
|
+
end
|
44
|
+
|
45
|
+
klass.new
|
46
|
+
end
|
47
|
+
|
48
|
+
alias new make_stash
|
49
|
+
end
|
50
|
+
end
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: chickpea
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Stone Tickle
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-04-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: benchmark-ips
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '12.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '12.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.8'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.8'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: simplecov
|
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:
|
84
|
+
email:
|
85
|
+
- lattis@mochiro.moe
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- bin/console
|
98
|
+
- bin/setup
|
99
|
+
- chickpea.gemspec
|
100
|
+
- lib/chickpea.rb
|
101
|
+
- lib/chickpea/stash.rb
|
102
|
+
- lib/chickpea/version.rb
|
103
|
+
homepage:
|
104
|
+
licenses:
|
105
|
+
- MIT
|
106
|
+
metadata: {}
|
107
|
+
post_install_message:
|
108
|
+
rdoc_options: []
|
109
|
+
require_paths:
|
110
|
+
- lib
|
111
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
requirements: []
|
122
|
+
rubygems_version: 3.0.3
|
123
|
+
signing_key:
|
124
|
+
specification_version: 4
|
125
|
+
summary: A simple stash
|
126
|
+
test_files: []
|