modulation 0.21 → 0.22

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: 1d5bf725c036d3db2d9604cd161f6742c669c392ed09bdeb315a39fd343d4887
4
- data.tar.gz: 655000532ad754fd550b913a38cc92adc790761d5b98b6b64a91ad9d1ec61664
3
+ metadata.gz: 2c3934a1afb8521b3ec5868ea4b7d1b80448b77c928ac00c6959a20a9d992c90
4
+ data.tar.gz: 1ff3b5b77a1add4bd03225557106127f154de4ec17d4f94cbe15dba29d9ef701
5
5
  SHA512:
6
- metadata.gz: f50f8030beb0e9f009735763c183623f20869ff78f10ac592b2ce1d5de11f1168d8f64576801d117e7337f1cdc8d80845db70ede5f090480437f06a42d328e54
7
- data.tar.gz: 437c524656fc62ee887ddf69e2a011b5984cd796bfeb0568afdd31296392930525a3752f200d2175deec41b6bbeceef308f0a8bd50609a8ef562f118e2296e1f
6
+ metadata.gz: ba5b255f4a4d704f9a24aa40eea62bd8d78ea10a822928dd3d7e134cc21b28ff370f1a58be8f0f19d8d22c9cd897db36757f669c9d12393f6bf6774bc2e5c6e9
7
+ data.tar.gz: a3063a6bd099b11e5cfe00ef120e868d72cc348e2426064add023c5c0e67dbdd7f12183f6568ba0714f2088e8b3c5415ea6cfb9daec7e461066add9cb637e13e
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ 0.22 2019-05-15
2
+ ---------------
3
+
4
+ * Export_default of a method now exportsa proc calling that method
5
+ * Raise error on export of undefined symbols
6
+
1
7
  0.21 2019-02-19
2
8
  ---------------
3
9
 
@@ -53,7 +53,7 @@ module Modulation
53
53
  mod.__module_info[:exported_symbols] = symbols
54
54
  singleton = mod.singleton_class
55
55
 
56
- privatize_non_exported_methods(singleton, symbols)
56
+ privatize_non_exported_methods(mod, singleton, symbols)
57
57
  expose_exported_constants(mod, singleton, symbols)
58
58
  end
59
59
 
@@ -61,7 +61,13 @@ module Modulation
61
61
  # @param singleton [Class] sinleton for module
62
62
  # @param symbols [Array] array of exported symbols
63
63
  # @return [void]
64
- def privatize_non_exported_methods(singleton, symbols)
64
+ def privatize_non_exported_methods(mod, singleton, symbols)
65
+ defined_methods = singleton.instance_methods(true)
66
+ difference = symbols.select { |s| s=~ /^[a-z]/} - defined_methods
67
+ unless difference.empty?
68
+ raise_exported_symbol_not_found_error(difference.first, mod, :method)
69
+ end
70
+
65
71
  singleton.instance_methods(false).each do |sym|
66
72
  next if symbols.include?(sym)
67
73
  singleton.send(:private, sym)
@@ -74,8 +80,14 @@ module Modulation
74
80
  # @param symbols [Array] array of exported symbols
75
81
  # @return [void]
76
82
  def expose_exported_constants(mod, singleton, symbols)
83
+ defined_constants = singleton.constants(false)
84
+ difference = symbols.select { |s| s=~ /^[A-Z]/} - defined_constants
85
+ unless difference.empty?
86
+ raise_exported_symbol_not_found_error(difference.first, mod, :const)
87
+ end
88
+
77
89
  private_constants = mod.__module_info[:private_constants] = []
78
- singleton.constants(false).each do |sym|
90
+ defined_constants.each do |sym|
79
91
  if symbols.include?(sym)
80
92
  mod.const_set(sym, singleton.const_get(sym))
81
93
  else
@@ -84,6 +96,16 @@ module Modulation
84
96
  end
85
97
  end
86
98
 
99
+ NOT_FOUND_MSG = "%s %s not found in module"
100
+
101
+ def raise_exported_symbol_not_found_error(sym, mod, kind)
102
+ error = NameError.new(NOT_FOUND_MSG % [
103
+ kind == :method ? 'Method' : 'Constant',
104
+ sym
105
+ ])
106
+ Modulation.raise_error(error, mod.__export_backtrace)
107
+ end
108
+
87
109
  # Returns exported value for a default export
88
110
  # If the given value is a symbol, returns the value of the corresponding
89
111
  # constant.
@@ -91,8 +113,20 @@ module Modulation
91
113
  # @param mod [Module] module
92
114
  # @return [any] exported value
93
115
  def transform_export_default_value(value, mod)
94
- value.is_a?(Symbol) ? mod.singleton_class.const_get(value) : value
95
- rescue NameError
116
+ if value.is_a?(Symbol)
117
+ case value
118
+ when /^[A-Z]/
119
+ unless mod.singleton_class.constants(true).include?(Symbol)
120
+ raise_exported_symbol_not_found_error(value, mod, :const)
121
+ end
122
+ else
123
+ if mod.singleton_class.instance_methods(true).include?(value)
124
+ return proc { |*args, &block| mod.send(value, *args, &block) }
125
+ else
126
+ raise_exported_symbol_not_found_error(value, mod, :method)
127
+ end
128
+ end
129
+ end
96
130
  value
97
131
  end
98
132
 
@@ -123,11 +123,12 @@ module Modulation
123
123
  # @param error [Error] raised error
124
124
  # @param caller [Array] error backtrace
125
125
  # @return [void]
126
- def raise_error(error, caller = error.backtrace)
127
- if @full_backtrace
128
- error.set_backtrace(caller)
129
- else
130
- error.set_backtrace(caller.reject { |l| l =~ /^#{Modulation::DIR}/ })
126
+ def raise_error(error, backtrace = nil)
127
+ if backtrace
128
+ unless @full_backtrace
129
+ backtrace = backtrace.reject { |l| l =~ /^#{Modulation::DIR}/ }
130
+ end
131
+ error.set_backtrace(backtrace)
131
132
  end
132
133
  raise error
133
134
  end
@@ -11,7 +11,8 @@ module Modulation
11
11
  # @return [void]
12
12
  def export(*symbols)
13
13
  symbols = symbols.first if symbols.first.is_a?(Array)
14
- __exported_symbols.concat(symbols)
14
+ self.__exported_symbols.concat(symbols)
15
+ self.__export_backtrace = caller
15
16
  end
16
17
 
17
18
  # Sets a module's value, so when imported it will represent the given value,
@@ -19,6 +20,7 @@ module Modulation
19
20
  # @param value [Symbol, any] symbol or value
20
21
  # @return [void]
21
22
  def export_default(value)
23
+ self.__export_backtrace = caller
22
24
  @__export_default_block&.call(value: value, caller: caller)
23
25
  end
24
26
 
@@ -74,6 +76,14 @@ module Modulation
74
76
  @__exported_symbols ||= []
75
77
  end
76
78
 
79
+ def __export_backtrace
80
+ @__export_backtrace
81
+ end
82
+
83
+ def __export_backtrace=(o)
84
+ @__export_backtrace = o
85
+ end
86
+
77
87
  # Allow modules to use attr_accessor/reader/writer and include methods by
78
88
  # forwarding calls to singleton_class
79
89
  %i[attr_accessor attr_reader attr_writer include].each do |sym|
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Modulation
4
- VERSION = '0.21'
4
+ VERSION = '0.22'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: modulation
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.21'
4
+ version: '0.22'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sharon Rosner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-20 00:00:00.000000000 Z
11
+ date: 2019-05-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest