accessor_utilities 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,105 @@
1
+ # Accessor Utilities #
2
+
3
+ http://rubygems.org/gems/accessor_utilities
4
+
5
+ # Description #
6
+
7
+ Utility methods for accessor-related meta-programming.
8
+
9
+ # Summary #
10
+
11
+ Extends String and Symbol instances with variable/accessor methods, provides swizzling helpers, provides accessor-related math for transforming :accessor, :reader, :writer.
12
+
13
+ ## AccessorUtilities::AccessorMath ##
14
+
15
+ Given attribute accessor types :accessor, :reader, :writer:
16
+
17
+ ### :status_plus_other_status ###
18
+
19
+ * status_plus_other_status( :accessor, :reader ) = :accessor
20
+ * status_plus_other_status( :accessor, :writer ) = :accessor
21
+ * status_plus_other_status( :reader, :writer ) = :accessor
22
+ * status_plus_other_status( :reader, :reader ) = :reader
23
+
24
+ ### :status_minus_other_status ###
25
+
26
+ * status_minus_other_status( :accessor, :reader ) = :writer
27
+ * status_minus_other_status( :accessor, :writer ) = :reader
28
+ * status_minus_other_status( :reader, :writer ) = :reader
29
+ * status_minus_other_status( :reader, :reader ) = nil
30
+
31
+ ## AccessorUtilities::Swizzle ##
32
+
33
+ Given a class with a method defined:
34
+
35
+ ```ruby
36
+ class ExampleClass
37
+
38
+ def some_method
39
+ return :first_value
40
+ end
41
+
42
+ end
43
+ ```
44
+
45
+ Swizzle methods within the present context:
46
+
47
+ ```ruby
48
+ class ExampleClass
49
+
50
+ include AccessorUtilities::Swizzle
51
+
52
+ swizzle_method( :some_method ) do
53
+ :second_value
54
+ end
55
+
56
+ end
57
+ ```
58
+
59
+ Result is method call invokes block used to swizzle method:
60
+
61
+ ```ruby
62
+ class ExampleClass
63
+
64
+ self.some_method
65
+ # => :second_value
66
+
67
+ end
68
+ ```
69
+
70
+ ## AccessorUtilities::StringInstance and AccessorUtilities::SymbolInstance ##
71
+
72
+ * is_variable_name?
73
+ * variable_name
74
+ * accessor_name
75
+ * write_accessor_name
76
+ * swizzle_name
77
+
78
+ # Install #
79
+
80
+ * sudo gem install accessor_utilities
81
+
82
+ # License #
83
+
84
+ (The MIT License)
85
+
86
+ Copyright (c) 2012 Ridiculous Power, Asher
87
+
88
+ Permission is hereby granted, free of charge, to any person obtaining
89
+ a copy of this software and associated documentation files (the
90
+ 'Software'), to deal in the Software without restriction, including
91
+ without limitation the rights to use, copy, modify, merge, publish,
92
+ distribute, sublicense, and/or sell copies of the Software, and to
93
+ permit persons to whom the Software is furnished to do so, subject to
94
+ the following conditions:
95
+
96
+ The above copyright notice and this permission notice shall be
97
+ included in all copies or substantial portions of the Software.
98
+
99
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
100
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
101
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
102
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
103
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
104
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
105
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,18 @@
1
+
2
+ # namespaces that have to be declared ahead of time for proper load order
3
+ require_relative './namespaces'
4
+
5
+ # source file requires
6
+ require_relative './requires.rb'
7
+
8
+ class ::Object
9
+ include ::AccessorUtilities::ObjectInstance
10
+ end
11
+
12
+ class ::String
13
+ include ::AccessorUtilities::StringInstance
14
+ end
15
+
16
+ class ::Symbol
17
+ include ::AccessorUtilities::SymbolInstance
18
+ end
@@ -0,0 +1,93 @@
1
+
2
+ module ::AccessorUtilities::AccessorMath
3
+
4
+ ##############################
5
+ # status_plus_other_status #
6
+ ##############################
7
+
8
+ def status_plus_other_status( existing_status, other_status )
9
+
10
+ status = nil
11
+
12
+ case existing_status
13
+
14
+ when :accessor
15
+
16
+ status = :accessor
17
+
18
+ when :reader
19
+
20
+ case other_status
21
+ when :writer, :accessor
22
+ status = :accessor
23
+ else
24
+ status = :reader
25
+ end
26
+
27
+ when :writer
28
+
29
+ case other_status
30
+ when :reader, :accessor
31
+ status = :accessor
32
+ else
33
+ status = :writer
34
+ end
35
+
36
+ when nil
37
+
38
+ return other_status
39
+
40
+ end
41
+
42
+ return status
43
+
44
+ end
45
+
46
+ ###############################
47
+ # status_minus_other_status #
48
+ ###############################
49
+
50
+ def status_minus_other_status( existing_status, other_status )
51
+
52
+ status = nil
53
+
54
+ case existing_status
55
+
56
+ when :accessor
57
+
58
+ case other_status
59
+ when :accessor
60
+ status = nil
61
+ when :reader
62
+ status = :writer
63
+ when :writer
64
+ status = :reader
65
+ else
66
+ status = :accessor
67
+ end
68
+
69
+ when :reader
70
+
71
+ case other_status
72
+ when :reader, :accessor
73
+ status = nil
74
+ else
75
+ status = :reader
76
+ end
77
+
78
+ when :writer
79
+
80
+ case other_status
81
+ when :writer, :accessor
82
+ status = nil
83
+ else
84
+ status = :writer
85
+ end
86
+
87
+ end
88
+
89
+ return status
90
+
91
+ end
92
+
93
+ end
@@ -0,0 +1,14 @@
1
+
2
+ module ::AccessorUtilities::ObjectInstance
3
+
4
+ #####################################
5
+ # instance_variables_as_accessors #
6
+ #####################################
7
+
8
+ def instance_variables_as_accessors
9
+
10
+ return instance_variables.collect { |this_var| this_var.accessor_name }
11
+
12
+ end
13
+
14
+ end
@@ -0,0 +1,49 @@
1
+
2
+ module ::AccessorUtilities::StringInstance
3
+
4
+ #######################
5
+ # is_variable_name? #
6
+ #######################
7
+
8
+ def is_variable_name?
9
+ return self[ 0 ] == '@'
10
+ end
11
+
12
+ ###################
13
+ # variable_name #
14
+ ###################
15
+
16
+ def variable_name
17
+ return ( is_variable_name? ? to_sym : ( '@' + self ).to_sym )
18
+ end
19
+
20
+ ###################
21
+ # accessor_name #
22
+ ###################
23
+
24
+ def accessor_name
25
+ return ( is_variable_name? ? self.slice( 1, length ).to_sym : to_sym )
26
+ end
27
+
28
+ #########################
29
+ # write_accessor_name #
30
+ #########################
31
+
32
+ def write_accessor_name
33
+ return ( self[ length - 1 ] == '=' ? to_sym : ( self + '=' ).to_sym )
34
+ end
35
+
36
+ ##################
37
+ # swizzle_name #
38
+ ##################
39
+
40
+ def swizzle_name( swizzled_method_prefix = 'swizzled__', append_equals_to_method_name = false )
41
+
42
+ swizzled_method_name = ( swizzled_method_prefix.to_s + to_s )
43
+ swizzled_method_name = swizzled_method_name.write_accessor_name if append_equals_to_method_name
44
+
45
+ return swizzled_method_name.to_sym
46
+
47
+ end
48
+
49
+ end
@@ -0,0 +1,44 @@
1
+
2
+ module ::AccessorUtilities::Swizzle
3
+
4
+ ####################
5
+ # swizzle_method #
6
+ ####################
7
+
8
+ def swizzle_method( method_name, swizzled_method_prefix = nil, append_equals_to_method_name = false, & method_definition )
9
+
10
+ swizzling_method_name = ( append_equals_to_method_name ? method_name.write_accessor_name : method_name )
11
+
12
+ swizzled_method_prefix = :swizzle__ unless swizzled_method_prefix
13
+
14
+ # alias existing method
15
+ swizzled_method_name = method_name.swizzle_name( swizzled_method_prefix, append_equals_to_method_name )
16
+ alias_method( swizzled_method_name, swizzling_method_name )
17
+
18
+ # declare new method in place of old
19
+ define_method( ( append_equals_to_method_name ? method_name.write_accessor_name : method_name ), & method_definition )
20
+
21
+ return swizzled_method_name
22
+
23
+ end
24
+
25
+ ######################
26
+ # unswizzle_method #
27
+ ######################
28
+
29
+ def unswizzle_method( method_name, swizzled_method_prefix = nil, append_equals_to_method_name = false )
30
+
31
+ swizzling_method_name = ( append_equals_to_method_name ? method_name.write_accessor_name : method_name )
32
+
33
+ # if we have an aliased method, alias it back
34
+ swizzle_name = method_name.swizzle_name( swizzled_method_prefix, append_equals_to_method_name )
35
+ alias_method( swizzling_method_name, swizzle_name ) if method_defined?( swizzle_name )
36
+
37
+ # remove swizzled method
38
+ unswizzled_method = remove_method( swizzle_name )
39
+
40
+ return unswizzled_method
41
+
42
+ end
43
+
44
+ end
@@ -0,0 +1,44 @@
1
+
2
+ module ::AccessorUtilities::SymbolInstance
3
+
4
+ #######################
5
+ # is_variable_name? #
6
+ #######################
7
+
8
+ def is_variable_name?
9
+ return to_s.is_variable_name?
10
+ end
11
+
12
+ ###################
13
+ # variable_name #
14
+ ###################
15
+
16
+ def variable_name
17
+ return to_s.variable_name
18
+ end
19
+
20
+ ###################
21
+ # accessor_name #
22
+ ###################
23
+
24
+ def accessor_name
25
+ return to_s.accessor_name
26
+ end
27
+
28
+ #########################
29
+ # write_accessor_name #
30
+ #########################
31
+
32
+ def write_accessor_name
33
+ return to_s.write_accessor_name
34
+ end
35
+
36
+ ##################
37
+ # swizzle_name #
38
+ ##################
39
+
40
+ def swizzle_name( swizzled_method_prefix = 'swizzled__', append_equals_to_method_name = false )
41
+ return to_s.swizzle_name( swizzled_method_prefix, append_equals_to_method_name )
42
+ end
43
+
44
+ end
data/lib/namespaces.rb ADDED
@@ -0,0 +1,3 @@
1
+
2
+ module ::AccessorUtilities
3
+ end
data/lib/requires.rb ADDED
@@ -0,0 +1,6 @@
1
+
2
+ require_relative 'accessor_utilities/object_instance.rb'
3
+ require_relative 'accessor_utilities/accessor_math.rb'
4
+ require_relative 'accessor_utilities/swizzle.rb'
5
+ require_relative 'accessor_utilities/string_instance.rb'
6
+ require_relative 'accessor_utilities/symbol_instance.rb'
@@ -0,0 +1,64 @@
1
+
2
+ require_relative '../../lib/accessor_utilities.rb'
3
+
4
+ describe AccessorUtilities::AccessorMath do
5
+
6
+ ##############################
7
+ # status_plus_other_status #
8
+ ##############################
9
+
10
+ it 'can add one status to another' do
11
+
12
+ class AccessorUtilities::AccessorMath::Mock
13
+
14
+ extend AccessorUtilities::AccessorMath
15
+
16
+ status_plus_other_status( nil, :accessor ).should == :accessor
17
+ status_plus_other_status( nil, :reader ).should == :reader
18
+ status_plus_other_status( nil, :writer ).should == :writer
19
+
20
+ status_plus_other_status( :accessor, :accessor ).should == :accessor
21
+ status_plus_other_status( :accessor, :reader ).should == :accessor
22
+ status_plus_other_status( :accessor, :writer ).should == :accessor
23
+
24
+ status_plus_other_status( :reader, :accessor ).should == :accessor
25
+ status_plus_other_status( :reader, :reader ).should == :reader
26
+ status_plus_other_status( :reader, :writer ).should == :accessor
27
+
28
+ status_plus_other_status( :writer, :accessor ).should == :accessor
29
+ status_plus_other_status( :writer, :reader ).should == :accessor
30
+ status_plus_other_status( :writer, :writer ).should == :writer
31
+
32
+ end
33
+
34
+ end
35
+
36
+ ###############################
37
+ # status_minus_other_status #
38
+ ###############################
39
+
40
+ it 'can subtract one status from another' do
41
+
42
+ class AccessorUtilities::AccessorMath::Mock
43
+
44
+ status_minus_other_status( nil, :accessor ).should == nil
45
+ status_minus_other_status( nil, :reader ).should == nil
46
+ status_minus_other_status( nil, :writer ).should == nil
47
+
48
+ status_minus_other_status( :accessor, :accessor ).should == nil
49
+ status_minus_other_status( :accessor, :reader ).should == :writer
50
+ status_minus_other_status( :accessor, :writer ).should == :reader
51
+
52
+ status_minus_other_status( :reader, :accessor ).should == nil
53
+ status_minus_other_status( :reader, :reader ).should == nil
54
+ status_minus_other_status( :reader, :writer ).should == :reader
55
+
56
+ status_minus_other_status( :writer, :accessor ).should == nil
57
+ status_minus_other_status( :writer, :reader ).should == :writer
58
+ status_minus_other_status( :writer, :writer ).should == nil
59
+
60
+ end
61
+
62
+ end
63
+
64
+ end
@@ -0,0 +1,19 @@
1
+
2
+ require_relative '../../lib/accessor_utilities.rb'
3
+
4
+ describe AccessorUtilities::ObjectInstance do
5
+
6
+ #####################################
7
+ # instance_variables_as_accessors #
8
+ #####################################
9
+
10
+ it 'can return a list of instance variables translated into corresponding accessor names' do
11
+ object_instance = Object.new
12
+ object_instance.instance_eval do
13
+ @some_variable = :value
14
+ @other_variable = :value
15
+ end
16
+ object_instance.instance_variables_as_accessors.should == [ :some_variable, :other_variable ]
17
+ end
18
+
19
+ end
@@ -0,0 +1,51 @@
1
+
2
+ require_relative '../../lib/accessor_utilities.rb'
3
+
4
+ describe AccessorUtilities::StringInstance do
5
+
6
+ #######################
7
+ # is_variable_name? #
8
+ #######################
9
+
10
+ it 'can report if a string is a variable name' do
11
+ '@variable'.is_variable_name?.should == true
12
+ 'not_variable'.is_variable_name?.should == false
13
+ end
14
+
15
+ ###################
16
+ # variable_name #
17
+ ###################
18
+
19
+ it 'can return variable name for variable or accessor name' do
20
+ '@variable'.variable_name.should == :@variable
21
+ 'not_variable'.variable_name.should == :@not_variable
22
+ end
23
+
24
+ ###################
25
+ # accessor_name #
26
+ ###################
27
+
28
+ it 'can return accessor name for variable or accessor name' do
29
+ '@variable'.accessor_name.should == :variable
30
+ 'not_variable'.accessor_name.should == :not_variable
31
+ end
32
+
33
+ #########################
34
+ # write_accessor_name #
35
+ #########################
36
+
37
+ it 'can return write accessor name for accessor name' do
38
+ 'accessor_name'.write_accessor_name.should == :accessor_name=
39
+ end
40
+
41
+ ##################
42
+ # swizzle_name #
43
+ ##################
44
+
45
+ it 'can return a swizzle name given a prefix for an accessor name' do
46
+ 'accessor_name'.swizzle_name.should == :swizzled__accessor_name
47
+ 'accessor_name'.swizzle_name( :swizzle_prefix__, false ).should == :swizzle_prefix__accessor_name
48
+ 'accessor_name'.swizzle_name( :swizzle_prefix__, true ).should == :swizzle_prefix__accessor_name=
49
+ end
50
+
51
+ end
@@ -0,0 +1,50 @@
1
+
2
+ require_relative '../../lib/accessor_utilities.rb'
3
+
4
+ describe AccessorUtilities::Swizzle do
5
+
6
+ ######################
7
+ # swizzle_method #
8
+ # unswizzle_method #
9
+ ######################
10
+
11
+ it 'can swizzle and unswizzle a method with a new method' do
12
+ class AccessorUtilities::Swizzle::Mock
13
+ def method_to_swizzle
14
+ return @return_value ||= :first_value
15
+ end
16
+ def method_to_swizzle=( value )
17
+ @return_value = value
18
+ end
19
+ end
20
+ object_instance = AccessorUtilities::Swizzle::Mock.new
21
+ object_instance.method_to_swizzle.should == :first_value
22
+ object_instance.method_to_swizzle = :value
23
+ object_instance.method_to_swizzle.should == :value
24
+ class AccessorUtilities::Swizzle::Mock
25
+ extend AccessorUtilities::Swizzle
26
+ swizzle_method( :method_to_swizzle, :swizzle_prefix__, false ) do
27
+ :second_value
28
+ end
29
+ swizzle_method( :method_to_swizzle, :swizzle_prefix__, true ) do |value|
30
+ :second_value=
31
+ end
32
+ end
33
+ object_instance.method_to_swizzle.should == :second_value
34
+ object_instance.method_to_swizzle = :value
35
+ object_instance.method_to_swizzle.should == :second_value
36
+ object_instance.swizzle_prefix__method_to_swizzle.should == :value
37
+ object_instance.swizzle_prefix__method_to_swizzle.should == :value
38
+ object_instance.method_to_swizzle.should == :second_value
39
+ class AccessorUtilities::Swizzle::Mock
40
+ unswizzle_method( :method_to_swizzle, :swizzle_prefix__, false )
41
+ unswizzle_method( :method_to_swizzle, :swizzle_prefix__, true )
42
+ end
43
+ object_instance.method_to_swizzle.should == :value
44
+ object_instance.method_to_swizzle = :first_value
45
+ object_instance.method_to_swizzle.should == :first_value
46
+ AccessorUtilities::Swizzle::Mock.method_defined?( :swizzle_prefix__method_to_swizzle ).should == false
47
+ AccessorUtilities::Swizzle::Mock.method_defined?( :swizzle_prefix__method_to_swizzle= ).should == false
48
+ end
49
+
50
+ end
@@ -0,0 +1,51 @@
1
+
2
+ require_relative '../../lib/accessor_utilities.rb'
3
+
4
+ describe AccessorUtilities::SymbolInstance do
5
+
6
+ #######################
7
+ # is_variable_name? #
8
+ #######################
9
+
10
+ it 'can report if a string is a variable name' do
11
+ :@variable.is_variable_name?.should == true
12
+ :not_variable.is_variable_name?.should == false
13
+ end
14
+
15
+ ###################
16
+ # variable_name #
17
+ ###################
18
+
19
+ it 'can return variable name for variable or accessor name' do
20
+ :@variable.variable_name.should == :@variable
21
+ :not_variable.variable_name.should == :@not_variable
22
+ end
23
+
24
+ ###################
25
+ # accessor_name #
26
+ ###################
27
+
28
+ it 'can return accessor name for variable or accessor name' do
29
+ :@variable.accessor_name.should == :variable
30
+ :not_variable.accessor_name.should == :not_variable
31
+ end
32
+
33
+ #########################
34
+ # write_accessor_name #
35
+ #########################
36
+
37
+ it 'can return write accessor name for accessor name' do
38
+ :accessor_name.write_accessor_name.should == :accessor_name=
39
+ end
40
+
41
+ ##################
42
+ # swizzle_name #
43
+ ##################
44
+
45
+ it 'can return a swizzle name given a prefix for an accessor name' do
46
+ :accessor_name.swizzle_name.should == :swizzled__accessor_name
47
+ :accessor_name.swizzle_name( :swizzle_prefix__, false ).should == :swizzle_prefix__accessor_name
48
+ :accessor_name.swizzle_name( :swizzle_prefix__, true ).should == :swizzle_prefix__accessor_name=
49
+ end
50
+
51
+ end
@@ -0,0 +1,9 @@
1
+
2
+ require_relative '../lib/accessor_utilities.rb'
3
+
4
+ describe AccessorUtilities do
5
+
6
+ # nothing unique here
7
+ # implicitly includes or extends supporting modules in correspondence to request
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: accessor_utilities
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Asher
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-06 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Extends String and Symbol instances with variable/accessor methods, provides
15
+ swizzling helpers, provides accessor-related math for transforming :accessor, :reader,
16
+ :writer.
17
+ email: asher@ridiculouspower.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - lib/accessor_utilities/accessor_math.rb
23
+ - lib/accessor_utilities/object_instance.rb
24
+ - lib/accessor_utilities/string_instance.rb
25
+ - lib/accessor_utilities/swizzle.rb
26
+ - lib/accessor_utilities/symbol_instance.rb
27
+ - lib/accessor_utilities.rb
28
+ - lib/namespaces.rb
29
+ - lib/requires.rb
30
+ - spec/accessor_utilities/accessor_math_spec.rb
31
+ - spec/accessor_utilities/object_instance_spec.rb
32
+ - spec/accessor_utilities/string_instance_spec.rb
33
+ - spec/accessor_utilities/swizzle_spec.rb
34
+ - spec/accessor_utilities/symbol_instance_spec.rb
35
+ - spec/accessor_utilities_spec.rb
36
+ - README.md
37
+ homepage: http://rubygems.org/gems/accessor_utilities
38
+ licenses: []
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project: accessor_utilities
57
+ rubygems_version: 1.8.23
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: Utility methods for accessor-related meta-programming.
61
+ test_files: []
62
+ has_rdoc: