low_type 0.8.8 → 0.8.9

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: b542b5e282a1c49db5ff50843ca16eae7ad0a6f50c143dd2568b4eed77b951ce
4
- data.tar.gz: deeb4c06c3e78041eefd69ddb0ab16c5317c2b4ec28306092cbd0a608d6b862b
3
+ metadata.gz: 65c0e662bfe788a1132c9ecf802392285089139cc2d721427a7285d62b87e606
4
+ data.tar.gz: da9f514df1428147990f55e7bc68a343dff87ce3df86d2e7913414ac97c80e15
5
5
  SHA512:
6
- metadata.gz: 7518519f80e3ba2edc2cd114a6fef9676e3866ac6c5032f983867cd8ab70a62de7e20113ed076c223e027c517654bd3538187eebd88a11964f46d8bdced623f0
7
- data.tar.gz: d6fee3d4fd5a7135e91eeafe090ed844fbde9bfb5b495e1022352910544f76d0c5074d2c7cb04657d18d421b50977b0194cd7a6f546bb4dbec37c8483023c849
6
+ metadata.gz: 323ed1e9adfe8a07e0e8b6e2ed6fba157caac080611827f32453816db5b0c40cb7631e99c9a004391e1565f05d4ad26f8e1a9ed7b710a13846bf37da1b4290e6
7
+ data.tar.gz: 7b85c07ee7cfe34c2c004a4c62381627d8f258236d891295a073dd9e4c2b304c35ccba6ef6521faf9e1da52558a8610862fb73ca76e184cbf4c19ea362fcc730
data/lib/redefiner.rb CHANGED
@@ -7,8 +7,6 @@ require_relative 'type_expression'
7
7
 
8
8
  module LowType
9
9
  class Redefiner
10
- FILE_PATH = File.expand_path(__FILE__)
11
-
12
10
  class << self
13
11
  def redefine_methods(method_nodes:, klass:, private_start_line:, file_path:)
14
12
  Module.new do
@@ -1,13 +1,17 @@
1
1
  require_relative 'proxies/param_proxy'
2
2
 
3
3
  module LowType
4
- class TypeExpression
5
- FILE_PATH = File.expand_path(__FILE__)
4
+ root_path = File.expand_path(__dir__)
5
+ file_path = File.expand_path(__FILE__)
6
+ adapter_paths = Dir.chdir(root_path) { Dir.glob('adapters/*') }.map { |path| File.join(root_path, path) }
7
+
8
+ HIDDEN_PATHS = [file_path, *adapter_paths, File.join(root_path, 'redefiner.rb')]
6
9
 
10
+ class TypeExpression
7
11
  attr_reader :types, :default_value
8
12
 
13
+ # @param type - A literal type or an instance representation of a typed structure.
9
14
  def initialize(type: nil, default_value: :LOW_TYPE_UNDEFINED)
10
- # Types can be instance representations of a structure.
11
15
  @types = []
12
16
  @types << type unless type.nil?
13
17
  @default_value = default_value
@@ -30,26 +34,6 @@ module LowType
30
34
  @default_value == :LOW_TYPE_UNDEFINED
31
35
  end
32
36
 
33
- # Called in situations where we want to be inclusive rather than exclusive and pass validation if one of the types is valid.
34
- def validate(value:, proxy:)
35
- if value.nil?
36
- return true if @default_value.nil?
37
- return false if required?
38
- end
39
-
40
- @types.each do |type|
41
- return true if LowType.type?(type) && type <= value.class # Example: HTML is a subclass of String and should pass as a String.
42
- return true if ::Array === type && ::Array === value && array_types_match_values?(types: type, values: value)
43
-
44
- # TODO: Shallow validation of enumerables could be made deeper with user config.
45
- if type.class == ::Hash && value.class == ::Hash && type.keys[0] == value.keys[0].class && type.values[0] == value.values[0].class
46
- return true
47
- end
48
- end
49
-
50
- false
51
- end
52
-
53
37
  def validate!(value:, proxy:)
54
38
  if value.nil?
55
39
  return true if @default_value.nil?
@@ -68,8 +52,7 @@ module LowType
68
52
 
69
53
  raise proxy.error_type, proxy.error_message(value:)
70
54
  rescue proxy.error_type => e
71
- file_paths = [FILE_PATH, LowType::Redefiner::FILE_PATH]
72
- raise proxy.error_type, e.message, backtrace_with_proxy(file_paths:, backtrace: e.backtrace, proxy:)
55
+ raise proxy.error_type, e.message, backtrace_with_proxy(backtrace: e.backtrace, proxy:)
73
56
  end
74
57
 
75
58
  def valid_types
@@ -95,9 +78,9 @@ module LowType
95
78
  true
96
79
  end
97
80
 
98
- def backtrace_with_proxy(file_paths:, proxy:, backtrace:)
99
- # Remove LowType file paths from the backtrace.
100
- filtered_backtrace = backtrace.reject { |line| file_paths.find { |file_path| line.include?(file_path) } }
81
+ def backtrace_with_proxy(proxy:, backtrace:)
82
+ # Remove LowType defined method file paths from the backtrace.
83
+ filtered_backtrace = backtrace.reject { |line| HIDDEN_PATHS.find { |file_path| line.include?(file_path) } }
101
84
 
102
85
  # Add the proxied file to the backtrace.
103
86
  proxy_file_backtrace = "#{proxy.file.path}:#{proxy.file.line}:in '#{proxy.file.scope}'"
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LowType
4
- VERSION = '0.8.8'
4
+ VERSION = '0.8.9'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: low_type
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.8
4
+ version: 0.8.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - maedi