class-cattr 0.1.2 → 0.3.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 +4 -4
- data/.version +1 -1
- data/lib/lib/base.rb +38 -9
- data/lib/lib/proxy.rb +14 -14
- metadata +3 -3
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,18 +1,47 @@
|
|
1
1
|
class Class
|
2
|
-
def cattr name = nil,
|
3
|
-
if name
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
2
|
+
def cattr name = nil, opts = {}, &block
|
3
|
+
if name
|
4
|
+
# cattr :foo, class: true, instance: true, default: proc { bar }
|
5
|
+
|
6
|
+
# if block provided, set it as a default value
|
7
|
+
if block
|
8
|
+
opts[:default] = block
|
9
|
+
end
|
10
|
+
|
11
|
+
# set class methods
|
12
|
+
if opts[:class]
|
13
|
+
define_singleton_method('%s=' % name) do |arg|
|
8
14
|
CattrProxy.new(self).send('%s=' % name, arg)
|
9
15
|
end
|
16
|
+
|
17
|
+
define_singleton_method(name) do
|
18
|
+
CattrProxy.new(self).send(name)
|
19
|
+
end
|
10
20
|
end
|
11
|
-
end
|
12
21
|
|
13
|
-
|
14
|
-
|
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
|
31
|
+
end
|
32
|
+
|
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(', ')]
|
38
|
+
end
|
39
|
+
|
40
|
+
# set values
|
41
|
+
CattrProxy.new(self).send('%s=' % name, opts[:default])
|
15
42
|
else
|
43
|
+
# Klass.cattr.foo
|
44
|
+
|
16
45
|
CattrProxy.new(self)
|
17
46
|
end
|
18
47
|
end
|
data/lib/lib/proxy.rb
CHANGED
@@ -3,29 +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
|
-
|
16
|
-
|
17
|
-
|
12
|
+
unless value.nil?
|
13
|
+
raise ArgumentError, 'Plese use setter cattr.%s= to set argument' % key
|
14
|
+
end
|
15
|
+
|
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
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
value = el.instance_variable_get name
|
22
|
-
value = value.call if value === Proc
|
23
|
-
return value
|
20
|
+
if local === Proc
|
21
|
+
local = @host.instance_exec &local
|
24
22
|
end
|
25
|
-
end
|
26
23
|
|
27
|
-
|
24
|
+
return local
|
25
|
+
end
|
28
26
|
end
|
27
|
+
|
28
|
+
raise ArgumentError, 'Cattr class variable "cattr.%s" not defined on "%s".' % [name.sub('@cattr_', ''), @host]
|
29
29
|
end
|
30
30
|
end
|
31
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:
|
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
|
@@ -39,7 +39,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
requirements: []
|
42
|
-
rubygems_version: 3.
|
42
|
+
rubygems_version: 3.2.22
|
43
43
|
signing_key:
|
44
44
|
specification_version: 4
|
45
45
|
summary: Ruby class attributes
|