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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/.version +1 -1
  3. data/lib/lib/base.rb +38 -9
  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: 1eda23fbd2a8040014f38211ae51181dd37c990f936860a8934e2b1a37912d22
4
- data.tar.gz: 47027b1c8fc3a495160c792a85cc99d6a25ac1daa2adbf49f24c3944665f7785
3
+ metadata.gz: 6afe725cd74e87fa927f966c51613a7be9f519220e182a200010cb78176692a7
4
+ data.tar.gz: eec357ac38c904324ced0cab8fe2e71480a87388d67ab8ce2ff65cf3b2c4954d
5
5
  SHA512:
6
- metadata.gz: 1756b3182dbaf2557ce40abf8266acdb32bdfec4b6261f260c9abfc5695556a7bc03f9beddce6893f02c9c50619551418fa75953c745b8663aa00a9cad75545a
7
- data.tar.gz: a382bb28e40f6e9b3cfed71456cd1d24debd0c458ed6282a51b8247cba29671a8746f5f63d0b722881028cf12bdd366ee30d24e7f3442cc8444afb2f9d1f29d3
6
+ metadata.gz: 910462bc7cf06b0077cd993ecb19e8e0db778f6af3ccd8eacc5057d7296bde6546af70a305adc3773364661077b1b0f9a6d5950007b0bbd6157ba5ab40c3ed5a
7
+ data.tar.gz: c4507794f655b5aaaf2563680522794007d51fdb3538d170e931f0ac3c07337808fe88e440829ac58f7d99843b44b4c3f55a6c7c68b5119c1ff6221143271ce1
data/.version CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.3.0
data/lib/lib/base.rb CHANGED
@@ -1,18 +1,47 @@
1
1
  class Class
2
- def cattr name = nil, create_class_method = true
3
- if name && create_class_method == true
4
- define_singleton_method(name) do |arg = :_nil|
5
- if arg == :_nil
6
- CattrProxy.new(self).send(name)
7
- else
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
- if name
14
- CattrProxy.new(self).send('%s=' % name, nil)
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, &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.2
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: 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