class-mattr 0.2.0
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 +7 -0
- data/.version +1 -0
- data/lib/class-mattr.rb +3 -0
- data/lib/lib/base.rb +32 -0
- data/lib/lib/proxy.rb +44 -0
- metadata +47 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d08fac693f01d5be1454ea2c40a2dee6ea1641063512d16d64bad83ea509a1bf
|
4
|
+
data.tar.gz: 95e08b65b37bd9accb26e8e2292559fd29e80191f6e6002c7714374c9ae6f6c1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a18681b8d8740aa05c350a763379433cbaca8b0307d100862178595d80e26fe60411cf88c28842c037e3eb3989c38dea87d47d0ec13122d6220d22a15453c2d5
|
7
|
+
data.tar.gz: 304cd89f9fe577a6824ce4dda8fcb581955a4dffbd2d644f171c574afa07efb959b1357483e8b5cd506f49a46dc5294fe7b82865add770512e6f02170b2192f5
|
data/.version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.2.0
|
data/lib/class-mattr.rb
ADDED
data/lib/lib/base.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
module ClassMattr
|
2
|
+
def self.included base
|
3
|
+
def base.mattr name=nil
|
4
|
+
if name
|
5
|
+
if name.is_a?(Array)
|
6
|
+
for el in name
|
7
|
+
self.define_singleton_method(el) do |*args|
|
8
|
+
::ClassMattr::Proxy.new(self).send(el, *args)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
else
|
12
|
+
::ClassMattr::Proxy.new(self)._get(name)
|
13
|
+
end
|
14
|
+
else
|
15
|
+
::ClassMattr::Proxy.new(self)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def base.method_added name
|
20
|
+
::ClassMattr::Proxy.new(self.class)._set name
|
21
|
+
super
|
22
|
+
end
|
23
|
+
|
24
|
+
def mattr name
|
25
|
+
::ClassMattr::Proxy.new(self.class)._get(name)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def mattr name
|
30
|
+
::ClassMattr::Proxy.new(self.class)._get(name)
|
31
|
+
end
|
32
|
+
end
|
data/lib/lib/proxy.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
module ClassMattr
|
2
|
+
class Proxy
|
3
|
+
MATTRS ||= {}
|
4
|
+
@@mattrs ||= []
|
5
|
+
|
6
|
+
def initialize host
|
7
|
+
@host = host
|
8
|
+
end
|
9
|
+
|
10
|
+
def _set name
|
11
|
+
while el = @@mattrs.shift
|
12
|
+
klass, trait, opts = el[0], el[1].to_sym, el[2]
|
13
|
+
|
14
|
+
MATTRS[klass] ||= {}
|
15
|
+
el = MATTRS[klass][name.to_sym] ||= {}
|
16
|
+
|
17
|
+
if el[trait]
|
18
|
+
# if trait exists (double define)
|
19
|
+
# convert to array and push
|
20
|
+
el[trait] = [el[trait]] unless el[trait].is_a?(Array)
|
21
|
+
el[trait].push opts
|
22
|
+
else
|
23
|
+
# default value if true if no argument provided
|
24
|
+
el[trait] = opts.nil? ? true : opts
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def _get name
|
30
|
+
for klass in @host.ancestors.map(&:to_s)
|
31
|
+
return {} if klass == 'ClassMattr'
|
32
|
+
|
33
|
+
hash = MATTRS.dig(klass, name.to_sym)
|
34
|
+
return hash if hash
|
35
|
+
end
|
36
|
+
|
37
|
+
raise 'ClassMattr not included?'
|
38
|
+
end
|
39
|
+
|
40
|
+
def method_missing name, value=nil
|
41
|
+
@@mattrs.push [@host.to_s, name.to_s, value]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: class-mattr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dino Reic
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-02-15 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Mothod attributes are not natively supported by ruby, this fixes the
|
14
|
+
problem!
|
15
|
+
email: reic.dino@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- "./.version"
|
21
|
+
- "./lib/class-mattr.rb"
|
22
|
+
- "./lib/lib/base.rb"
|
23
|
+
- "./lib/lib/proxy.rb"
|
24
|
+
homepage: https://github.com/dux/class-mattr
|
25
|
+
licenses:
|
26
|
+
- MIT
|
27
|
+
metadata: {}
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
requirements: []
|
43
|
+
rubygems_version: 3.0.6
|
44
|
+
signing_key:
|
45
|
+
specification_version: 4
|
46
|
+
summary: Ruby method attributes
|
47
|
+
test_files: []
|