clean-annotations 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8d9303baf6e5dad38ca20584945c4e0c8da1df1b87ea34e0c312eca6dc5723ab
4
+ data.tar.gz: 0d9f07d7f5b397b8ca6510f19037c1bf89d76ac53aad20fcf085d676e0943c9f
5
+ SHA512:
6
+ metadata.gz: 4ad56b523d3c8cc57328853e2d888e323c59e2844e32324681fde6b1051181a9722d66109c2195cde2d06fd7c1ecd3b3d36b2620b9f4a8215bd74898327059e7
7
+ data.tar.gz: f63139c6deed120ed6679b8214c35c72687925d643d63c44796f536f76e8428566967321d8281b388b793ab3d8772e71def90bf3e9575688e57ba223604086a3
data/.version ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,11 @@
1
+ require_relative './clean-annotations/class_callbacks'
2
+
3
+ # do not load if allready defined
4
+
5
+ unless Object.respond_to?(:class_attribute)
6
+ require_relative './clean-annotations/class_attribute'
7
+ end
8
+
9
+ unless Object.respond_to?(:method_attr)
10
+ require_relative './clean-annotations/method_attr'
11
+ end
@@ -0,0 +1,55 @@
1
+ # Defines class variable
2
+
3
+ def Object.class_attribute name, default=nil, &block
4
+ raise ArgumentError.new('name must be symbol') unless name.is_a?(Symbol)
5
+
6
+ ivar = "@class_attribute_#{name}"
7
+
8
+ instance_variable_set ivar, block || default
9
+
10
+ define_singleton_method('%s=' % name) { |arg| send(name, arg) }
11
+ define_singleton_method(name) do |arg=:_undefined, &block|
12
+ # define and set if argument given
13
+ if block || arg != :_undefined
14
+ return instance_variable_set ivar, block || arg
15
+ end
16
+
17
+ # find value and return
18
+ ancestors.each do |klass|
19
+ if klass.instance_variable_defined?(ivar)
20
+ value = klass.instance_variable_get ivar
21
+ return value.is_a?(Proc) ? instance_exec(&value) : value
22
+ end
23
+ end
24
+ end
25
+
26
+ def class_attribute name
27
+ self.class.send(name)
28
+ end
29
+ end
30
+
31
+ # class A
32
+ # class_attribute :layout, 'default'
33
+ # class_attribute(:time) { Time.now }
34
+ # end
35
+
36
+ # class B < A
37
+ # layout 'main'
38
+ # end
39
+
40
+ # class C < B
41
+ # time '11:55'
42
+ # end
43
+
44
+ # for func in [:layout, :time]
45
+ # for klass in [A, B, C]
46
+ # puts "> %s = %s" % ["#{klass}.#{func}".ljust(8), klass.send(func)]
47
+ # end
48
+ # end
49
+
50
+ # # > A.layout = default
51
+ # # > B.layout = main
52
+ # # > C.layout = main
53
+ # # > A.time = 2019-10-28 18:07:33 +0100
54
+ # # > B.time = 2019-10-28 18:07:33 +0100
55
+ # # > C.time = 11:55
@@ -0,0 +1,47 @@
1
+ # Rails style callbacks
2
+
3
+ class Object
4
+ def class_callback name, arg=nil
5
+ Object.class_callback name, self, arg
6
+ end
7
+
8
+ def self.class_callback name, context=nil, arg=nil
9
+ ivar = "@class_callbacks_#{name}"
10
+
11
+ unless context
12
+ define_singleton_method(name) do |method_name=nil, &block|
13
+ ref = caller[0].split(':in ').first
14
+
15
+ self.instance_variable_set(ivar, {}) unless instance_variable_defined?(ivar)
16
+ self.instance_variable_get(ivar)[ref] = method_name || block
17
+ end
18
+
19
+ else
20
+ list = context.respond_to?(:const_missing) && context.respond_to?(:ancestors) ? context.ancestors : context.class.ancestors
21
+ list = list.slice 0, list.index(Object) if list.index(Object)
22
+
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
+ context.send m
29
+ else
30
+ context.instance_exec arg, &m
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ # for controllers, execute from AppController to MainController
40
+ # class_callback :before
41
+ # before do
42
+ # ...
43
+ # end
44
+ # before :method_name
45
+ # instance = new
46
+ # instance.class_callback :before,
47
+ # instance.class_callback :before, arg
@@ -0,0 +1,77 @@
1
+ module MethodAttributes
2
+ extend self
3
+
4
+ @@GLOBAL_OPTS = {}
5
+ @@METHOD_OPTS = {}
6
+
7
+ def define klass, param_name, &block
8
+ klass.define_singleton_method(param_name) do |*args|
9
+ @@METHOD_OPTS[param_name] ||= []
10
+ @@METHOD_OPTS[param_name].push block ? block.call(*args) : args[0]
11
+ end
12
+
13
+ klass.define_singleton_method(:method_added) do |name|
14
+ return unless @@METHOD_OPTS.keys.first
15
+
16
+ @@GLOBAL_OPTS[to_s] ||= {}
17
+ @@GLOBAL_OPTS[to_s][name] = @@METHOD_OPTS.dup
18
+ @@METHOD_OPTS.clear
19
+ end
20
+ end
21
+
22
+ def get klass, method_name=nil
23
+ return @@GLOBAL_OPTS[klass.to_s] unless method_name
24
+
25
+ klass.ancestors.map(&:to_s).each do |a_klass|
26
+ v = @@GLOBAL_OPTS[a_klass][method_name]
27
+ return v if v
28
+ end
29
+ end
30
+ end
31
+
32
+ ###
33
+
34
+ class Object
35
+ def method_attr name=nil, &block
36
+ if respond_to?(:const_missing) && respond_to?(:ancestors)
37
+ if name.nil?
38
+ return MethodAttributes.get(self) || {}
39
+ end
40
+
41
+ MethodAttributes.define self, name, &block
42
+ else
43
+ # instance
44
+ base = MethodAttributes.get(self.class)
45
+ name ? base[name] : base
46
+ end
47
+ end
48
+ end
49
+
50
+ # class Foo
51
+ # method_attr :name
52
+ # method_attr :param do |field, type=String, opts={}|
53
+ # opts[:name] = field
54
+ # opts[:type] ||= String
55
+ # opts
56
+ # end
57
+
58
+ # name "Test method desc"
59
+ # param :email, :email
60
+ # def test
61
+ # end
62
+ # end
63
+
64
+ # ap Foo.method_attr
65
+ # {
66
+ # "test": {
67
+ # "name": [
68
+ # "Test method desc"
69
+ # ],
70
+ # "param": [
71
+ # {
72
+ # "name": "email",
73
+ # "type": "String"
74
+ # }
75
+ # ]
76
+ # }
77
+ # }
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: clean-annotations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dino Reic
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-12-09 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Define annotatable attribute names and assign them to methods or classes,
14
+ add callbacks to non-Rails classes. Void of dependencies.
15
+ email: reic.dino@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - "./.version"
21
+ - "./lib/clean-annotations.rb"
22
+ - "./lib/clean-annotations/class_attribute.rb"
23
+ - "./lib/clean-annotations/class_callbacks.rb"
24
+ - "./lib/clean-annotations/method_attr.rb"
25
+ homepage: https://github.com/dux/clean-annotations
26
+ licenses:
27
+ - MIT
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubygems_version: 3.0.6
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: Annotate ruby methods and classes, define callbacks
48
+ test_files: []