instance_variable_hash 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.5.2"
12
+ gem "rcov", ">= 0"
13
+ gem "ZenTest", "4.5.0"
14
+ gem "autotest", "4.4.6"
15
+ gem "autotest-notification", "2.3.1"
16
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,28 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ ZenTest (4.5.0)
5
+ autotest (4.4.6)
6
+ ZenTest (>= 4.4.1)
7
+ autotest-notification (2.3.1)
8
+ autotest (~> 4.3)
9
+ git (1.2.5)
10
+ jeweler (1.5.2)
11
+ bundler (~> 1.0.0)
12
+ git (>= 1.2.5)
13
+ rake
14
+ rake (0.8.7)
15
+ rcov (0.9.9)
16
+ shoulda (2.11.3)
17
+
18
+ PLATFORMS
19
+ ruby
20
+
21
+ DEPENDENCIES
22
+ ZenTest (= 4.5.0)
23
+ autotest (= 4.4.6)
24
+ autotest-notification (= 2.3.1)
25
+ bundler (~> 1.0.0)
26
+ jeweler (~> 1.5.2)
27
+ rcov
28
+ shoulda
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Marc Rene Arns
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,55 @@
1
+ = InstanceVariableHash
2
+
3
+ == What is it for?
4
+ You might have attributes (in a plugin for example) that you want
5
+ to use as Hash. But if you want to set these attribute from the outside it
6
+ will be difficult to set parts of this hash at different places.
7
+
8
+ So it would be better to have all attributes as simple values and have a
9
+ naming convention that allows conversion into hashes if needed.
10
+
11
+ Instead of
12
+
13
+ @config = { :a => {:b => 'val_b', :c => ['c1', 'c2']} }
14
+
15
+ you would do
16
+
17
+ @a_b = 'val_b'
18
+ @a_c = ['c1', 'c2']
19
+
20
+ and with this lib you would have a shortcut that allows you to pass a hash:
21
+
22
+ instance_variables_set_with_hash({ :a => {:b => 'val_b', :c => ['c1', 'c2']} })
23
+
24
+ to get them back as a hash
25
+
26
+ instance_variables_get_as_hash('a') # => {:b => 'val_b', :c => ['c1', 'c2']} }
27
+
28
+ To use the lib:
29
+
30
+ require 'rubygems'
31
+ require 'instance_variable_hash'
32
+
33
+ # you might want to use it only for certain classes / objects
34
+ class Object
35
+ include InstanceVariableHash
36
+ end
37
+
38
+ That's all. A very simple library, tested with ruby 1.8 and 1.9.
39
+
40
+
41
+ == Contributing
42
+
43
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
44
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
45
+ * Fork the project
46
+ * Start a feature/bugfix branch
47
+ * Commit and push until you are happy with your contribution
48
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
49
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
50
+
51
+ == Copyright
52
+
53
+ Copyright (c) 2011 Marc Rene Arns. See LICENSE.txt for
54
+ further details.
55
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "instance_variable_hash"
16
+ gem.homepage = "http://github.com/metakeule/instance_variable_hash"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q|get and set @under_scored_instance_variables as recursive Hash|
19
+ gem.description = %Q{get and set @under_scored_instance_variables as recursive Hash}
20
+ gem.email = "Base64.decode64('bGludXhAbWFyY3JlbmVhcm5zLmRl\n')"
21
+ gem.authors = ["Marc Rene Arns"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "instance_variable_hash #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,42 @@
1
+ # Author:: Marc Rene Arns (Base64.decode64('bGludXhAbWFyY3JlbmVhcm5zLmRl\n'))
2
+ # Copyright:: Copyright (c) 2011 Marc Rene Arns
3
+ # License:: Distributes under the same terms as Ruby
4
+
5
+ # This module has two methods to convert between @under_scored_instance_variables
6
+ # and recursive Hashes, see README and the unit tests for examples
7
+
8
+ module InstanceVariableHash
9
+ # get all instance variables that begin with 'key' as a recursive Hash (separated by _)
10
+ def instance_variables_get_as_hash(key)
11
+ response = {} # we need a hash to have an object (id) to attach to
12
+ self.instance_variables.each do |var|
13
+ if var =~ /^@#{Regexp.escape(key)}(.*)$/ # we take the exact match and the subkeys
14
+ keys, hsh, pre = $1.split('_'), response, []
15
+ keys[0] = key # put the key itself into the result to have it in the loop
16
+ stop = keys.size
17
+ keys.each do |k|
18
+ pre << k
19
+ if pre.size == stop # last element
20
+ hsh[k] = self.instance_variable_get("@#{pre.join('_')}")
21
+ else
22
+ hsh[k] ||= {}
23
+ hsh = hsh[k]
24
+ end
25
+ end
26
+ end
27
+ end
28
+ return response[key]
29
+ end
30
+
31
+ # set instance variables by a Hash, so that they will end up with underscored names
32
+ def instance_variables_set_with_hash(hsh)
33
+ meth = proc do |pre,h|
34
+ h.each do |k,v|
35
+ keys = (pre.dup << k)
36
+ v.is_a?(Hash) ? meth.call(keys,v) : self.instance_variable_set("@#{keys.join('_')}", v)
37
+ end
38
+ end
39
+
40
+ meth.call [], hsh
41
+ end
42
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'instance_variable_hash'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,95 @@
1
+ require 'helper'
2
+
3
+ class Testobject
4
+ include InstanceVariableHash
5
+
6
+ def initialize(hsh={})
7
+ hsh.each{ |key,val| self["@#{key}"] = val }
8
+ end
9
+
10
+ # shortcuts for testing
11
+ alias :get :instance_variables_get_as_hash
12
+ alias :set :instance_variables_set_with_hash
13
+ alias :[] :instance_variable_get
14
+ alias :[]= :instance_variable_set
15
+ end
16
+
17
+ class TestInstanceVarHash < Test::Unit::TestCase
18
+
19
+ context "when getting" do
20
+ context "vars without underscores" do
21
+ setup do
22
+ @obj = Testobject.new :a => 'res_a',
23
+ :b => 'res_b'
24
+ end
25
+
26
+ should "have several" do
27
+ assert_equal 'res_a', @obj.get('a')
28
+ assert_equal 'res_b', @obj.get('b')
29
+ end
30
+ end
31
+
32
+
33
+ context "vars with one underscore" do
34
+ setup do
35
+ @obj = Testobject.new :a_b => 'res_a_b',
36
+ :a_c => 'res_a_c',
37
+ :b_c => 'res_b_c',
38
+ :b_d => 'res_b_d'
39
+ end
40
+
41
+ should "have several grouped" do
42
+ res1 = {'b' => 'res_a_b', 'c' => 'res_a_c'}
43
+ assert_equal res1, @obj.get('a')
44
+ res2 = {'c' => 'res_b_c', 'd' => 'res_b_d'}
45
+ assert_equal res2, @obj.get('b')
46
+ end
47
+ end
48
+
49
+ context "vars with n underscores" do
50
+ setup do
51
+ @obj = Testobject.new :a_c_b => 'res_a_c_b',
52
+ :a_c_d => 'res_a_c_d',
53
+ :b_c_d => 'res_b_c_d',
54
+ :b_d_e_f => 'res_b_d_e_f'
55
+ end
56
+
57
+ should "have several recursively grouped" do
58
+ res1 = {'c' => {'d' => 'res_a_c_d', 'b' => 'res_a_c_b' }}
59
+ assert_equal res1, @obj.get('a')
60
+ res2 = {'c' => {'d' => 'res_b_c_d'}, 'd' => {'e' => { 'f' => 'res_b_d_e_f' }}}
61
+ assert_equal res2, @obj.get('b')
62
+ end
63
+ end
64
+ end
65
+
66
+ context "when setting" do
67
+ setup do
68
+ @obj = Testobject.new
69
+ end
70
+
71
+ should "handle a depth of 1" do
72
+ @obj.set :a => 'res_a',
73
+ :b => 'res_b'
74
+
75
+ assert_equal 'res_a', @obj["@a"]
76
+ assert_equal 'res_b', @obj["@b"]
77
+ end
78
+
79
+ should "handle a depth of 2" do
80
+ @obj.set :a => { :b => 'res_a_b',
81
+ :c => 'res_a_c'}
82
+
83
+ assert_equal 'res_a_b', @obj["@a_b"]
84
+ assert_equal 'res_a_c', @obj["@a_c"]
85
+ end
86
+
87
+ should "handle a depth of n" do
88
+ @obj.set :a => { :c => {:d => 'res_a_c_d',
89
+ :e => { :f => { :g => 'res_a_c_e_f_g' } }}}
90
+
91
+ assert_equal 'res_a_c_e_f_g', @obj["@a_c_e_f_g"]
92
+ assert_equal 'res_a_c_d', @obj["@a_c_d"]
93
+ end
94
+ end
95
+ end
metadata ADDED
@@ -0,0 +1,187 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: instance_variable_hash
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Marc Rene Arns
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-09-06 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ type: :development
23
+ prerelease: false
24
+ name: shoulda
25
+ version_requirements: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ hash: 3
31
+ segments:
32
+ - 0
33
+ version: "0"
34
+ requirement: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ type: :development
37
+ prerelease: false
38
+ name: bundler
39
+ version_requirements: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ hash: 23
45
+ segments:
46
+ - 1
47
+ - 0
48
+ - 0
49
+ version: 1.0.0
50
+ requirement: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ type: :development
53
+ prerelease: false
54
+ name: jeweler
55
+ version_requirements: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ hash: 7
61
+ segments:
62
+ - 1
63
+ - 5
64
+ - 2
65
+ version: 1.5.2
66
+ requirement: *id003
67
+ - !ruby/object:Gem::Dependency
68
+ type: :development
69
+ prerelease: false
70
+ name: rcov
71
+ version_requirements: &id004 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ hash: 3
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ requirement: *id004
81
+ - !ruby/object:Gem::Dependency
82
+ type: :development
83
+ prerelease: false
84
+ name: ZenTest
85
+ version_requirements: &id005 !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - "="
89
+ - !ruby/object:Gem::Version
90
+ hash: 43
91
+ segments:
92
+ - 4
93
+ - 5
94
+ - 0
95
+ version: 4.5.0
96
+ requirement: *id005
97
+ - !ruby/object:Gem::Dependency
98
+ type: :development
99
+ prerelease: false
100
+ name: autotest
101
+ version_requirements: &id006 !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - "="
105
+ - !ruby/object:Gem::Version
106
+ hash: 35
107
+ segments:
108
+ - 4
109
+ - 4
110
+ - 6
111
+ version: 4.4.6
112
+ requirement: *id006
113
+ - !ruby/object:Gem::Dependency
114
+ type: :development
115
+ prerelease: false
116
+ name: autotest-notification
117
+ version_requirements: &id007 !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - "="
121
+ - !ruby/object:Gem::Version
122
+ hash: 1
123
+ segments:
124
+ - 2
125
+ - 3
126
+ - 1
127
+ version: 2.3.1
128
+ requirement: *id007
129
+ description: get and set @under_scored_instance_variables as recursive Hash
130
+ email: |-
131
+ Base64.decode64('bGludXhAbWFyY3JlbmVhcm5zLmRl
132
+ ')
133
+ executables: []
134
+
135
+ extensions: []
136
+
137
+ extra_rdoc_files:
138
+ - LICENSE.txt
139
+ - README.rdoc
140
+ files:
141
+ - .document
142
+ - Gemfile
143
+ - Gemfile.lock
144
+ - LICENSE.txt
145
+ - README.rdoc
146
+ - Rakefile
147
+ - VERSION
148
+ - lib/instance_variable_hash.rb
149
+ - test/helper.rb
150
+ - test/test_instance_variable_hash.rb
151
+ has_rdoc: true
152
+ homepage: http://github.com/metakeule/instance_variable_hash
153
+ licenses:
154
+ - MIT
155
+ post_install_message:
156
+ rdoc_options: []
157
+
158
+ require_paths:
159
+ - lib
160
+ required_ruby_version: !ruby/object:Gem::Requirement
161
+ none: false
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ hash: 3
166
+ segments:
167
+ - 0
168
+ version: "0"
169
+ required_rubygems_version: !ruby/object:Gem::Requirement
170
+ none: false
171
+ requirements:
172
+ - - ">="
173
+ - !ruby/object:Gem::Version
174
+ hash: 3
175
+ segments:
176
+ - 0
177
+ version: "0"
178
+ requirements: []
179
+
180
+ rubyforge_project:
181
+ rubygems_version: 1.3.7
182
+ signing_key:
183
+ specification_version: 3
184
+ summary: get and set @under_scored_instance_variables as recursive Hash
185
+ test_files:
186
+ - test/helper.rb
187
+ - test/test_instance_variable_hash.rb