lowtype 1.7.0 → 1.7.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: 1f10f8ba754648f8989ade0f8341a64c198345005d7563ea5b09604e63d5546b
4
- data.tar.gz: c48642e88ca4616eef730a2929a756a06f0fe708dc636571ff07ad32ccf563a9
3
+ metadata.gz: 01eb02dec83d1e5199bd291076f40826123d1bfbc508a26ebc5edee36f8ce29f
4
+ data.tar.gz: 3d56ec387c38fcc6c6cd25141c17ac65d83817b3fe198ae1ba7843ab35d66a4e
5
5
  SHA512:
6
- metadata.gz: b4057c034994ed7c4d817d44a6e750e96273d01e47bbf5f42453d1452b368e034c02c26f46bd2adde8c14c06362cb4b8e288bd0115eaa936213b2c7c4f2cbf5a
7
- data.tar.gz: 42bd196d005c75e0e44977e5149d288a57e32ae8286acde5fcb5bdbf752747779e87c0b89aec3f6b328662eb0287e7ad9c07c66051fa21cde10703ed429df591
6
+ metadata.gz: 80f0cab236b0a0a6d96d4ad5dfb2fc01ce217b3be969761bd301af20d54bbaad18826b157d93fe1f3acf006deb1dd2ee224af3daf23ed4cdac903db28f72db70
7
+ data.tar.gz: 84ce516c59acfd11dd78f45e5cf4d5f5f40b3b9ae92fc00af0411aa37697af56c407244de73094f1404ea5414077ac65b90a14947da1d167abff526fa4fb0174
@@ -107,7 +107,7 @@ module Low
107
107
  # Class methods are evaluated on the singleton class but "def self.method" would define a method on the
108
108
  # singleton class's own singleton class. So methods with a receiver are evaluated in the class instead.
109
109
  target = klass.singleton_class? && method_proxy.receiver? ? klass.attached_object : klass
110
- target.class_eval(method_proxy.export, method_proxy.file_path, method_proxy.start_line)
110
+ evaluate_nested(target:, source: method_proxy.export, file_path: method_proxy.file_path, line: method_proxy.start_line)
111
111
 
112
112
  if class_proxy.private_start_line && method_proxy.start_line > class_proxy.private_start_line
113
113
  klass.send(:private, method_proxy.name)
@@ -116,6 +116,32 @@ module Low
116
116
 
117
117
  nil
118
118
  end
119
+
120
+ # A method evaluated with "class_eval" finds constants in its class but not in the modules the class is nested in,
121
+ # like "TrieNode" from "Rain::TrieNode", which Ruby finds for the method as written. So the method is evaluated
122
+ # inside each module and class of the target's name. The method still reports its own file and line.
123
+ def evaluate_nested(target:, source:, file_path:, line:)
124
+ namespaces = nesting(target:)
125
+ return target.class_eval(source, file_path, line) unless namespaces
126
+
127
+ opening = namespaces.map { |namespace| "#{namespace.is_a?(Class) ? 'class' : 'module'} #{namespace.name.split('::').last};" }
128
+ closing = ' end;' * namespaces.size
129
+ TOPLEVEL_BINDING.eval("#{opening.join(' ')} #{source}\n#{closing}", file_path, line)
130
+ end
131
+
132
+ # Each module and class in the target's name, like [Rain, Rain::TrieNode], or nil for an anonymous class.
133
+ def nesting(target:)
134
+ name = target.name
135
+ return nil if name.nil? || target.singleton_class?
136
+
137
+ parts = name.split('::')
138
+ namespaces = parts.each_index.map { |index| Object.const_get(parts[0..index].join('::')) }
139
+ return nil unless namespaces.last.equal?(target)
140
+
141
+ namespaces
142
+ rescue NameError
143
+ nil
144
+ end
119
145
  end
120
146
  end
121
147
  end
@@ -55,6 +55,8 @@ module Low
55
55
 
56
56
  def define_type_reader(name:, expression:, location:)
57
57
  expression = register(name:, expression:, location:)
58
+ return define_untyped_reader(name:, expression:) unless LowType.config.type_checking
59
+
58
60
  proxy = ::Lowkey::ReturnProxy.new(name:, source: accessor_source(name:, location:), expression:)
59
61
 
60
62
  define_method(name) do
@@ -68,6 +70,8 @@ module Low
68
70
 
69
71
  def define_type_writer(name:, expression:, location:)
70
72
  expression = register(name:, expression:, location:)
73
+ return attr_writer(name) unless LowType.config.type_checking
74
+
71
75
  proxy = ::Lowkey::ParamProxy.new(name:, source: accessor_source(name:, location:), type: :pos_req, position: nil, expression:)
72
76
 
73
77
  define_method("#{name}=") do |value|
@@ -76,6 +80,17 @@ module Low
76
80
  end
77
81
  end
78
82
 
83
+ # Without type checking a reader only returns its default value when nil, and one without a default is attr_reader.
84
+ def define_untyped_reader(name:, expression:)
85
+ return attr_reader(name) if expression.required?
86
+
87
+ define_method(name) do
88
+ value = instance_variable_get("@#{name}")
89
+ value = TypeAccessors.default_value(expression:) if value.nil?
90
+ value.is_a?(ValueExpression) ? value.value : value
91
+ end
92
+ end
93
+
79
94
  def register(name:, expression:, location:)
80
95
  expression = cast_type_expression(expression)
81
96
  expressions = TypeAccessors.expressions[[location.path, location.lineno]] ||= {}
data/lib/version.rb CHANGED
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Low
4
4
  module Type
5
- VERSION = '1.7.0'
5
+ VERSION = '1.7.1'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lowtype
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.0
4
+ version: 1.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - maedi
@@ -131,7 +131,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
131
131
  - !ruby/object:Gem::Version
132
132
  version: '0'
133
133
  requirements: []
134
- rubygems_version: 4.0.6
134
+ rubygems_version: 4.0.20
135
135
  specification_version: 4
136
136
  summary: Elegant types in Ruby
137
137
  test_files: []