hash-access 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2007,2008 Slava Chernobai <chernobai@gmail.com>
2
+
3
+ This is free software. You may redistribute copies of it under the terms
4
+ of the GNU General Public License.
5
+
6
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
7
+
data/README ADDED
@@ -0,0 +1,17 @@
1
+ == HashAccess 0.1.0
2
+ This library allows access to hash elements by methods.
3
+
4
+ Install:
5
+
6
+ gem ins hash-access
7
+
8
+ Usage example:
9
+
10
+ require 'hash_access'
11
+ include HashAccess
12
+
13
+ h = Hash.new
14
+ h.access_by_methods
15
+ h.one.two.three = 'qwe'
16
+ print(h.inspect, "\n") # => {"one"=>{"two"=>{"three"=>"qwe"}}}=> nil
17
+
data/Rakefile ADDED
@@ -0,0 +1,63 @@
1
+ require 'rubygems'
2
+
3
+ require 'rake/clean'
4
+ require 'rake/testtask'
5
+ require 'rake/rdoctask'
6
+ require 'rake/gempackagetask'
7
+
8
+ require 'lib/hash_access'
9
+
10
+ task :default => [ :test ]
11
+
12
+ CLEAN.include(%w[ pkg/* README doc/*])
13
+
14
+ desc 'Generate README'
15
+ task :readme do
16
+ %x[erb -T 1 README.erb > README] if File.exists?('README.erb')
17
+ end
18
+
19
+ desc 'Generate RDoc'
20
+ task :rdoc => [ :readme ]
21
+ Rake::RDocTask.new do |rd|
22
+ rd.main = 'README'
23
+ rd.title = 'HashAccess'
24
+ rd.rdoc_dir = 'doc'
25
+ rd.template = 'jamis'
26
+ rd.options = %w[ --line-numbers --inline-source ]
27
+ rd.rdoc_files.include('README', 'LICENSE', 'lib/*.rb')
28
+ end
29
+
30
+ desc 'Upload RDoc to site'
31
+ task :rdoc_pub => [:rdoc] do
32
+ sh 'rsync -ae ssh --delete doc/ slava@slava.cn:/var/www/code/htdocs/hash-access/doc/'
33
+ end
34
+
35
+ Rake::TestTask.new do |t|
36
+ t.libs << 'test'
37
+ t.test_files = FileList[ 'test/test*.rb' ]
38
+ t.verbose = true
39
+ end
40
+
41
+ spec = Gem::Specification.new do |s|
42
+ s.name = 'hash-access'
43
+ s.version = HashAccess::VERSION
44
+ s.platform = Gem::Platform::RUBY
45
+ s.has_rdoc = true
46
+ s.extra_rdoc_files = %w[ README LICENSE ]
47
+ s.rdoc_options = %w[ --main=README --line-numbers --inline-source ]
48
+ s.summary = 'HashAccess. Allows access to hash elements by methods.'
49
+ s.description = s.summary
50
+ s.author = 'Slava Chernobai'
51
+ s.email = 'chernobai@gmail.com'
52
+ s.homepage = 'http://code.slava.pp.ru/hash-access/'
53
+ s.files = %w[ README LICENSE Rakefile ] + Dir.glob("{lib,test}/*")
54
+ s.require_path = 'lib'
55
+ end
56
+
57
+ task :gem => [ :readme ]
58
+ Rake::GemPackageTask.new(spec) do |p|
59
+ p.gem_spec = spec
60
+ p.need_tar = true
61
+ p.need_zip = true
62
+ end
63
+
@@ -0,0 +1,80 @@
1
+ module HashAccess # :nodoc:
2
+ VERSION = '0.1.0'
3
+
4
+ def self.included(base)
5
+ Hash.send :include, HashAccess::InstanceMethods
6
+ end
7
+
8
+ # This module contains only one method - instance method access_by_methods,
9
+ # which is being added to class Hash after inclusion of
10
+ # HashAccess module in any context (any module or class).
11
+ # All other methods are being generated by access_by_methods.
12
+ # Generated methods becomes instance methods, not the class methods as RDoc
13
+ # thinks :)
14
+ #
15
+ # Below, the list of generated methods:
16
+ # * store, []=
17
+ # * method_missing
18
+ # * stringify_keys!
19
+ module InstanceMethods
20
+ # Generates singleton instance methods,
21
+ # which allows access to hash elements with methods.
22
+ def access_by_methods
23
+ class << self
24
+ alias_method :__hash_store, :store
25
+ alias_method :__hash_assign, :[]=
26
+
27
+ # Generated instance method. Assigns the value to key.
28
+ def store(key, value)
29
+ key = key.to_s
30
+ __hash_store(key, value)
31
+ if value.is_a? Hash
32
+ value.access_by_methods
33
+ end
34
+ end # def store
35
+
36
+ # Generated instance method. Assigns the value to key.
37
+ def []=(key, value)
38
+ self.store(key, value)
39
+ end # def []=
40
+
41
+ # Generated instance method.
42
+ # Creates new hash element with the method key.
43
+ def method_missing(method, *values)
44
+ method_name = method.to_s
45
+ super(method, *values) unless method_name =~ /^[a-z0-9]+.*$/
46
+ if method_name =~ /^.*=$/
47
+ key = method_name.chop
48
+ if values.size == 1
49
+ self[key] = values[0]
50
+ else
51
+ self[key] = values
52
+ end
53
+ else
54
+ self[method_name] = Hash.new unless self.keys.include?(method_name)
55
+ self[method_name]
56
+ end
57
+ end # def method_missing
58
+
59
+ # Generated instance method. Converts all hash keys to strings.
60
+ def stringify_keys!
61
+ keys.each do |key|
62
+ next if key.is_a? String
63
+ self[key.to_s] = self[key]
64
+ delete(key)
65
+ end
66
+ self
67
+ end # def stringify_keys!
68
+ end # class
69
+
70
+ self.stringify_keys!
71
+
72
+ self.each do |key, value|
73
+ if value.is_a? Hash
74
+ value.access_by_methods
75
+ end
76
+ end
77
+ end # def access_by_methods
78
+ end # module InstanceMethods
79
+ end # module HashAccess
80
+
@@ -0,0 +1,41 @@
1
+ require 'test/unit'
2
+ require 'hash_access'
3
+
4
+ class HashAccessTest < Test::Unit::TestCase
5
+ def setup
6
+ self.class.send :include, HashAccess
7
+ end
8
+
9
+ def test_001_hash_must_respond_to_access_by_methods
10
+ hash = {}
11
+ assert_respond_to(hash, :access_by_methods)
12
+ end
13
+
14
+ def test_002_access_by_methods_generates_methods
15
+ hash_abm = {}
16
+ hash_abm.access_by_methods
17
+ assert_respond_to(hash_abm, :stringify_keys!)
18
+ assert_respond_to(hash_abm, :__hash_store)
19
+ assert_respond_to(hash_abm, :__hash_assign)
20
+ end
21
+
22
+ def test_003_changes_existing_hash_instance
23
+ hash_abm = {}
24
+ hash_abm['first'] = {}
25
+ hash_abm['first']['one'] = :first_one
26
+ hash_abm[:second] = {}
27
+ hash_abm[:second][:one] = :second_one
28
+ hash_abm.access_by_methods
29
+ assert_equal(hash_abm.keys.sort, %w[ first second ])
30
+ assert_equal(hash_abm.first.one, :first_one)
31
+ assert_equal(hash_abm.second.one, :second_one)
32
+ end
33
+
34
+ def test_004_creates_new_element_and_parent_hashes
35
+ hash_abm = {}
36
+ hash_abm.access_by_methods
37
+ hash_abm.first.second.third.fourth = :first_second_third_fourth
38
+ assert_equal(hash_abm['first']['second']['third']['fourth'], hash_abm.first.second.third.fourth)
39
+ end
40
+ end
41
+
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hash-access
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Slava Chernobai
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-01-10 00:00:00 +03:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: HashAccess. Allows access to hash elements by methods.
17
+ email: chernobai@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ - LICENSE
25
+ files:
26
+ - README
27
+ - LICENSE
28
+ - Rakefile
29
+ - lib/hash_access.rb
30
+ - test/test_hash_access.rb
31
+ has_rdoc: true
32
+ homepage: http://code.slava.pp.ru/hash-access/
33
+ post_install_message:
34
+ rdoc_options:
35
+ - --main=README
36
+ - --line-numbers
37
+ - --inline-source
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project:
55
+ rubygems_version: 1.0.1
56
+ signing_key:
57
+ specification_version: 2
58
+ summary: HashAccess. Allows access to hash elements by methods.
59
+ test_files: []
60
+