camille 0.5.4 → 0.5.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9ebcd5a9f0f1efd10108a596a8e4e442eb5afd3975728cd2ec51319563ae504e
4
- data.tar.gz: '0889d8c954ef2983e27f7b9d97d396db0edb46251b6b55febc1f1427e9c3391f'
3
+ metadata.gz: 991a6d473c51e10dce629da09a1b430ff316b448521bf8631b8994b358cdc809
4
+ data.tar.gz: 34398f5bf1e5e2d2793de422d43dfd9c57b4f1eed9933cb3e99db91778e15ef9
5
5
  SHA512:
6
- metadata.gz: 5247e53a9b76e8117247ef8d5e7c83d27cb57b3c8bf4971a5ee978e168e1e1e1c58a1348522ec7d2a4ab530d958d044c7c670a464e5cd5c02fadf046284e97c4
7
- data.tar.gz: b64ab07566a56d4e407ec887af105493b4af9dd99c0fa4c1e32de32fce6e54d9e78ca375ba59377f8cecd0002f27bfc6822de2635612345ff67ea67efa305466
6
+ metadata.gz: 772f197217bc1ffdd29b432b3c615c34f13f286e694d33abec58a468722d3217c1d8ffcd893aadab57b3f2be4b57fbe693c08e7976d72f9c7aeee33bea67e837
7
+ data.tar.gz: 679738f907ba017a94dc6e73b29ff82500765c207fb48a5da42c037e0ec9dbfb6155e4d26abc465b205271fbb1928ef29d677f1510c40f9a62dbb829e9e26d73
data/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.5.6
4
+
5
+ ### Added
6
+
7
+ * Added record type
8
+
9
+ ## 0.5.5
10
+
11
+ ### Added
12
+
13
+ * Added case conversion check for object keys.
14
+
3
15
  ## 0.5.4
4
16
 
5
17
  ### Added
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- camille (0.5.3)
4
+ camille (0.5.6)
5
5
  rails (>= 6.1, < 8)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -163,9 +163,11 @@ params(
163
163
  boolean_literal: false,
164
164
  # a custom type we defined above
165
165
  product: Product,
166
- # Pick and Omit accepts a type and an array of symbols
166
+ # Pick and Omit accept a type and an array of symbols
167
167
  pick: Pick[{a: 1, b: 2}, [:a, :b]],
168
- omit: Omit[Product, [:id]]
168
+ omit: Omit[Product, [:id]],
169
+ # Record accepts a key type and a value type
170
+ record: Record[Number, String]
169
171
  )
170
172
  ```
171
173
 
@@ -1,6 +1,7 @@
1
1
  module Camille
2
2
  module Types
3
3
  class Object < Camille::BasicType
4
+ class ArgumentError < ::ArgumentError; end
4
5
  attr_reader :fields
5
6
 
6
7
  def initialize fields
@@ -43,6 +44,7 @@ module Camille
43
44
  private
44
45
  def normalize_fields fields
45
46
  fields.map do |key, value|
47
+ check_case_conversion_safe key
46
48
  type = Camille::Type.instance(value)
47
49
  if key.end_with?('?')
48
50
  new_key = remove_question_mark(key)
@@ -57,6 +59,13 @@ module Camille
57
59
  def remove_question_mark sym
58
60
  sym.to_s.gsub(/\?$/, '').to_sym
59
61
  end
62
+
63
+ def check_case_conversion_safe sym
64
+ str = sym.to_s
65
+ if str != str.camelize.underscore
66
+ raise ArgumentError.new("Only keys satisfying `key.to_s == key.to_s.caamelize.underscore` can be used.")
67
+ end
68
+ end
60
69
  end
61
70
  end
62
71
  end
@@ -0,0 +1,58 @@
1
+ module Camille
2
+ module Types
3
+ class Record < Camille::BasicType
4
+ attr_reader :left, :right
5
+ attr_reader :key, :value
6
+
7
+ def initialize key, value
8
+ @key = Camille::Type.instance key
9
+ @value = Camille::Type.instance value
10
+ end
11
+
12
+ def transform_and_check value
13
+ if value.is_a? ::Hash
14
+
15
+ transform_and_check_results = value.map.with_index do |(k, v), index|
16
+ [index, transform_and_check_pair(k, v)]
17
+ end
18
+
19
+ errors = transform_and_check_results.map do |index, (error, transformed)|
20
+ ["record[#{index}]", error]
21
+ end.select{|x| x[1]}
22
+
23
+ if errors.empty?
24
+ transformed = transform_and_check_results.map{|_, (_, transformed)| transformed}.to_h
25
+ [nil, transformed]
26
+ else
27
+ [Camille::TypeError.new(**errors.to_h), nil]
28
+ end
29
+ else
30
+ [Camille::TypeError.new("Expected hash, got #{value.inspect}."), nil]
31
+ end
32
+ end
33
+
34
+ def self.[] key, value
35
+ self.new key, value
36
+ end
37
+
38
+ def literal
39
+ "Record<#{@key.literal}, #{@value.literal}>"
40
+ end
41
+
42
+ private
43
+ def transform_and_check_pair key, value
44
+ key_error, key_transformed = @key.transform_and_check key
45
+ value_error, value_transformed = @value.transform_and_check value
46
+
47
+ if key_error.nil? && value_error.nil?
48
+ [nil, [key_transformed, value_transformed]]
49
+ else
50
+ errors = [['record.key', key_error], ['record.value', value_error]].select{|x| x[1]}.to_h
51
+
52
+ error = Camille::TypeError.new(**errors)
53
+ [error, nil]
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Camille
4
- VERSION = "0.5.4"
4
+ VERSION = "0.5.6"
5
5
  end
data/lib/camille.rb CHANGED
@@ -21,6 +21,7 @@ require_relative "camille/types/boolean_literal"
21
21
  require_relative "camille/pick_and_omit"
22
22
  require_relative "camille/types/pick"
23
23
  require_relative "camille/types/omit"
24
+ require_relative "camille/types/record"
24
25
  require_relative "camille/type"
25
26
  require_relative "camille/type_error"
26
27
  require_relative "camille/type_error_printer"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: camille
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.4
4
+ version: 0.5.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - 辻彩
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-05-12 00:00:00.000000000 Z
11
+ date: 2023-05-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -84,6 +84,7 @@ files:
84
84
  - lib/camille/types/object.rb
85
85
  - lib/camille/types/omit.rb
86
86
  - lib/camille/types/pick.rb
87
+ - lib/camille/types/record.rb
87
88
  - lib/camille/types/string.rb
88
89
  - lib/camille/types/string_literal.rb
89
90
  - lib/camille/types/tuple.rb