classprop 1.3 → 1.4
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/README.md +33 -12
- data/lib/classprop.rb +22 -5
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 66c01968cae6780a152ab327e772168e8e7cab31b06257d888da56668c6a757e
|
4
|
+
data.tar.gz: dc4fb6f077418bf7d80c526ff5fbdbb11c75e8cd5c5b0fc0c2966fb406528901
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 82dc692237e50f5ed7bf52a08672b1ddd4b70f8aeb40ec2d966583c26dcbcf8bfc39e92d9acf1a4db44be1fa8a87824efcf38f8112f1fdee9b1107920b85e8d6
|
7
|
+
data.tar.gz: c7742d1933eae5d9bda92dde3dfc872e4832183c80b0058c92d2a92927f87c1a376b672065ef7c9f174d7dca687c51a7857dd9afa47f8faab54ccc59562ce1c2
|
data/README.md
CHANGED
@@ -8,15 +8,15 @@ called `fco`. Finally, it sets the value of that property to *base*.
|
|
8
8
|
```ruby
|
9
9
|
class Base
|
10
10
|
include ClassProp
|
11
|
-
define_class_prop '
|
12
|
-
self.
|
11
|
+
define_class_prop 'independent'
|
12
|
+
self.independent = 'base'
|
13
13
|
end
|
14
14
|
```
|
15
15
|
|
16
16
|
The `fco` property is now available from `Base`.
|
17
17
|
|
18
18
|
```ruby
|
19
|
-
puts Base.
|
19
|
+
puts Base.independent # => 'base'
|
20
20
|
```
|
21
21
|
|
22
22
|
Another class can inherit from `Base`.
|
@@ -30,21 +30,21 @@ If `X1` does not define its own `fco` property, then when that property is
|
|
30
30
|
retrieved from `X1` it returns `Base.fco`.
|
31
31
|
|
32
32
|
```ruby
|
33
|
-
puts X1.
|
33
|
+
puts X1.independent # => 'base'
|
34
34
|
```
|
35
35
|
|
36
36
|
`X1` can set its own value for `fco`.
|
37
37
|
|
38
38
|
```ruby
|
39
39
|
class X1
|
40
|
-
self.
|
40
|
+
self.independent = 'x1'
|
41
41
|
end
|
42
42
|
```
|
43
43
|
|
44
44
|
That gives us this output.
|
45
45
|
|
46
46
|
```ruby
|
47
|
-
puts X1.
|
47
|
+
puts X1.independent # => 'x1'
|
48
48
|
```
|
49
49
|
|
50
50
|
To delete a class property (so that the property is again inherited from the
|
@@ -52,10 +52,10 @@ superclass) use `delete_class_prop`.
|
|
52
52
|
|
53
53
|
```ruby
|
54
54
|
class X1
|
55
|
-
delete_class_prop '
|
55
|
+
delete_class_prop 'independent'
|
56
56
|
end
|
57
57
|
|
58
|
-
puts X1.
|
58
|
+
puts X1.independent # => 'base'
|
59
59
|
```
|
60
60
|
|
61
61
|
You can set a property so that it must be defined by subclasses. To do so, set
|
@@ -65,16 +65,36 @@ that property then an error is raised when the property is retrieved.
|
|
65
65
|
```ruby
|
66
66
|
class Base
|
67
67
|
include ClassProp
|
68
|
-
define_class_prop '
|
69
|
-
self.
|
68
|
+
define_class_prop 'independent'
|
69
|
+
self.independent = ClassProp::MustDefine
|
70
70
|
end
|
71
71
|
|
72
72
|
class X1 < Base
|
73
73
|
end
|
74
74
|
|
75
|
-
X1.
|
75
|
+
X1.independent # raises exception 'must-define-class-property: independent'
|
76
76
|
```
|
77
77
|
|
78
|
+
If you want to normalize or check a property, use the `before_set` option.
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
class Base
|
82
|
+
include ClassProp
|
83
|
+
|
84
|
+
# upper
|
85
|
+
define_class_prop(
|
86
|
+
'upper',
|
87
|
+
'before_set' => proc do |val|
|
88
|
+
val.to_s.upcase
|
89
|
+
end
|
90
|
+
);
|
91
|
+
end
|
92
|
+
|
93
|
+
Base.upper = 'whatever'
|
94
|
+
puts Base.upper #=> WHATEVER
|
95
|
+
```
|
96
|
+
|
97
|
+
|
78
98
|
## Install
|
79
99
|
|
80
100
|
```
|
@@ -93,4 +113,5 @@ mike@idocs.com
|
|
93
113
|
| 1.0 | Feb 24, 2020 | Initial upload. |
|
94
114
|
| 1.1 | Feb 24, 2020 | Reworked interface. |
|
95
115
|
| 1.2 | Feb 24, 2020 | Fixed minor problem with exception id. |
|
96
|
-
| 1.3 | Feb 26, 2020 | Added options to define_class_prop: init and inherit. |
|
116
|
+
| 1.3 | Feb 26, 2020 | Added options to define_class_prop: init and inherit. |
|
117
|
+
| 1.3 | Mar 13, 2020 | Added option to define_class_prop: before_set. |
|
data/lib/classprop.rb
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
#
|
4
4
|
module ClassProp
|
5
5
|
# version
|
6
|
-
VERSION = '1.
|
6
|
+
VERSION = '1.4'
|
7
7
|
|
8
8
|
# private
|
9
9
|
private
|
@@ -23,14 +23,14 @@ module ClassProp
|
|
23
23
|
#
|
24
24
|
# class Base
|
25
25
|
# include ClassProp
|
26
|
-
# define_class_prop '
|
27
|
-
# self.
|
26
|
+
# define_class_prop 'independent'
|
27
|
+
# self.independent = ClassProp::MustDefine
|
28
28
|
# end
|
29
29
|
#
|
30
30
|
# class X1 < Base
|
31
31
|
# end
|
32
32
|
#
|
33
|
-
# X1.
|
33
|
+
# X1.independent # raises exception 'must-define-class-property: independent'
|
34
34
|
module MustDefine
|
35
35
|
end
|
36
36
|
|
@@ -50,12 +50,29 @@ module ClassProp
|
|
50
50
|
#
|
51
51
|
# option: inherit
|
52
52
|
# If set to false, the property is not inherited by subclasses.
|
53
|
+
#
|
54
|
+
# option: before_set
|
55
|
+
# If set, must be a Proc. The proc is called and val is set to the
|
56
|
+
# proc's return value.
|
53
57
|
def define_class_prop(prop_name, opts={})
|
54
58
|
# default options
|
55
59
|
opts = {'inherit'=>true}.merge(opts)
|
56
60
|
|
61
|
+
# TESTING
|
62
|
+
# $tm.show opts
|
63
|
+
|
57
64
|
# set
|
58
65
|
self.define_singleton_method("#{prop_name}=") do |val|
|
66
|
+
# run before_set if necessary
|
67
|
+
if before_set = opts['before_set']
|
68
|
+
if not before_set.is_a?(Proc)
|
69
|
+
raise 'before-set-not-proc'
|
70
|
+
end
|
71
|
+
|
72
|
+
val = opts['before_set'].call(val)
|
73
|
+
end
|
74
|
+
|
75
|
+
# set property
|
59
76
|
instance_variable_set "@#{prop_name}", val
|
60
77
|
end
|
61
78
|
|
@@ -97,4 +114,4 @@ module ClassProp
|
|
97
114
|
end
|
98
115
|
#
|
99
116
|
# ClassProp
|
100
|
-
#===============================================================================
|
117
|
+
#===============================================================================
|