class-cattr 0.1.3 → 0.3.1
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 +46 -13
- 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: daf0e9dce7f539dcee2efa21621efe388773b7d8d5a241bee4a752306857a37f
|
4
|
+
data.tar.gz: 3f8afb451644f3d88e158943de7cd9c33cf84dd26724067bf5b64667d7b18064
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 80dd686971edcea3eeb2316ac6f3ea4e78c9e0132e66a810ff86e471a795cf377947fc8694b21923512f42158942ab968f0b808178be61a0877a96fd542de72c
|
7
|
+
data.tar.gz: 9b1ac1643ff555037c60ba35f0a96347aa277144f7db4c08399c6b080cdb628a9a99c1d665524013f48f2aa16763237c1521fb7135a55244ccd091c9f9e97fd8
|
data/.version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1
|
1
|
+
0.3.1
|
data/lib/lib/base.rb
CHANGED
@@ -1,23 +1,56 @@
|
|
1
1
|
class Class
|
2
|
-
def cattr name = nil,
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
2
|
+
def cattr name = nil, opts = {}, &block
|
3
|
+
if name
|
4
|
+
# cattr :foo, class: true, instance: true, default: proc { bar }
|
5
|
+
|
6
|
+
unless opts.class == Hash
|
7
|
+
raise ArgumentError, 'Options are not a Hash'
|
8
|
+
end
|
9
|
+
|
10
|
+
# if block provided, set it as a default value
|
11
|
+
if block
|
12
|
+
opts[:default] = block
|
13
|
+
end
|
14
|
+
|
15
|
+
# set class methods
|
16
|
+
if opts[:class]
|
17
|
+
define_singleton_method('%s=' % name) do |arg|
|
10
18
|
CattrProxy.new(self).send('%s=' % name, arg)
|
11
19
|
end
|
20
|
+
|
21
|
+
define_singleton_method(name) do |arg = :_nil|
|
22
|
+
if arg == :_nil
|
23
|
+
CattrProxy.new(self).send(name)
|
24
|
+
else
|
25
|
+
CattrProxy.new(self).send('%s=' % name, arg)
|
26
|
+
end
|
27
|
+
end
|
12
28
|
end
|
13
29
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
30
|
+
# set instance methods
|
31
|
+
if opts[:instance]
|
32
|
+
define_method('%s=' % name) do |arg|
|
33
|
+
CattrProxy.new(self.class).send('%s=' % name, arg)
|
34
|
+
end
|
35
|
+
|
36
|
+
define_method(name) do
|
37
|
+
CattrProxy.new(self.class).send(name)
|
38
|
+
end
|
18
39
|
end
|
40
|
+
|
41
|
+
# capture bad arguments
|
42
|
+
supported = [:default, :class, :instance]
|
43
|
+
invalid = opts.keys - supported
|
44
|
+
if invalid.first
|
45
|
+
raise ArgumentError, 'Invalid argument :%s, supported: %s' % [invalid.first, supported.join(', ')]
|
46
|
+
end
|
47
|
+
|
48
|
+
# set values
|
49
|
+
CattrProxy.new(self).send('%s=' % name, opts[:default])
|
19
50
|
else
|
20
|
-
|
51
|
+
# Klass.cattr.foo
|
52
|
+
|
53
|
+
CattrProxy.new(self)
|
21
54
|
end
|
22
55
|
end
|
23
56
|
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.1
|
4
|
+
version: 0.3.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:
|
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
|