lab42_checked_class 0.2.0 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ff1daba1d7c01bd5f4af095171c104f71fdaa6d180659ebdf6fdbb86a6f9c6e9
4
- data.tar.gz: ac04957601c51e0a505dfb2621a5f44ff3664aeb09a0e00d412fba45de71bc33
3
+ metadata.gz: 010c1b5675022a9214b591ff4a4516928d5a55019c2203fadf75b6206a3fa744
4
+ data.tar.gz: b9b6e7396a49f3fe5b2e29734623108112556095a8b13d3086b42e94d987166c
5
5
  SHA512:
6
- metadata.gz: cae9380f7d40546c63af28f0abdb59bca958298208cc9b9ec39b1d0ca93acbdfd3eeec642af53e9eeceae9c75259abe79ce7b313dd6be33e6a4d52da7c94755c
7
- data.tar.gz: a57c32b8094f45501ca71c7bbcf77fa3880732ecadcff5b56ce44a50df824b458cdc6ba4a391c2f12ce58dc32416776b833e0d9ca1f6ab1b159716bf17aead7d
6
+ metadata.gz: c128e03d74d01e824ba153e5d21a2dee18035c0953b1f4ccea9f346fdcbe0f57bf83ec7815c7e83559e72e2c0174ce4b0bcdb0a5d1d0b0da1e813beacb61eece
7
+ data.tar.gz: 8e4ddb87d4b4dfa542c76fb5c8a91203fdd6bb99b598d67575692dd2003d0e96007d8ae0063f275a97518b587d703a954142530cc8c76c9deb234ae0e558cf16
data/README.md CHANGED
@@ -69,11 +69,11 @@ Then we will get them loaded
69
69
  ```ruby
70
70
  expected_output = <<~EOT
71
71
  #{ENV["PWD"]}/lib/lab42/checked_class/all.rb:5: warning: already initialized constant CheckedClass
72
- #{ENV["PWD"]}/spec/spec_helper.rb:36: warning: previous definition of CheckedClass was here
72
+ #{ENV["PWD"]}/spec/spec_helper.rb:38: warning: previous definition of CheckedClass was here
73
73
  #{ENV["PWD"]}/lib/lab42/checked_class/all.rb:6: warning: already initialized constant Constraint
74
- #{ENV["PWD"]}/spec/spec_helper.rb:37: warning: previous definition of Constraint was here
74
+ #{ENV["PWD"]}/spec/spec_helper.rb:39: warning: previous definition of Constraint was here
75
75
  #{ENV["PWD"]}/lib/lab42/checked_class/all.rb:7: warning: already initialized constant ConstraintError
76
- #{ENV["PWD"]}/spec/spec_helper.rb:38: warning: previous definition of ConstraintError was here
76
+ #{ENV["PWD"]}/spec/spec_helper.rb:40: warning: previous definition of ConstraintError was here
77
77
  EOT
78
78
  expect {
79
79
  require 'lab42/checked_class/all'
@@ -89,10 +89,10 @@ Given a checked class
89
89
  attr :a # No constraints, no defaults
90
90
  attr b: 42 # No constraints, but a default
91
91
  attr :c, Integer # Constraint, no default
92
- attr(d: 1) { it > 0 } # Constraint and default
92
+ attr(d: 1) { it > 0 } # Default and block constraint
93
93
  attr(:e, :even?) # Symbolic constraint, no default
94
94
  attr(:f) { (it % 10).zero? } # Block constraint, no default
95
- attr(g: 2) { it > 1 } # Block Constraint and default
95
+ attr(:g, [:>, 1], default: 2) # Default and functional constraint
96
96
 
97
97
  # Named constraint
98
98
  constrain("a + b > e") { it.a + it.b > it.e }
@@ -7,7 +7,7 @@ module Lab42
7
7
 
8
8
 
9
9
  def check(subject)
10
- subject = @attr ? subject[@attr] : subject
10
+ subject = @attr ? subject[@attr] : subject
11
11
  return nil if @constraint.(subject)
12
12
 
13
13
  @name
@@ -28,18 +28,14 @@ module Lab42
28
28
 
29
29
  def _mk_constraint(constraint, name, &blk)
30
30
  case constraint
31
- in Symbol
32
- constraint.to_proc
33
- in [Symbol, *] => args
34
- -> { it.send(*args) }
35
- in Module
36
- -> { constraint === it }
31
+ in Proc
32
+ constraint
37
33
  in [Predefined, blk, name]
38
34
  @name = [name, @name].join(' ')
39
35
  blk
40
36
  in nil
41
37
  blk
42
- in _
38
+ else
43
39
  raise ArgumentError, "Type Error for constraint of attr #{name.inspect}; must be a Module, Symbol, or an array starting with a Symbol, but was #{constraint.inspect}"
44
40
  end
45
41
  end
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'ex_aequo/base/fn'
4
+ require 'ex_aequo/base/to_proc'
5
+
3
6
  require_relative 'constraint'
4
7
  require_relative 'constraint_checker'
5
8
  require_relative 'injector'
@@ -7,6 +10,7 @@ require_relative 'injector'
7
10
  module Lab42
8
11
  module CheckedClass
9
12
  class Proxy
13
+ Fn = ExAequo::Base::Fn
10
14
  None = Object.new.freeze
11
15
 
12
16
  attr_reader :klass
@@ -26,22 +30,27 @@ module Lab42
26
30
  @klass = klass
27
31
  end
28
32
 
29
- def attr(att, *args, &blk)
30
- if Hash === att
31
- raise ArgumentError, "attr definition with default does not support symbolic constraints" unless args.empty?
32
- raise ArgumentError, "only one attribute can be defined with defaults, not #{att.keys.inspect}" if att.size != 1
33
-
34
- return _define_att(att.keys.first, default: att.values.first, &blk)
35
- end
36
-
37
- raise ArgumentError, "attributes must be symbols, not: #{att.inspect}" unless Symbol === att
38
-
39
- if args.empty?
40
- return _define_att(att, &blk)
33
+ def attr(*args, &block)
34
+ case [block, *args]
35
+ in [constraint, Symbol => name]
36
+ _def_undefaulted_attr(name:, constraint:)
37
+ in [nil, Symbol => name, fun]
38
+ constraint = _make_fun_or_constraint(fun)
39
+ _def_undefaulted_attr(name:, constraint:)
40
+ in [constraint, Symbol => name, {default:, **nil}]
41
+ _def_defaulted_attr(name:, constraint:, default:)
42
+ in [default, Symbol => name, fun]
43
+ constraint = _make_fun_or_constraint(fun)
44
+ _def_defaulted_attr(name:, default: default.(), constraint:)
45
+ in [constraint, Hash => arg]
46
+ _def_defaulted_attr(name: arg.keys.first, default: arg.values.first, constraint:)
47
+ in [nil, Symbol => name, fun, {default:, **nil}]
48
+ constraint = _make_fun_or_constraint(fun)
49
+ _def_defaulted_attr(name:, default:, constraint:)
50
+ in [_, Symbol => name, _, {default:, **nil}]
51
+ raise ArgumentError, "functional constraint default kwd and block"
41
52
  else
42
- raise ArgumentError, "must not define symbolic and functional constraint" if @blk
43
-
44
- _define_att(att, args.first)
53
+ raise ArgumentError, "too many arguments"
45
54
  end
46
55
  end
47
56
 
@@ -70,12 +79,24 @@ module Lab42
70
79
  attr(name, *a, **k, &b)
71
80
  end
72
81
 
73
- def _define_att(name, constraint = nil, default: None, &blk)
82
+ def _def_defaulted_attr(name:, constraint:, default:)
74
83
  _attributes << name
84
+ _defaults[name] = default
85
+ _constraints[name] = Constraint.new(attr: name, constraint:) if constraint
86
+ end
87
+
88
+ def _def_undefaulted_attr(name:, constraint:)
89
+ _attributes << name
90
+ _constraints[name] = Constraint.new(attr: name, constraint:) if constraint
91
+ end
75
92
 
76
- # allow nil default to avoid implicit nil defaults
77
- _defaults[name] = default unless default == None
78
- _constraints[name] = Constraint.new(attr: name, constraint:, &blk) if constraint || blk
93
+ def _make_fun_or_constraint(fun)
94
+ case fun
95
+ in [c, *] if Constraint == c
96
+ fun
97
+ else
98
+ Fn.mk_fun(fun)
99
+ end
79
100
  end
80
101
 
81
102
  def _attributes
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
  module Lab42
3
3
  module CheckedClass
4
- VERSION = '0.2.0'
4
+ VERSION = '0.2.1'
5
5
  end
6
6
  end
7
7
  # SPDX-License-Identifier: AGPL-3.0-or-later
metadata CHANGED
@@ -1,14 +1,28 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lab42_checked_class
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Dober
8
8
  bindir: bin
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
- dependencies: []
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: ex_aequo_base
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: 0.1.17
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: 0.1.17
12
26
  description: 'Mutable classes with constraints and lots of boilerplate code removal.
13
27
 
14
28
  '
@@ -42,14 +56,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
42
56
  requirements:
43
57
  - - ">="
44
58
  - !ruby/object:Gem::Version
45
- version: 3.4.1
59
+ version: 3.4.5
46
60
  required_rubygems_version: !ruby/object:Gem::Requirement
47
61
  requirements:
48
62
  - - ">="
49
63
  - !ruby/object:Gem::Version
50
64
  version: '0'
51
65
  requirements: []
52
- rubygems_version: 3.6.8
66
+ rubygems_version: 3.7.1
53
67
  specification_version: 4
54
68
  summary: Mutable classes with constraints and lots of boilerplate code removal.
55
69
  test_files: []