camille 0.4.1 → 0.4.2

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: 2370ceb41fe8fdd701b99671f1d4d82678fc103218e96a10a52e60b9b2d1ad1f
4
- data.tar.gz: ef8fe63ad679a1744fdef8c58971148d3642be0be2c5e23ff53b510dfef2b449
3
+ metadata.gz: 1c6d8ec2d346e2ff02811be47d900bee304a82f5a96e213cbf5e1c9f96f3fe61
4
+ data.tar.gz: 1a67d813067334b228caa09c9cc10411fa4a3b56eab57610b415c129616a4533
5
5
  SHA512:
6
- metadata.gz: 6b8068692bd238a82cded511bad5e15d3f0993640a487b335cfd7d1592fe3c8cb45f88924753a1b580ae7448a59d58e423c29332842fa9be09472585bc810ea7
7
- data.tar.gz: 71bc242a3a3fc9224c0d2e7d3598d9e4be378ff7b2a028e02b55833927cfb516d6309c44bbc7b2296b0b9b780ffe07458435e40a89a1953d6044a465621e7ef9
6
+ metadata.gz: 0d977978049f5c5589731135c24c360145c3145d3ded0be2d158ea67546eece82d8af7c07674e1895d03a3071418adc9b1a6a597e5246b22b96d0c7abb2123d3
7
+ data.tar.gz: b4fd67e56fdd11db3c00d1e1ddee3a775c8e3d87ba2423cbe652729ed3dfb3460591d658a89bd1192385c45f0507fbeae5172a03acac65fcb3ad0880fe3b1aa7
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.4.2
4
+
5
+ ### Added
6
+
7
+ * Supported `transform` for custom types
8
+ * Added default DateTime and Decimal type to install generator
9
+
3
10
  ## 0.4.1
4
11
 
5
12
  ### Added
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- camille (0.4.1)
4
+ camille (0.4.2)
5
5
  rails (>= 6.1, < 8)
6
6
 
7
7
  GEM
@@ -11,6 +11,15 @@ module Camille
11
11
  Camille::Types::Array.new(self)
12
12
  end
13
13
 
14
+ def transform_and_check value
15
+ transformed = transform value
16
+ [check(value), transformed]
17
+ end
18
+
19
+ def transform value
20
+ value
21
+ end
22
+
14
23
  def self.| other
15
24
  Camille::Type.instance(self) | other
16
25
  end
@@ -15,16 +15,16 @@ module Camille
15
15
  if endpoint = camille_endpoint
16
16
  render_options = args.last
17
17
  if value = render_options[:json]
18
- error = endpoint.response_type.check(value)
18
+ error, transformed = endpoint.response_type.transform_and_check(value)
19
19
  if error
20
20
  string_io = StringIO.new
21
21
  Camille::TypeErrorPrinter.new(error).print(string_io)
22
22
  raise TypeError.new("\nType check failed for response.\n#{string_io.string}")
23
23
  else
24
- if value.is_a? Hash
25
- value.deep_transform_keys!{|k| k.to_s.camelize(:lower)}
24
+ if transformed.is_a? Hash
25
+ transformed.deep_transform_keys!{|k| k.to_s.camelize(:lower)}
26
26
  end
27
- super(json: value)
27
+ super(json: transformed)
28
28
  end
29
29
  else
30
30
  raise ArgumentError.new("Expected key :json for `render` call.")
@@ -0,0 +1,31 @@
1
+ require 'date'
2
+
3
+ # This type is only for testing, to simulate the usage of transform.
4
+ module Camille
5
+ module CustomTypes
6
+ class Date < Camille::Type
7
+ include Camille::Types
8
+
9
+ alias_of(
10
+ type: 'Date',
11
+ value: Number
12
+ )
13
+
14
+ def transform value
15
+ case value
16
+ when ::Date
17
+ timestamp = value.to_time.to_i
18
+ when ::Time
19
+ timestamp = value.to_i
20
+ end
21
+ {
22
+ type: 'Date',
23
+ value: timestamp.to_i * 1000
24
+ }
25
+ end
26
+
27
+ end
28
+ end
29
+ end
30
+
31
+ Camille::Loader.loaded_types.delete(Camille::CustomTypes::Date)
@@ -10,8 +10,9 @@ module Camille
10
10
  copy_file "configuration.rb", "config/camille/configuration.rb"
11
11
  end
12
12
 
13
- def create_types_folder
14
- copy_file ".keep", "config/camille/types/.keep"
13
+ def create_date_time_and_decimal
14
+ copy_file "date_time.rb", "config/camille/types/date_time.rb"
15
+ copy_file "decimal.rb", "config/camille/types/decimal.rb"
15
16
  end
16
17
 
17
18
  def create_schemas_folder
