inch 0.2.3 → 0.3.0.rc1
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.
- checksums.yaml +4 -4
- data/.simplecov +7 -0
- data/TODOS.md +0 -5
- data/config/defaults.rb +26 -1
- data/inch.gemspec +1 -0
- data/lib/inch.rb +2 -1
- data/lib/inch/api.rb +34 -0
- data/lib/inch/api/filter.rb +17 -0
- data/lib/inch/api/get.rb +30 -0
- data/lib/inch/api/list.rb +10 -0
- data/lib/inch/api/options/base.rb +45 -0
- data/lib/inch/api/options/filter.rb +25 -0
- data/lib/inch/api/options/suggest.rb +36 -0
- data/lib/inch/api/stats.rb +7 -0
- data/lib/inch/api/suggest.rb +110 -0
- data/lib/inch/cli.rb +4 -1
- data/lib/inch/cli/command/base.rb +1 -17
- data/lib/inch/cli/command/base_list.rb +3 -63
- data/lib/inch/cli/command/base_object.rb +6 -28
- data/lib/inch/cli/command/list.rb +3 -2
- data/lib/inch/cli/command/options/base.rb +1 -1
- data/lib/inch/cli/command/options/base_list.rb +4 -2
- data/lib/inch/cli/command/options/suggest.rb +9 -8
- data/lib/inch/cli/command/output/base.rb +9 -11
- data/lib/inch/cli/command/output/list.rb +2 -2
- data/lib/inch/cli/command/output/show.rb +2 -10
- data/lib/inch/cli/command/output/stats.rb +4 -3
- data/lib/inch/cli/command/output/suggest.rb +5 -5
- data/lib/inch/cli/command/stats.rb +4 -3
- data/lib/inch/cli/command/suggest.rb +4 -94
- data/lib/inch/code_object.rb +2 -2
- data/lib/inch/code_object/converter.rb +89 -0
- data/lib/inch/code_object/provider.rb +36 -0
- data/lib/inch/code_object/provider/yard.rb +19 -0
- data/lib/inch/code_object/provider/yard/docstring.rb +106 -0
- data/lib/inch/code_object/provider/yard/nodoc_helper.rb +93 -0
- data/lib/inch/code_object/provider/yard/object.rb +55 -0
- data/lib/inch/code_object/provider/yard/object/base.rb +262 -0
- data/lib/inch/code_object/provider/yard/object/class_object.rb +12 -0
- data/lib/inch/code_object/provider/yard/object/constant_object.rb +12 -0
- data/lib/inch/code_object/provider/yard/object/method_object.rb +126 -0
- data/lib/inch/code_object/provider/yard/object/method_parameter_object.rb +88 -0
- data/lib/inch/code_object/provider/yard/object/module_object.rb +12 -0
- data/lib/inch/code_object/provider/yard/object/namespace_object.rb +47 -0
- data/lib/inch/code_object/provider/yard/parser.rb +54 -0
- data/lib/inch/code_object/proxy.rb +5 -3
- data/lib/inch/code_object/proxy/base.rb +103 -110
- data/lib/inch/code_object/proxy/class_object.rb +0 -1
- data/lib/inch/code_object/proxy/method_object.rb +20 -99
- data/lib/inch/code_object/proxy/method_parameter_object.rb +15 -39
- data/lib/inch/code_object/proxy/namespace_object.rb +7 -18
- data/lib/inch/codebase.rb +19 -0
- data/lib/inch/codebase/objects.rb +73 -0
- data/lib/inch/codebase/objects_filter.rb +61 -0
- data/lib/inch/codebase/proxy.rb +22 -0
- data/lib/inch/config.rb +8 -1
- data/lib/inch/evaluation.rb +5 -7
- data/lib/inch/evaluation/file.rb +1 -1
- data/lib/inch/evaluation/grade.rb +1 -1
- data/lib/inch/evaluation/object_schema.rb +3 -1
- data/lib/inch/evaluation/priority_range.rb +44 -0
- data/lib/inch/evaluation/proxy.rb +25 -0
- data/lib/inch/evaluation/proxy/base.rb +146 -0
- data/lib/inch/evaluation/proxy/class_object.rb +8 -0
- data/lib/inch/evaluation/proxy/constant_object.rb +19 -0
- data/lib/inch/evaluation/proxy/method_object.rb +65 -0
- data/lib/inch/evaluation/proxy/module_object.rb +8 -0
- data/lib/inch/evaluation/proxy/namespace_object.rb +27 -0
- data/lib/inch/evaluation/role/base.rb +19 -0
- data/lib/inch/evaluation/role/constant.rb +16 -0
- data/lib/inch/evaluation/role/method.rb +22 -0
- data/lib/inch/evaluation/role/method_parameter.rb +31 -1
- data/lib/inch/evaluation/role/namespace.rb +15 -0
- data/lib/inch/evaluation/role/object.rb +24 -0
- data/lib/inch/rake/suggest.rb +1 -0
- data/lib/inch/utils/read_write_methods.rb +44 -0
- data/lib/inch/{cli → utils}/weighted_list.rb +1 -1
- data/lib/inch/version.rb +1 -1
- data/test/fixtures/simple/lib/broken.rb +8 -0
- data/test/inch/api/filter_test.rb +51 -0
- data/test/inch/api/get_test.rb +22 -0
- data/test/inch/api/list_test.rb +15 -0
- data/test/inch/api/options/base_test.rb +30 -0
- data/test/inch/api/stats_test.rb +15 -0
- data/test/inch/api/suggest_test.rb +26 -0
- data/test/inch/cli/command/list_test.rb +2 -1
- data/test/inch/code_object/converter_test.rb +29 -0
- data/test/inch/code_object/{docstring_test.rb → provider/yard/docstring_test.rb} +13 -13
- data/test/inch/code_object/{nodoc_helper_test.rb → provider/yard/nodoc_helper_test.rb} +6 -6
- data/test/inch/code_object/provider/yard_test.rb +11 -0
- data/test/inch/code_object/provider_test.rb +9 -0
- data/test/inch/code_object/proxy/method_object_test.rb +22 -22
- data/test/inch/code_object/proxy_test.rb +10 -10
- data/test/inch/codebase/objects_test.rb +28 -0
- data/test/inch/codebase/proxy_test.rb +17 -0
- data/test/inch/evaluation/role/base_test.rb +71 -0
- data/test/inch/{cli → utils}/weighted_list_test.rb +2 -2
- data/test/shared/base_list.rb +73 -0
- data/test/test_helper.rb +0 -95
- metadata +89 -24
- data/lib/inch/code_object/docstring.rb +0 -102
- data/lib/inch/code_object/nodoc_helper.rb +0 -107
- data/lib/inch/evaluation/base.rb +0 -157
- data/lib/inch/evaluation/class_object.rb +0 -6
- data/lib/inch/evaluation/constant_object.rb +0 -33
- data/lib/inch/evaluation/method_object.rb +0 -105
- data/lib/inch/evaluation/module_object.rb +0 -6
- data/lib/inch/evaluation/namespace_object.rb +0 -52
- data/lib/inch/evaluation/read_write_methods.rb +0 -21
- data/lib/inch/source_parser.rb +0 -62
- data/test/inch/source_parser_test.rb +0 -23
@@ -7,6 +7,25 @@ module Inch
|
|
7
7
|
class Base
|
8
8
|
attr_reader :object
|
9
9
|
|
10
|
+
class << self
|
11
|
+
def applicable_if(symbol = nil, &block)
|
12
|
+
@applicable_procs ||= {}
|
13
|
+
@applicable_procs[to_s] = block || symbol.to_proc
|
14
|
+
end
|
15
|
+
|
16
|
+
def applicable_unless(symbol = nil, &block)
|
17
|
+
@applicable_procs ||= {}
|
18
|
+
@applicable_procs[to_s] = proc do |object|
|
19
|
+
!(block || symbol.to_proc).call(object)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def applicable?(object)
|
24
|
+
@applicable_procs ||= {}
|
25
|
+
@applicable_procs[to_s].call(object)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
10
29
|
def initialize(object, value = nil)
|
11
30
|
@object = object
|
12
31
|
@value = value
|
@@ -4,31 +4,47 @@ module Inch
|
|
4
4
|
# Roles assigned to constants
|
5
5
|
module Constant
|
6
6
|
class WithDoc < Object::WithDoc
|
7
|
+
applicable_if :has_doc?
|
7
8
|
end
|
8
9
|
class WithoutDoc < Object::WithoutDoc
|
10
|
+
applicable_unless :has_doc?
|
9
11
|
end
|
10
12
|
|
11
13
|
class TaggedAsNodoc < Object::TaggedAsNodoc
|
14
|
+
applicable_if :nodoc?
|
12
15
|
end
|
13
16
|
class InRoot < Object::InRoot
|
17
|
+
applicable_if :in_root?
|
14
18
|
end
|
15
19
|
|
16
20
|
class Public < Object::Public
|
21
|
+
applicable_if :public?
|
22
|
+
|
17
23
|
def priority
|
18
24
|
-1
|
19
25
|
end
|
20
26
|
end
|
21
27
|
class Private < Object::Private
|
28
|
+
applicable_if :private?
|
29
|
+
|
22
30
|
def priority
|
23
31
|
-3
|
24
32
|
end
|
25
33
|
end
|
26
34
|
|
27
35
|
class WithCodeExample < Object::WithCodeExample
|
36
|
+
applicable_if do |o|
|
37
|
+
o.has_code_example? && !o.has_multiple_code_examples?
|
38
|
+
end
|
28
39
|
end
|
40
|
+
|
29
41
|
class WithMultipleCodeExamples < Object::WithMultipleCodeExamples
|
42
|
+
applicable_if :has_multiple_code_examples?
|
30
43
|
end
|
44
|
+
|
31
45
|
class WithoutCodeExample < Object::WithoutCodeExample
|
46
|
+
applicable_unless :has_code_example?
|
47
|
+
|
32
48
|
def suggestion
|
33
49
|
nil
|
34
50
|
end
|
@@ -4,12 +4,15 @@ module Inch
|
|
4
4
|
module Method
|
5
5
|
# Role assigned to methods without parameters
|
6
6
|
class WithoutParameters < Base
|
7
|
+
applicable_unless :has_parameters?
|
7
8
|
end
|
8
9
|
|
9
10
|
# Role assigned to methods with many parameters
|
10
11
|
#
|
11
12
|
# @see CodeObject::Proxy::MethodObject#has_many_parameters?
|
12
13
|
class WithManyParameters < Base
|
14
|
+
applicable_if :has_many_parameters?
|
15
|
+
|
13
16
|
def priority
|
14
17
|
+2
|
15
18
|
end
|
@@ -17,10 +20,13 @@ module Inch
|
|
17
20
|
|
18
21
|
# Role assigned to methods where the return value is typed in the docs
|
19
22
|
class WithReturnType < Base
|
23
|
+
applicable_if :return_typed?
|
20
24
|
end
|
21
25
|
|
22
26
|
# Role assigned to methods where the return value is not typed
|
23
27
|
class WithoutReturnType < Missing
|
28
|
+
applicable_unless :return_typed?
|
29
|
+
|
24
30
|
def suggestion
|
25
31
|
"Describe what '#{object.name}' returns"
|
26
32
|
end
|
@@ -28,10 +34,13 @@ module Inch
|
|
28
34
|
|
29
35
|
# Role assigned to methods where the return value is decribed in the docs
|
30
36
|
class WithReturnDescription < Base
|
37
|
+
applicable_if :return_described?
|
31
38
|
end
|
32
39
|
|
33
40
|
# Role assigned to methods where the return value is not decribed
|
34
41
|
class WithoutReturnDescription < Missing
|
42
|
+
applicable_unless :return_described?
|
43
|
+
|
35
44
|
def suggestion
|
36
45
|
"Describe what '#{object.name}' returns"
|
37
46
|
end
|
@@ -42,6 +51,8 @@ module Inch
|
|
42
51
|
#
|
43
52
|
# @see CodeObject::Proxy::MethodObject#has_many_lines?
|
44
53
|
class WithManyLines < Base
|
54
|
+
applicable_if :has_many_lines?
|
55
|
+
|
45
56
|
def priority
|
46
57
|
+2
|
47
58
|
end
|
@@ -49,6 +60,8 @@ module Inch
|
|
49
60
|
|
50
61
|
# Role assigned to methods whose name end in a '!'
|
51
62
|
class WithBangName < Base
|
63
|
+
applicable_if :bang_name?
|
64
|
+
|
52
65
|
def priority
|
53
66
|
+3
|
54
67
|
end
|
@@ -56,6 +69,8 @@ module Inch
|
|
56
69
|
|
57
70
|
# Role assigned to methods whose name end in a '?'
|
58
71
|
class WithQuestioningName < Base
|
72
|
+
applicable_if :questioning_name?
|
73
|
+
|
59
74
|
def priority
|
60
75
|
-4
|
61
76
|
end
|
@@ -63,6 +78,8 @@ module Inch
|
|
63
78
|
|
64
79
|
# Role assigned to methods which are aliased
|
65
80
|
class HasAlias < Base
|
81
|
+
applicable_if :has_alias?
|
82
|
+
|
66
83
|
def priority
|
67
84
|
+2
|
68
85
|
end
|
@@ -70,18 +87,23 @@ module Inch
|
|
70
87
|
|
71
88
|
# Role assigned to methods that are constructors
|
72
89
|
class Constructor < Base
|
90
|
+
applicable_if :constructor?
|
73
91
|
end
|
74
92
|
|
75
93
|
# Role assigned to methods that are getters
|
76
94
|
class Getter < Base
|
95
|
+
applicable_if :getter?
|
77
96
|
end
|
78
97
|
|
79
98
|
# Role assigned to methods that are setters
|
80
99
|
class Setter < Base
|
100
|
+
applicable_if :setter?
|
81
101
|
end
|
82
102
|
|
83
103
|
# Role assigned to methods that are overriding another method
|
84
104
|
class Overridden < Base
|
105
|
+
applicable_if :overridden?
|
106
|
+
|
85
107
|
# It seems more important to document the overridden method,
|
86
108
|
# than the overriding one
|
87
109
|
def priority
|
@@ -2,35 +2,58 @@ module Inch
|
|
2
2
|
module Evaluation
|
3
3
|
module Role
|
4
4
|
# Roles assigned to method parameters
|
5
|
+
#
|
6
|
+
# @note The +object+ is a MethodParameterObject here!
|
5
7
|
module MethodParameter
|
6
8
|
# Role assigned to parameters that are mentioned in the docs
|
9
|
+
#
|
10
|
+
# @see CodeObject::Proxy::MethodParameterObject#mentioned?
|
7
11
|
class WithMention < Base
|
12
|
+
applicable_if :mentioned?
|
8
13
|
end
|
9
14
|
|
10
15
|
# Role assigned to parameters that are not mentioned in the docs
|
16
|
+
#
|
17
|
+
# @see CodeObject::Proxy::MethodParameterObject#mentioned?
|
11
18
|
class WithoutMention < Missing
|
19
|
+
applicable_unless :mentioned?
|
20
|
+
|
12
21
|
def suggestion
|
13
22
|
"Describe the parameter '#{object.name}'"
|
14
23
|
end
|
15
24
|
end
|
16
25
|
|
17
26
|
# Role assigned to parameters that are typed in the docs
|
27
|
+
#
|
28
|
+
# @see CodeObject::Proxy::MethodParameterObject#typed?
|
18
29
|
class WithType < Base
|
30
|
+
applicable_if :typed?
|
19
31
|
end
|
20
32
|
|
21
33
|
# Role assigned to parameters that are not typed in the docs
|
34
|
+
#
|
35
|
+
# @see CodeObject::Proxy::MethodParameterObject#typed?
|
22
36
|
class WithoutType < Missing
|
37
|
+
applicable_unless :typed?
|
23
38
|
end
|
24
39
|
|
25
40
|
# Role assigned to parameters that are spalts, e.g. +*args+
|
41
|
+
#
|
42
|
+
# @see CodeObject::Proxy::MethodParameterObject#splat?
|
26
43
|
class Splat < Base
|
44
|
+
applicable_if :splat?
|
45
|
+
|
27
46
|
def priority
|
28
47
|
+1
|
29
48
|
end
|
30
49
|
end
|
31
50
|
|
32
51
|
# Role assigned to parameters that are blocks, e.g. +&block+
|
52
|
+
#
|
53
|
+
# @see CodeObject::Proxy::MethodParameterObject#block?
|
33
54
|
class Block < Base
|
55
|
+
applicable_if :block?
|
56
|
+
|
34
57
|
def priority
|
35
58
|
+1
|
36
59
|
end
|
@@ -38,10 +61,15 @@ module Inch
|
|
38
61
|
|
39
62
|
# Role assigned to parameters that are documented, but not part of
|
40
63
|
# the method signature
|
41
|
-
|
64
|
+
#
|
65
|
+
# @see CodeObject::Proxy::MethodParameterObject#wrongly_mentioned?
|
66
|
+
class WithWrongMention < Base
|
67
|
+
applicable_if :wrongly_mentioned?
|
68
|
+
|
42
69
|
def suggestion
|
43
70
|
"The parameter '#{object.name}' seems not to be part of the signature."
|
44
71
|
end
|
72
|
+
|
45
73
|
def priority
|
46
74
|
+1
|
47
75
|
end
|
@@ -51,6 +79,8 @@ module Inch
|
|
51
79
|
#
|
52
80
|
# @see CodeObject::Proxy::MethodParameterObject#bad_name?
|
53
81
|
class WithBadName < Base
|
82
|
+
applicable_if :bad_name?
|
83
|
+
|
54
84
|
def priority
|
55
85
|
+1
|
56
86
|
end
|
@@ -4,7 +4,11 @@ module Inch
|
|
4
4
|
# Roles assigned to namespaces (classes and modules)
|
5
5
|
module Namespace
|
6
6
|
# Role assigned to namespaces with children
|
7
|
+
#
|
8
|
+
# @see CodeObject::Proxy::NamespaceObject#has_children?
|
7
9
|
class WithChildren < Base
|
10
|
+
applicable_if :has_children?
|
11
|
+
|
8
12
|
# This role doesnot assign a score.
|
9
13
|
def score
|
10
14
|
0
|
@@ -20,6 +24,8 @@ module Inch
|
|
20
24
|
#
|
21
25
|
# @see CodeObject::Proxy::NamespaceObject#has_many_children?
|
22
26
|
class WithManyChildren < Base
|
27
|
+
applicable_if :has_many_children?
|
28
|
+
|
23
29
|
# +priority
|
24
30
|
def priority
|
25
31
|
+1
|
@@ -30,6 +36,8 @@ module Inch
|
|
30
36
|
#
|
31
37
|
# @see CodeObject::Proxy::NamespaceObject#has_many_attributes?
|
32
38
|
class WithManyAttributes < Base
|
39
|
+
applicable_if :has_many_attributes?
|
40
|
+
|
33
41
|
# +priority
|
34
42
|
def priority
|
35
43
|
+1
|
@@ -38,10 +46,13 @@ module Inch
|
|
38
46
|
|
39
47
|
# Role assigned to namespaces without any children
|
40
48
|
class WithoutChildren < Base
|
49
|
+
applicable_unless :has_children?
|
41
50
|
end
|
42
51
|
|
43
52
|
# Role assigned to namespaces without any methods
|
44
53
|
class WithoutMethods < Base
|
54
|
+
applicable_unless :has_methods?
|
55
|
+
|
45
56
|
def priority
|
46
57
|
-2
|
47
58
|
end
|
@@ -49,6 +60,8 @@ module Inch
|
|
49
60
|
|
50
61
|
# A 'pure' namespace has only namespaces as children
|
51
62
|
class Pure < Base
|
63
|
+
applicable_if :pure_namespace?
|
64
|
+
|
52
65
|
def priority
|
53
66
|
-2
|
54
67
|
end
|
@@ -60,6 +73,8 @@ module Inch
|
|
60
73
|
# (the reasoning here is: just because we patch Hash does not mean
|
61
74
|
# we need to document the Hash class itself)
|
62
75
|
class Core < Base
|
76
|
+
applicable_if :core?
|
77
|
+
|
63
78
|
def priority
|
64
79
|
-7
|
65
80
|
end
|
@@ -5,10 +5,13 @@ module Inch
|
|
5
5
|
module Object
|
6
6
|
# Role assigned to objects with a describing comment (docstring)
|
7
7
|
class WithDoc < Base
|
8
|
+
applicable_if :has_doc?
|
8
9
|
end
|
9
10
|
|
10
11
|
# Role assigned to objects without a docstring
|
11
12
|
class WithoutDoc < Missing
|
13
|
+
applicable_unless :has_doc?
|
14
|
+
|
12
15
|
def suggestion
|
13
16
|
"Add a comment describing the #{object_type}"
|
14
17
|
end
|
@@ -18,6 +21,8 @@ module Inch
|
|
18
21
|
# considered by Inch. Since these tags are parsed from the docstring
|
19
22
|
# the object seems undocumented to Inch.
|
20
23
|
class Tagged < Base
|
24
|
+
applicable_if :has_unconsidered_tags?
|
25
|
+
|
21
26
|
def priority
|
22
27
|
-1
|
23
28
|
end
|
@@ -28,6 +33,8 @@ module Inch
|
|
28
33
|
#
|
29
34
|
# @see CodeObject::NodocHelper
|
30
35
|
class TaggedAsNodoc < Base
|
36
|
+
applicable_if :nodoc?
|
37
|
+
|
31
38
|
def priority
|
32
39
|
-7
|
33
40
|
end
|
@@ -37,11 +44,14 @@ module Inch
|
|
37
44
|
# of an API. If the API is 'private' TaggedAsPrivateAPI is assigned
|
38
45
|
# instead.
|
39
46
|
class TaggedAsAPI < Base
|
47
|
+
applicable_if :api_tag?
|
40
48
|
end
|
41
49
|
|
42
50
|
# Role assigned to objects explicitly or implicitly tagged to be part
|
43
51
|
# of a private API.
|
44
52
|
class TaggedAsPrivateAPI < Base
|
53
|
+
applicable_if :private_api_tag?
|
54
|
+
|
45
55
|
def priority
|
46
56
|
-5
|
47
57
|
end
|
@@ -49,6 +59,8 @@ module Inch
|
|
49
59
|
|
50
60
|
# Role assigned to objects declared in the top-level namespace
|
51
61
|
class InRoot < Base
|
62
|
+
applicable_if :in_root?
|
63
|
+
|
52
64
|
def priority
|
53
65
|
+3
|
54
66
|
end
|
@@ -56,6 +68,8 @@ module Inch
|
|
56
68
|
|
57
69
|
# Role assigned to public objects
|
58
70
|
class Public < Base
|
71
|
+
applicable_if :public?
|
72
|
+
|
59
73
|
def priority
|
60
74
|
+2
|
61
75
|
end
|
@@ -63,6 +77,8 @@ module Inch
|
|
63
77
|
|
64
78
|
# Role assigned to protected objects
|
65
79
|
class Protected < Base
|
80
|
+
applicable_if :protected?
|
81
|
+
|
66
82
|
def priority
|
67
83
|
+1
|
68
84
|
end
|
@@ -70,6 +86,8 @@ module Inch
|
|
70
86
|
|
71
87
|
# Role assigned to private objects
|
72
88
|
class Private < Base
|
89
|
+
applicable_if :private?
|
90
|
+
|
73
91
|
def priority
|
74
92
|
-2
|
75
93
|
end
|
@@ -77,14 +95,20 @@ module Inch
|
|
77
95
|
|
78
96
|
# Role assigned to objects with a single code example
|
79
97
|
class WithCodeExample < Base
|
98
|
+
applicable_if do |o|
|
99
|
+
o.has_code_example? && !o.has_multiple_code_examples?
|
100
|
+
end
|
80
101
|
end
|
81
102
|
|
82
103
|
# Role assigned to objects with multiple code examples
|
83
104
|
class WithMultipleCodeExamples < Base
|
105
|
+
applicable_if :has_multiple_code_examples?
|
84
106
|
end
|
85
107
|
|
86
108
|
# Role assigned to objects without a code example
|
87
109
|
class WithoutCodeExample < Missing
|
110
|
+
applicable_unless :has_code_example?
|
111
|
+
|
88
112
|
def suggestion
|
89
113
|
"Add a code example (optional)"
|
90
114
|
end
|
data/lib/inch/rake/suggest.rb
CHANGED
@@ -0,0 +1,44 @@
|
|
1
|
+
module Inch
|
2
|
+
module Utils
|
3
|
+
# Extend a class with ReadWriteMethods and you will gain the +rw_methods+
|
4
|
+
# macro.
|
5
|
+
#
|
6
|
+
# class User
|
7
|
+
# extend ReadWriteMethods
|
8
|
+
# rw_methods :name, :admin
|
9
|
+
# end
|
10
|
+
#
|
11
|
+
# This implements read methods that act as writer methods when called
|
12
|
+
# with a value parameter:
|
13
|
+
#
|
14
|
+
# user = User.new
|
15
|
+
# user.name # => nil
|
16
|
+
# user.name "Adam"
|
17
|
+
# user.name # => "Adam"
|
18
|
+
#
|
19
|
+
module ReadWriteMethods
|
20
|
+
# Implements a read method that acts as writer if called with a value
|
21
|
+
#
|
22
|
+
# @return [void]
|
23
|
+
def rw_method(name)
|
24
|
+
class_eval """
|
25
|
+
def #{name}(value = nil)
|
26
|
+
if value.nil?
|
27
|
+
@#{name}
|
28
|
+
else
|
29
|
+
@#{name} = value
|
30
|
+
end
|
31
|
+
end
|
32
|
+
"""
|
33
|
+
end
|
34
|
+
|
35
|
+
# Implements multiple read(write) methods with the given +names+
|
36
|
+
#
|
37
|
+
# @param names [Array<String,Symbol>]
|
38
|
+
# @return [void]
|
39
|
+
def rw_methods(*names)
|
40
|
+
[names].flatten.each { |name| rw_method(name) }
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|