class-cattr 0.2.1 → 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 +35 -14
  4. data/lib/lib/proxy.rb +13 -17
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7e36c2669386c70f837891e37cd035574309c08127c0d8982cdc94b4f0b7a400
4
- data.tar.gz: 6edda84721643fa97c763f4a41364134ba85be03d37885a977c3104a1cecbca2
3
+ metadata.gz: 6afe725cd74e87fa927f966c51613a7be9f519220e182a200010cb78176692a7
4
+ data.tar.gz: eec357ac38c904324ced0cab8fe2e71480a87388d67ab8ce2ff65cf3b2c4954d
5
5
  SHA512:
6
- metadata.gz: f0b0d0a2b8f31d1a82476adb734f7c704a926cd314f0b7dfd54da7528f386c7a193518f7a5beb9b4b1319be6dd054b4e7e90668f852a409caf44b368d384c210
7
- data.tar.gz: b8eb643415ad4a8d9872537910f917955b28ffc120c7719680895c122206e4dc17a89f36724dab674b3b679e704c1f37cdb89288be5b0865256338f129f8f085
6
+ metadata.gz: 910462bc7cf06b0077cd993ecb19e8e0db778f6af3ccd8eacc5057d7296bde6546af70a305adc3773364661077b1b0f9a6d5950007b0bbd6157ba5ab40c3ed5a
7
+ data.tar.gz: c4507794f655b5aaaf2563680522794007d51fdb3538d170e931f0ac3c07337808fe88e440829ac58f7d99843b44b4c3f55a6c7c68b5119c1ff6221143271ce1
data/.version CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.3.0
data/lib/lib/base.rb CHANGED
@@ -1,27 +1,48 @@
1
1
  class Class
2
- def cattr name = nil, value = :_nil, &block
3
- return CattrProxy.new(self) unless name
2
+ def cattr name = nil, opts = {}, &block
3
+ if name
4
+ # cattr :foo, class: true, instance: true, default: proc { bar }
4
5
 
5
- if value != :_nil || block
6
- define_singleton_method('%s=' % name) do |arg|
7
- CattrProxy.new(self).send('%s=' % name, arg)
6
+ # if block provided, set it as a default value
7
+ if block
8
+ opts[:default] = block
8
9
  end
9
10
 
10
- define_singleton_method(name) do |arg = :_nil|
11
- if arg == :_nil
12
- CattrProxy.new(self).send(name)
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
- if value != :_nil
19
- CattrProxy.new(self).send('%s=' % name, value)
20
- elsif block
21
- CattrProxy.new(self).send(name, value, &block)
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
- CattrProxy.new(self).send('%s=' % name, nil)
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, &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
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
- if local === Proc
24
- local = @host.instance_exec &local
25
- end
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
- return local
20
+ if local === Proc
21
+ local = @host.instance_exec &local
28
22
  end
29
- end
30
23
 
31
- raise ArgumentError, 'Cattr class variable "cattr.%s" not defined on "%s".' % [name.sub('@cattr_', ''), @host]
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.2.1
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-12 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