class-cattr 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.version +1 -1
- data/lib/lib/base.rb +35 -14
- data/lib/lib/proxy.rb +13 -17
- 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: 6afe725cd74e87fa927f966c51613a7be9f519220e182a200010cb78176692a7
|
4
|
+
data.tar.gz: eec357ac38c904324ced0cab8fe2e71480a87388d67ab8ce2ff65cf3b2c4954d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 910462bc7cf06b0077cd993ecb19e8e0db778f6af3ccd8eacc5057d7296bde6546af70a305adc3773364661077b1b0f9a6d5950007b0bbd6157ba5ab40c3ed5a
|
7
|
+
data.tar.gz: c4507794f655b5aaaf2563680522794007d51fdb3538d170e931f0ac3c07337808fe88e440829ac58f7d99843b44b4c3f55a6c7c68b5119c1ff6221143271ce1
|
data/.version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/lib/lib/base.rb
CHANGED
@@ -1,27 +1,48 @@
|
|
1
1
|
class Class
|
2
|
-
def cattr name = nil,
|
3
|
-
|
2
|
+
def cattr name = nil, opts = {}, &block
|
3
|
+
if name
|
4
|
+
# cattr :foo, class: true, instance: true, default: proc { bar }
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
6
|
+
# if block provided, set it as a default value
|
7
|
+
if block
|
8
|
+
opts[:default] = block
|
8
9
|
end
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
else
|
11
|
+
# set class methods
|
12
|
+
if opts[:class]
|
13
|
+
define_singleton_method('%s=' % name) do |arg|
|
14
14
|
CattrProxy.new(self).send('%s=' % name, arg)
|
15
15
|
end
|
16
|
+
|
17
|
+
define_singleton_method(name) do
|
18
|
+
CattrProxy.new(self).send(name)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# set instance methods
|
23
|
+
if opts[:instance]
|
24
|
+
define_method('%s=' % name) do |arg|
|
25
|
+
CattrProxy.new(self.class).send('%s=' % name, arg)
|
26
|
+
end
|
27
|
+
|
28
|
+
define_method(name) do
|
29
|
+
CattrProxy.new(self.class).send(name)
|
30
|
+
end
|
16
31
|
end
|
17
32
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
33
|
+
# capture bad arguments
|
34
|
+
supported = [:default, :class, :instance]
|
35
|
+
invalid = opts.keys - supported
|
36
|
+
if invalid.first
|
37
|
+
raise ArgumentError, 'Invalid argument :%s, supported: %s' % [invalid.first, supported.join(', ')]
|
22
38
|
end
|
39
|
+
|
40
|
+
# set values
|
41
|
+
CattrProxy.new(self).send('%s=' % name, opts[:default])
|
23
42
|
else
|
24
|
-
|
43
|
+
# Klass.cattr.foo
|
44
|
+
|
45
|
+
CattrProxy.new(self)
|
25
46
|
end
|
26
47
|
end
|
27
48
|
end
|
data/lib/lib/proxy.rb
CHANGED
@@ -3,33 +3,29 @@ class CattrProxy
|
|
3
3
|
@host = host
|
4
4
|
end
|
5
5
|
|
6
|
-
def method_missing key, value=nil
|
6
|
+
def method_missing key, value = nil
|
7
7
|
name = '@cattr_%s' % key
|
8
8
|
|
9
9
|
if name.sub!(/=$/, '')
|
10
10
|
@host.instance_variable_set name, value
|
11
11
|
else
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
unless value.nil?
|
16
|
-
raise ArgumentError, 'Plese use setter cattr.%s= to set argument' % key.to_s.sub('=', '')
|
17
|
-
end
|
18
|
-
|
19
|
-
for el in @host.ancestors
|
20
|
-
if el.respond_to?(:superclass) && el != Object && el.instance_variable_defined?(name)
|
21
|
-
local = el.instance_variable_get name
|
12
|
+
unless value.nil?
|
13
|
+
raise ArgumentError, 'Plese use setter cattr.%s= to set argument' % key
|
14
|
+
end
|
22
15
|
|
23
|
-
|
24
|
-
|
25
|
-
|
16
|
+
for el in @host.ancestors
|
17
|
+
if el.respond_to?(:superclass) && el != Object && el.instance_variable_defined?(name)
|
18
|
+
local = el.instance_variable_get name
|
26
19
|
|
27
|
-
|
20
|
+
if local === Proc
|
21
|
+
local = @host.instance_exec &local
|
28
22
|
end
|
29
|
-
end
|
30
23
|
|
31
|
-
|
24
|
+
return local
|
25
|
+
end
|
32
26
|
end
|
27
|
+
|
28
|
+
raise ArgumentError, 'Cattr class variable "cattr.%s" not defined on "%s".' % [name.sub('@cattr_', ''), @host]
|
33
29
|
end
|
34
30
|
end
|
35
31
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: class-cattr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dino Reic
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-02-
|
11
|
+
date: 2022-02-13 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Class attributes are not natively supported by ruby, this fixes the problem!
|
14
14
|
email: reic.dino@gmail.com
|