shash 0.0.1
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.
- data/LICENSE +6 -0
- data/README +35 -0
- data/lib/shash.rb +40 -0
- data/lib/version.rb +7 -0
- data/shash.gemspec +20 -0
- data/test/test_shash.rb +33 -0
- metadata +52 -0
data/LICENSE
ADDED
data/README
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
Shash
|
2
|
+
=====
|
3
|
+
Shash provides quick way to handle configuration file, settings or other form of key/value store. It's based
|
4
|
+
on a single hash, but lets you set/read keys like attributes. Shash stands for StructHash or, for intimates,
|
5
|
+
SuperHash.
|
6
|
+
|
7
|
+
Installation
|
8
|
+
------------
|
9
|
+
gem install shash
|
10
|
+
|
11
|
+
Test
|
12
|
+
----
|
13
|
+
rspec test/test_shash.rb
|
14
|
+
|
15
|
+
Usage
|
16
|
+
-----
|
17
|
+
Here his the simpliest scenario
|
18
|
+
Settings = Shash.new({"name"=>"Shash!", "application"=>{"version"=>1}})
|
19
|
+
Settings.name #=> "Shash!"
|
20
|
+
Settings.application #=> {"version"=>1}
|
21
|
+
Settings.application.version #=> 1
|
22
|
+
|
23
|
+
From a hash, it could not be simplier
|
24
|
+
Settings = {"name"=>"Shash!"}.to_shash
|
25
|
+
Settings.name #=> "Shash!"
|
26
|
+
|
27
|
+
You can of course set values directly from a Shash object
|
28
|
+
Settings = Shash.new
|
29
|
+
Settings.respond_to?("name") #=> false
|
30
|
+
Settings.name = "Shash!"
|
31
|
+
Settings.respond_to?("name") #=> true
|
32
|
+
Settings.name #=> "Shash!"
|
33
|
+
|
34
|
+
From a YAML file, or any other key/value kind of file
|
35
|
+
Settings = YAML.load_file( "path/to/your/file.yml" ).to_shash
|
data/lib/shash.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
class Shash
|
2
|
+
attr_reader :hash
|
3
|
+
|
4
|
+
def initialize(hash={})
|
5
|
+
@hash = hash
|
6
|
+
end
|
7
|
+
|
8
|
+
def method_missing(key, *args, &block)
|
9
|
+
if key[/=$/]
|
10
|
+
@hash[key[0...-1]] = args.first
|
11
|
+
else
|
12
|
+
if value = @hash[key.to_s]
|
13
|
+
case value
|
14
|
+
when Hash
|
15
|
+
Shash.new(value)
|
16
|
+
else
|
17
|
+
value
|
18
|
+
end
|
19
|
+
else
|
20
|
+
@hash.send(key, *args, &block)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def respond_to?(key)
|
26
|
+
@hash.has_key?(key.to_s)
|
27
|
+
end
|
28
|
+
alias_method :has_key?, :respond_to?
|
29
|
+
alias_method :key?, :respond_to?
|
30
|
+
|
31
|
+
def == other
|
32
|
+
@hash == other.hash
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class Hash
|
37
|
+
def to_shash
|
38
|
+
Shash.new(self)
|
39
|
+
end
|
40
|
+
end
|
data/lib/version.rb
ADDED
data/shash.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib/', __FILE__)
|
3
|
+
$:.unshift lib unless $:.include?(lib)
|
4
|
+
|
5
|
+
require './lib/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "shash"
|
9
|
+
s.version = Shash::VERSION
|
10
|
+
s.platform = Gem::Platform::RUBY
|
11
|
+
s.authors = ["Christian Blais"]
|
12
|
+
s.email = ["christ.blais@gmail.com"]
|
13
|
+
s.homepage = "http://github.com/christianblais/shash"
|
14
|
+
s.summary = "Replace hash keys with method calls"
|
15
|
+
s.description = "Replace hash keys with method calls"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
|
19
|
+
s.require_paths = ['lib', 'test']
|
20
|
+
end
|
data/test/test_shash.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "..", "lib", "shash")
|
2
|
+
|
3
|
+
describe Hash do
|
4
|
+
it "#to_shash should returns a new Shash object" do
|
5
|
+
{"a"=>1}.to_shash.should == Shash.new({"a"=>1})
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
describe Shash do
|
10
|
+
it "should accesses hash values through method calls" do
|
11
|
+
h = {"a"=>1, "b"=>{"c"=>2}}.to_shash
|
12
|
+
h.a.should == 1
|
13
|
+
h.b.should == {"c"=>2}.to_shash
|
14
|
+
h.b.c.should == 2
|
15
|
+
end
|
16
|
+
|
17
|
+
it "#method=(value) should set a new key with corresponding value" do
|
18
|
+
h = {}.to_shash
|
19
|
+
h.y = nil
|
20
|
+
h.should == {"y"=>nil}.to_shash
|
21
|
+
h.y = {}
|
22
|
+
h.should == {"y"=>{}}.to_shash
|
23
|
+
h.y.z = 0
|
24
|
+
h.should == {"y"=>{"z"=>0}}.to_shash
|
25
|
+
end
|
26
|
+
|
27
|
+
it "#respond_to? should check for key existence correctly" do
|
28
|
+
h = {"a"=>1, "b"=>{"c"=>2}}.to_shash
|
29
|
+
h.respond_to?("a").should == true
|
30
|
+
h.has_key?("b").should == true
|
31
|
+
h.b.key?("c").should == true
|
32
|
+
end
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: shash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Christian Blais
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-08-31 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: Replace hash keys with method calls
|
15
|
+
email:
|
16
|
+
- christ.blais@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- LICENSE
|
22
|
+
- README
|
23
|
+
- lib/shash.rb
|
24
|
+
- lib/version.rb
|
25
|
+
- shash.gemspec
|
26
|
+
- test/test_shash.rb
|
27
|
+
homepage: http://github.com/christianblais/shash
|
28
|
+
licenses: []
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
- test
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
requirements: []
|
47
|
+
rubyforge_project:
|
48
|
+
rubygems_version: 1.8.8
|
49
|
+
signing_key:
|
50
|
+
specification_version: 3
|
51
|
+
summary: Replace hash keys with method calls
|
52
|
+
test_files: []
|