class-cattr 0.1.3 → 0.3.1

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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/.version +1 -1
  3. data/lib/lib/base.rb +46 -13
  4. data/lib/lib/proxy.rb +14 -14
  5. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e5b8ee894264312e37f4ab8c50c9b968f78daa42bf099cb83a10bea0110074da
4
- data.tar.gz: 71287dd412409772d3ca15f9260da2b9bdf419164644088d13552f9d5db4ad54
3
+ metadata.gz: daf0e9dce7f539dcee2efa21621efe388773b7d8d5a241bee4a752306857a37f
4
+ data.tar.gz: 3f8afb451644f3d88e158943de7cd9c33cf84dd26724067bf5b64667d7b18064
5
5
  SHA512:
6
- metadata.gz: 88948b3e2d2a34b3d153dcdbafcf054159c5b01d55ce2f6c7b4212d39cc5256a1f1b53507eec4dcc41bfe3a903e9d22f16162be492040c56f59179c025c52901
7
- data.tar.gz: 786b42b9e22806b4aa3ab92d45f0706f613c1c1439e6b9dd7c06dedd4fdfcd9775bcb26732acb183e21b6ec2d176d2f81fafdad48f9a90c16b0d418209981702
6
+ metadata.gz: 80dd686971edcea3eeb2316ac6f3ea4e78c9e0132e66a810ff86e471a795cf377947fc8694b21923512f42158942ab968f0b808178be61a0877a96fd542de72c
7
+ data.tar.gz: 9b1ac1643ff555037c60ba35f0a96347aa277144f7db4c08399c6b080cdb628a9a99c1d665524013f48f2aa16763237c1521fb7135a55244ccd091c9f9e97fd8
data/.version CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.3.1
data/lib/lib/base.rb CHANGED
@@ -1,23 +1,56 @@
1
1
  class Class
2
- def cattr name = nil, value = nil, &block
3
- return CattrProxy.new(self) unless name
4
-
5
- if !value.nil? || block
6
- define_singleton_method(name) do |arg = :_nil|
7
- if arg == :_nil
8
- CattrProxy.new(self).send(name)
9
- else
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
- if !value.nil?
15
- CattrProxy.new(self).send('%s=' % name, value)
16
- elsif block
17
- CattrProxy.new(self).send(name, value, &block)
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
- CattrProxy.new(self).send('%s=' % name, nil)
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, &block
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
- if block_given?
13
- @host.instance_variable_set name, block
14
- else
15
- unless value.nil?
16
- raise ArgumentError, 'Plese use setter cattr.%s= to set argument' % key.to_s.sub('=', '')
17
- end
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
- for el in @host.ancestors
20
- if el.respond_to?(:superclass) && el != Object && el.instance_variable_defined?(name)
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
- raise ArgumentError, 'Cattr class variable "cattr.%s" not defined on "%s".' % [name.sub('@cattr_', ''), @host]
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.3
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: 2021-05-22 00:00:00.000000000 Z
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.0.6
42
+ rubygems_version: 3.2.22
43
43
  signing_key:
44
44
  specification_version: 4
45
45
  summary: Ruby class attributes