hashtring 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +16 -0
- data/.ruby-gemset +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +107 -0
- data/Rakefile +2 -0
- data/hashtring.gemspec +23 -0
- data/lib/core_ext/string.rb +5 -0
- data/lib/hashtring.rb +39 -0
- data/lib/hashtring/version.rb +3 -0
- data/spec/lib/core_ext/string_spec.rb +15 -0
- data/spec/lib/hashtring/constructor_spec.rb +49 -0
- data/spec/spec_helper.rb +1 -0
- metadata +108 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6f83b210c5b457222fcd868fb13f462c182a3438
|
4
|
+
data.tar.gz: a12f61eff097d023fb34b5ce07380b138f67d0c1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0635dd3747860003d4351ad9d1139ceb3e17235196267ef4737add71161c28917ef5fb249936bc9b953ab89acaf6c0b3ef306c317f9403c61ccf8eb8f7dccce9
|
7
|
+
data.tar.gz: 4edcc2faa73cfebbfeab668188a7fedd199f993672d70880ba7b52742d2f5ca9c6c3d565f52e9c0f087cf489790692a22fbbf1aa3380c94982bbc0718784800f
|
data/.gitignore
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
hashtring
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Kyle Macey
|
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,107 @@
|
|
1
|
+
# Hashtring
|
2
|
+
|
3
|
+
A Hashstring is an object that I wanted to experiment with. I was annoyed that i18n translations don't have a nice way of nesting under logical trees.
|
4
|
+
|
5
|
+
For example, given:
|
6
|
+
|
7
|
+
```yaml
|
8
|
+
# en.yml
|
9
|
+
|
10
|
+
reports:
|
11
|
+
marketing: Marketing Reports
|
12
|
+
sales: Sales Reports
|
13
|
+
```
|
14
|
+
|
15
|
+
Trying to call `I18n.t(:reports)` returns the hash of marketing and sales with their values. The solution would be to have two keys: an actual name and a namespace:
|
16
|
+
|
17
|
+
```yaml
|
18
|
+
# en.yml
|
19
|
+
|
20
|
+
reports: Reports
|
21
|
+
report_types:
|
22
|
+
marketing: Marketing Reports
|
23
|
+
sales: Sales Reports
|
24
|
+
```
|
25
|
+
|
26
|
+
I wanted to experiment with a pattern that wouldn't require sacrifice. Enter the Hashtring:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
reports = "Reports" ** {
|
30
|
+
marketing: "Marketing Reports",
|
31
|
+
sales: "Sales Reports"
|
32
|
+
}
|
33
|
+
|
34
|
+
puts reports # "Reports"
|
35
|
+
puts reports.marketing # "Marketing Reports"
|
36
|
+
```
|
37
|
+
|
38
|
+
Sure, it may not be entirely practical, and it probably doesn't solve any *real* problems, but it was something I wanted to play around with.
|
39
|
+
|
40
|
+
## Installation
|
41
|
+
|
42
|
+
Add this line to your application's Gemfile:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
gem 'hashtring'
|
46
|
+
```
|
47
|
+
|
48
|
+
And then execute:
|
49
|
+
|
50
|
+
$ bundle
|
51
|
+
|
52
|
+
Or install it yourself as:
|
53
|
+
|
54
|
+
$ gem install hashtring
|
55
|
+
|
56
|
+
## Usage
|
57
|
+
|
58
|
+
To create a Hashtring, simply call the `#**` method on a string and give it a hash.
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
"label" ** {
|
62
|
+
key1: "value1",
|
63
|
+
key2: "value2"
|
64
|
+
}
|
65
|
+
```
|
66
|
+
|
67
|
+
One-liner:
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
"label" ** key: "value"
|
71
|
+
```
|
72
|
+
|
73
|
+
You can even nest Hashtrings:
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
orders = "Orders" ** {
|
77
|
+
line_items: "Line Items",
|
78
|
+
additional_charges: "Summary" ** {
|
79
|
+
tax: "NYS Tax",
|
80
|
+
shipping: "UPS Shipping"
|
81
|
+
}
|
82
|
+
}
|
83
|
+
```
|
84
|
+
|
85
|
+
Call up values just like you would with a normal hash, but root nodes can be output as strings.
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
puts orders # orders
|
89
|
+
puts orders[:line_items] # "Line Items"
|
90
|
+
puts orders[:additional_charges] # "Summary"
|
91
|
+
puts orders[:additional_charges][:tax] # "NYS Tax"
|
92
|
+
```
|
93
|
+
|
94
|
+
For fun, I leveraged method_missing to give an i18n-like syntax
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
puts orders.line_items # "Line Items"
|
98
|
+
puts orders.additional_charges.tax # "NYS Tax"
|
99
|
+
```
|
100
|
+
|
101
|
+
## Contributing
|
102
|
+
|
103
|
+
1. Fork it ( https://github.com/KyleMacey/Hashtring/fork )
|
104
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
105
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
106
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
107
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/hashtring.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'hashtring/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "hashtring"
|
8
|
+
spec.version = Hashtring::VERSION
|
9
|
+
spec.authors = ["Kyle Macey"]
|
10
|
+
spec.email = ["shout@kylemacey.com"]
|
11
|
+
spec.summary = %q{An interesting class}
|
12
|
+
spec.homepage = "https://github.com/KyleMacey/Hashtring"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
21
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
22
|
+
spec.add_development_dependency "rspec", '~> 3.2', '>= 3.2.0'
|
23
|
+
end
|
data/lib/hashtring.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require "hashtring/version"
|
2
|
+
require "core_ext/string"
|
3
|
+
|
4
|
+
module Hashtring
|
5
|
+
class Constructor
|
6
|
+
attr_accessor :name, :hash
|
7
|
+
|
8
|
+
def initialize(name, hash={})
|
9
|
+
@name = name.to_s
|
10
|
+
@hash = hash
|
11
|
+
end
|
12
|
+
|
13
|
+
def [](*args)
|
14
|
+
hash.send(:[], *args)
|
15
|
+
end
|
16
|
+
|
17
|
+
def inspect
|
18
|
+
to_s
|
19
|
+
end
|
20
|
+
|
21
|
+
def method_missing(method_sym, *args, &block)
|
22
|
+
if stringify_keys(hash).has_key? method_sym.to_s
|
23
|
+
stringify_keys(hash).send(:[], method_sym.to_s)
|
24
|
+
else
|
25
|
+
super
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_s
|
30
|
+
name.to_s
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def stringify_keys(hash)
|
36
|
+
Hash[hash.map { |k, v| [k.to_s, v] }]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Hashtring::Constructor do
|
4
|
+
subject do
|
5
|
+
Hashtring::Constructor.new(
|
6
|
+
"Reports", {
|
7
|
+
marketing: "Marketing Reports"
|
8
|
+
}
|
9
|
+
)
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "#[]" do
|
13
|
+
it 'returns a value from the hash' do
|
14
|
+
expect(subject[:marketing]).to eq("Marketing Reports")
|
15
|
+
end
|
16
|
+
|
17
|
+
context "when the key does not exist" do
|
18
|
+
it "returns nil" do
|
19
|
+
expect(subject[:wrong]).to be_nil
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "#inspect" do
|
25
|
+
it "returns the name" do
|
26
|
+
expect(subject.inspect).to eq("Reports")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "#method_missing" do
|
31
|
+
context "when the hash key exists" do
|
32
|
+
it "returns a value from the hash" do
|
33
|
+
expect(subject.marketing).to eq("Marketing Reports")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context "when the hash key does not exist" do
|
38
|
+
it "raises a no method error" do
|
39
|
+
expect{ subject.wrong }.to raise_error(NoMethodError)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "#to_s" do
|
45
|
+
it "returns the name" do
|
46
|
+
expect(subject.to_s).to eq("Reports")
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'hashtring'
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hashtring
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kyle Macey
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-04 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.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
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.2'
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: 3.2.0
|
51
|
+
type: :development
|
52
|
+
prerelease: false
|
53
|
+
version_requirements: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - "~>"
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '3.2'
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 3.2.0
|
61
|
+
description:
|
62
|
+
email:
|
63
|
+
- shout@kylemacey.com
|
64
|
+
executables: []
|
65
|
+
extensions: []
|
66
|
+
extra_rdoc_files: []
|
67
|
+
files:
|
68
|
+
- ".gitignore"
|
69
|
+
- ".ruby-gemset"
|
70
|
+
- Gemfile
|
71
|
+
- LICENSE.txt
|
72
|
+
- README.md
|
73
|
+
- Rakefile
|
74
|
+
- hashtring.gemspec
|
75
|
+
- lib/core_ext/string.rb
|
76
|
+
- lib/hashtring.rb
|
77
|
+
- lib/hashtring/version.rb
|
78
|
+
- spec/lib/core_ext/string_spec.rb
|
79
|
+
- spec/lib/hashtring/constructor_spec.rb
|
80
|
+
- spec/spec_helper.rb
|
81
|
+
homepage: https://github.com/KyleMacey/Hashtring
|
82
|
+
licenses:
|
83
|
+
- MIT
|
84
|
+
metadata: {}
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
requirements: []
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 2.4.5
|
102
|
+
signing_key:
|
103
|
+
specification_version: 4
|
104
|
+
summary: An interesting class
|
105
|
+
test_files:
|
106
|
+
- spec/lib/core_ext/string_spec.rb
|
107
|
+
- spec/lib/hashtring/constructor_spec.rb
|
108
|
+
- spec/spec_helper.rb
|