unfickle 0.0.2 → 0.1.0
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/CHANGELOG +7 -0
- data/README +5 -5
- data/lib/unfickle/unfickle.rb +44 -5
- data/lib/unfickle/version.rb +1 -1
- data/spec/lib/unfickle/unfickle_spec.rb +41 -6
- metadata +3 -2
data/CHANGELOG
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
v0.1.0
|
2
|
+
- iterate through the constants using the .each_constant method instead of
|
3
|
+
.each
|
4
|
+
- grab all constant values using .constant_values instead of .values
|
5
|
+
- grab all constant keys using .constant_keys instead of .keys
|
6
|
+
- clear method renamed to clear_contents
|
7
|
+
- deprecation messages for removed methods
|
data/README
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
This allows for better constant management. Use it like so:
|
2
2
|
class MyClass
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
self.add_item(:SOMECONST, 1)
|
3
|
+
set_const :read, 1
|
4
|
+
set_const :write, 2
|
7
5
|
end
|
8
6
|
|
9
7
|
You can then reference it directly like so:
|
10
8
|
|
11
|
-
MyClass::
|
9
|
+
MyClass::READ
|
10
|
+
|
11
|
+
Check out this blog post for a more detailed description: http://wellthatsruby.blogspot.com/2011/06/unfickle-new-constant-way.html
|
data/lib/unfickle/unfickle.rb
CHANGED
@@ -1,17 +1,56 @@
|
|
1
1
|
module Unfickle
|
2
2
|
extend Forwardable
|
3
3
|
def set_const(key,value)
|
4
|
-
@hash ||= {}
|
5
|
-
@hash[key.upcase.to_sym]=value
|
4
|
+
@hash ||= {constants: {}}
|
5
|
+
@hash[:constants][key.upcase.to_sym]=value
|
6
6
|
end
|
7
7
|
|
8
8
|
def const_missing(key)
|
9
|
-
|
9
|
+
get_const(key)
|
10
10
|
end
|
11
11
|
|
12
|
-
|
12
|
+
def each_constant
|
13
|
+
@hash[:constants].each {|key,value| yield(key,value)}
|
14
|
+
end
|
15
|
+
|
16
|
+
def constant_values
|
17
|
+
@hash[:constants].values || []
|
18
|
+
end
|
19
|
+
|
20
|
+
def constant_keys
|
21
|
+
@hash[:constants].keys || []
|
22
|
+
end
|
23
|
+
|
24
|
+
def get_const(key)
|
25
|
+
@hash[:constants][key]
|
26
|
+
end
|
27
|
+
|
28
|
+
def clear_constants
|
29
|
+
@hash = {constants: {}}
|
30
|
+
end
|
31
|
+
|
32
|
+
def each
|
33
|
+
warn '.each method is deprecated in favor of .each_constant'
|
34
|
+
warn "please remove your call to .each from #{caller.first}"
|
35
|
+
end
|
36
|
+
|
37
|
+
def values
|
38
|
+
warn '.values method is deprecated in favor of .constant_values'
|
39
|
+
warn "please remove your call to .values from #{caller.first}"
|
40
|
+
end
|
41
|
+
|
42
|
+
def keys
|
43
|
+
warn '.keys method is deprecated in favor of .constant_keys'
|
44
|
+
warn "please remove your call to .keys from #{caller.first}"
|
45
|
+
end
|
13
46
|
|
14
47
|
def clear
|
15
|
-
|
48
|
+
warn '.clear method is deprecated in favor of .clear_constants'
|
49
|
+
warn "please remove your call to .clear from #{caller.first}"
|
50
|
+
end
|
51
|
+
|
52
|
+
def [](key)
|
53
|
+
warn '[] method is deprecated in favor of .get_const'
|
54
|
+
warn "please remove your call to [] from #{caller.first}"
|
16
55
|
end
|
17
56
|
end
|
data/lib/unfickle/version.rb
CHANGED
@@ -8,7 +8,7 @@ describe Unfickle do
|
|
8
8
|
end
|
9
9
|
|
10
10
|
before(:each) do
|
11
|
-
subject.
|
11
|
+
subject.clear_constants
|
12
12
|
subject.set_const(:BLAH, 'value')
|
13
13
|
end
|
14
14
|
|
@@ -21,15 +21,50 @@ describe Unfickle do
|
|
21
21
|
end
|
22
22
|
|
23
23
|
describe '.const_missing' do
|
24
|
-
|
25
|
-
|
24
|
+
context 'constant really is missing' do
|
25
|
+
it 'just returns nil' do
|
26
|
+
subject.const_missing(:YEEHAH).should == nil
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'we have something in the hash' do
|
31
|
+
it 'returns that something' do
|
32
|
+
subject.const_missing(:BLAH).should == 'value'
|
33
|
+
end
|
26
34
|
end
|
27
35
|
end
|
28
36
|
|
29
|
-
describe '
|
37
|
+
describe '.clear_constants' do
|
30
38
|
it 'clears the constants' do
|
31
|
-
subject.
|
32
|
-
subject.
|
39
|
+
subject.clear_constants
|
40
|
+
subject.constant_keys.should == []
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe '.each_constant' do
|
45
|
+
it 'iterates over all constants' do
|
46
|
+
subject.each_constant do |key,value|
|
47
|
+
key.should == :BLAH
|
48
|
+
value.should == 'value'
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe '.constant_values' do
|
54
|
+
it 'gets all values for the constants' do
|
55
|
+
subject.constant_values.should == ['value']
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe '.constant_keys' do
|
60
|
+
it 'gets all keys for the constants' do
|
61
|
+
subject.constant_keys.should == [:BLAH]
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe '.get_const' do
|
66
|
+
it 'grabs a single constant' do
|
67
|
+
subject.get_const(:BLAH).should == 'value'
|
33
68
|
end
|
34
69
|
end
|
35
70
|
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: unfickle
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0
|
5
|
+
version: 0.1.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Tracey Eubanks
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-06-
|
13
|
+
date: 2011-06-20 00:00:00 -06:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -47,6 +47,7 @@ extra_rdoc_files: []
|
|
47
47
|
|
48
48
|
files:
|
49
49
|
- .gitignore
|
50
|
+
- CHANGELOG
|
50
51
|
- Gemfile
|
51
52
|
- Gemfile.lock
|
52
53
|
- README
|