@@ -0,0 +1,12 @@
1
+ using Camille::Syntax
2
+
3
+ class Camille::Types::DateTime < Camille::Type
4
+ include Camille::Types
5
+
6
+ alias_of(String)
7
+
8
+ # transforms a DateTime into a String of ISO 8601 format
9
+ def transform value
10
+ value.as_json
11
+ end
12
+ end
@@ -0,0 +1,16 @@
1
+ using Camille::Syntax
2
+
3
+ class Camille::Types::Decimal < Camille::Type
4
+ include Camille::Types
5
+
6
+ alias_of(Number)
7
+
8
+ # transforms a BigDecimal into a Float so it fits in Number type
9
+ def transform value
10
+ if value.is_a? BigDecimal
11
+ value.to_f
12
+ else
13
+ value
14
+ end
15
+ end
16
+ end
data/lib/camille/type.rb CHANGED
@@ -21,6 +21,11 @@ module Camille
21
21
  @underlying.check value
22
22
  end
23
23
 
24
+ def transform_and_check value
25
+ transformed = transform value
26
+ @underlying.transform_and_check transformed
27
+ end
28
+
24
29
  def self.klass_name
25
30
  name.gsub(/^Camille::Types::/, '')
26
31
  end
@@ -18,6 +18,11 @@ module Camille
18
18
  end
19
19
  end
20
20
 
21
+ def transform_and_check value
22
+ transformed = value.map{|x| @content.transform x}
23
+ [check(transformed), transformed]
24
+ end
25
+
21
26
  def literal
22
27
  "#{@content.literal}[]"
23
28
  end
@@ -21,6 +21,29 @@ module Camille
21
21
  end
22
22
  end
23
23
 
24
+ def transform_and_check value
25
+ if value.is_a? Hash
26
+ transform_and_check_results = @fields.map do |key, type|
27
+ [key, type.transform_and_check(value[key])]
28
+ end
29
+
30
+ errors = transform_and_check_results.map do |key, (error, transformed)|
31
+ [key.to_s, error]
32
+ end.select{|x| x[1]}
33
+
34
+ if errors.empty?
35
+ transformed = transform_and_check_results.map do |key, (error, transformed)|
36
+ [key, transformed]
37
+ end.to_h
38
+ [nil, transformed]
39
+ else
40
+ Camille::TypeError.new(**errors.to_h)
41
+ end
42
+ else
43
+ Camille::TypeError.new("Expected hash, got #{value.inspect}.")
44
+ end
45
+ end
46
+
24
47
  def literal
25
48
  "{#{@fields.map{|k,v| "#{ActiveSupport::Inflector.camelize k.to_s, false}: #{v.literal}"}.join(', ')}}"
26
49
  end
@@ -5,6 +5,10 @@ module Camille
5
5
  processed_object.check(value)
6
6
  end
7
7
 
8
+ def transform_and_check value
9
+ processed_object.transform_and_check(value)
10
+ end
11
+
8
12
  private
9
13
  def klass_name
10
14
  'Omit'
@@ -5,6 +5,10 @@ module Camille
5
5
  processed_object.check(value)
6
6
  end
7
7
 
8
+ def transform_and_check value
9
+ processed_object.transform_and_check(value)
10
+ end
11
+
8
12
  private
9
13
  def klass_name
10
14
  'Pick'
@@ -21,6 +21,26 @@ module Camille
21
21
  end
22
22
  end
23
23
 
24
+ def transform_and_check value
25
+ left_transformed = @left.transform value
26
+ left_result = @left.check left_transformed
27
+ if left_result
28
+ right_transformed = @right.transform value
29
+ right_result = @right.check right_transformed
30
+ if right_result
31
+ error = Camille::TypeError.new(
32
+ 'union.left' => left_result,
33
+ 'union.right' => right_result
34
+ )
35
+ [error, nil]
36
+ else
37
+ [nil, right_transformed]
38
+ end
39
+ else
40
+ [nil, left_transformed]
41
+ end
42
+ end
43
+
24
44
  def literal
25
45
  "#{@left.literal} | #{@right.literal}"
26
46
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Camille
4
- VERSION = "0.4.1"
4
+ VERSION = "0.4.2"
5
5
  end
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.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - 辻彩
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-03-17 00:00:00.000000000 Z
11
+ date: 2023-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -52,11 +52,14 @@ files:
52
52
  - lib/camille/code_generator.rb
53
53
  - lib/camille/configuration.rb
54
54
  - lib/camille/controller_extension.rb
55
+ - lib/camille/custom_types/date.rb
55
56
  - lib/camille/endpoint.rb
56
57
  - lib/camille/generators/install_generator.rb
57
58
  - lib/camille/generators/schema_generator.rb
58
59
  - lib/camille/generators/templates/.keep
59
60
  - lib/camille/generators/templates/configuration.rb
61
+ - lib/camille/generators/templates/date_time.rb
62
+ - lib/camille/generators/templates/decimal.rb
60
63
  - lib/camille/generators/templates/schema_template.erb
61
64
  - lib/camille/generators/templates/type_template.erb
62
65
  - lib/camille/generators/type_generator.rb