clean-annotations 0.1.0 → 0.1.1
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/.version +1 -1
- data/lib/clean-annotations/class_attribute.rb +19 -15
- data/lib/clean-annotations/class_callbacks.rb +25 -26
- data/lib/clean-annotations/method_attr.rb +10 -8
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4a9aaf5121402a2a3a19d2e4aa0e5eb26d3a1cf8bb3aa9e32377bf7a7f38780e
|
4
|
+
data.tar.gz: 6dd0855bc5ce65fa2a1360ef703cff2fa427bd63a0b3f674b48b0f87ad883699
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2727df348b72d4df04c43bdc3671b945d0db5250249170257fe1ef15ffb87d33862ce18f8adc8cdb99b4b3e09d01a75a7631678b36b2e804f4f1309f02ff5cb5
|
7
|
+
data.tar.gz: 7db76fb31d58cd3feef260bdd46f36879a5e27fec7ab92c7b564cb6ddaa9dfb8a3f256d923aaeabce054019dfb0e3ea441c9a19ce238a9c688b098a7708f01f7
|
data/.version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|
@@ -1,28 +1,32 @@
|
|
1
1
|
# Defines class variable
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
class Class
|
4
|
+
def class_attribute name, default=nil, &block
|
5
|
+
raise ArgumentError.new('name must be symbol') unless name.is_a?(Symbol)
|
5
6
|
|
6
|
-
|
7
|
+
ivar = "@class_attribute_#{name}"
|
7
8
|
|
8
|
-
|
9
|
+
instance_variable_set ivar, block || default
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
11
|
+
define_singleton_method('%s=' % name) { |arg| send(name, arg) }
|
12
|
+
define_singleton_method(name) do |arg=:_undefined, &block|
|
13
|
+
# define and set if argument given
|
14
|
+
if block || arg != :_undefined
|
15
|
+
return instance_variable_set ivar, block || arg
|
16
|
+
end
|
16
17
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
18
|
+
# find value and return
|
19
|
+
ancestors.each do |klass|
|
20
|
+
if klass.instance_variable_defined?(ivar)
|
21
|
+
value = klass.instance_variable_get ivar
|
22
|
+
return value.is_a?(Proc) ? instance_exec(&value) : value
|
23
|
+
end
|
22
24
|
end
|
23
25
|
end
|
24
26
|
end
|
27
|
+
end
|
25
28
|
|
29
|
+
class Object
|
26
30
|
def class_attribute name
|
27
31
|
self.class.send(name)
|
28
32
|
end
|
@@ -1,34 +1,33 @@
|
|
1
1
|
# Rails style callbacks
|
2
2
|
|
3
|
-
class
|
4
|
-
def class_callback name
|
5
|
-
Object.class_callback name, self, arg
|
6
|
-
end
|
7
|
-
|
8
|
-
def self.class_callback name, context=nil, arg=nil
|
3
|
+
class Class
|
4
|
+
def class_callback name
|
9
5
|
ivar = "@class_callbacks_#{name}"
|
10
6
|
|
11
|
-
|
12
|
-
|
13
|
-
ref = caller[0].split(':in ').first
|
7
|
+
define_singleton_method(name) do |method_name=nil, &block|
|
8
|
+
ref = caller[0].split(':in ').first
|
14
9
|
|
15
|
-
|
16
|
-
|
17
|
-
|
10
|
+
self.instance_variable_set(ivar, {}) unless instance_variable_defined?(ivar)
|
11
|
+
self.instance_variable_get(ivar)[ref] = method_name || block
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class Object
|
17
|
+
def class_callback name, *args
|
18
|
+
ivar = "@class_callbacks_#{name}"
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
list = list.slice 0, list.index(Object) if list.index(Object)
|
20
|
+
list = self.class.ancestors
|
21
|
+
list = list.slice 0, list.index(Object) if list.index(Object)
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
end
|
23
|
+
list.reverse.each do |klass|
|
24
|
+
if klass.instance_variable_defined?(ivar)
|
25
|
+
mlist = klass.instance_variable_get(ivar).values
|
26
|
+
mlist.each do |m|
|
27
|
+
if m.is_a?(Symbol)
|
28
|
+
send m, *args
|
29
|
+
else
|
30
|
+
instance_exec *args, &m
|
32
31
|
end
|
33
32
|
end
|
34
33
|
end
|
@@ -43,5 +42,5 @@ end
|
|
43
42
|
# end
|
44
43
|
# before :method_name
|
45
44
|
# instance = new
|
46
|
-
# instance.class_callback :before
|
47
|
-
# instance.class_callback :before, arg
|
45
|
+
# instance.class_callback :before
|
46
|
+
# instance.class_callback :before, @arg
|
@@ -55,22 +55,24 @@ end
|
|
55
55
|
# opts
|
56
56
|
# end
|
57
57
|
|
58
|
-
# name "Test method desc"
|
58
|
+
# name "Test method desc 1"
|
59
|
+
# name "Test method desc 2"
|
59
60
|
# param :email, :email
|
60
61
|
# def test
|
61
62
|
# end
|
62
63
|
# end
|
63
64
|
|
64
|
-
#
|
65
|
+
# Foo.method_attr
|
65
66
|
# {
|
66
|
-
#
|
67
|
-
#
|
68
|
-
# "Test method desc"
|
67
|
+
# test: {
|
68
|
+
# name: [
|
69
|
+
# "Test method desc 1",
|
70
|
+
# "Test method desc 2"
|
69
71
|
# ],
|
70
|
-
#
|
72
|
+
# param: [
|
71
73
|
# {
|
72
|
-
#
|
73
|
-
#
|
74
|
+
# name: :email,
|
75
|
+
# type: String
|
74
76
|
# }
|
75
77
|
# ]
|
76
78
|
# }
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: clean-annotations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dino Reic
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-12-
|
11
|
+
date: 2019-12-11 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Define annotatable attribute names and assign them to methods or classes,
|
14
14
|
add callbacks to non-Rails classes. Void of dependencies.
|