constant_resolver 0.1.5 → 0.2.0

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: 3c05074565a7f3112d32cf0a4968ea4193fbce4eafc41e4873279e3a1a9787f2
4
- data.tar.gz: 4eab3c4e64102b05497709501bb1bab4b718be511b7ccfc31d2c7c919f86b131
3
+ metadata.gz: dbe8153c22d706203ca7a89f53e39748657e75c9c508ebb7614cf9d933970dfa
4
+ data.tar.gz: 77fb1450efa1027547b6e8a24c316d4624454a0b10bd3b636646e1e6eb6d8612
5
5
  SHA512:
6
- metadata.gz: b3feb2e2646e09c6fc840dd406e36341fe5c3fac6d0ecd6f1dcf8e2e7aa2cd64d9cf659e3eb832b5b8240191bdf02ae1f5d9355362e8c36f3c684a314463d9d9
7
- data.tar.gz: 16922c192716b2ddb615cdc43503a27b878d4bc798ad7869ad0fa319a442f64df01da1510fea8510b2fd2299e4ea75dd14c2f76be267574664c8b767d1d9d7ad
6
+ metadata.gz: 90c79e21790f1a952966df8113073ba863dd4f6fc41bf8800bf0232ee5e7ed8e0ba2034a476616147fc5947b1ff3f44987470f611772ce25c1b0b0c5c7221bae
7
+ data.tar.gz: 2be1c0dc66498943a093fcf3dc6cf6d2ee378486a704ee5f4d62db7454bf8aeb78c620852cd0b9489b72d79c81965e3e62502d689fbf5c03337097fcaed36dea
data/README.md CHANGED
@@ -36,6 +36,21 @@ resolver = ConstantResolver.new(
36
36
  )
37
37
  ```
38
38
 
39
+ ### Default namespaces
40
+
41
+ If any load paths have a default namespace other than `Object`, these can be specified in a hash:
42
+
43
+ ```ruby
44
+ resolver = ConstantResolver.new(
45
+ root_path: "/app",
46
+ load_paths: {
47
+ "/app/models" => "::Object",
48
+ "/app/services" => "::Object",
49
+ "/app/internal" => "::Business",
50
+ }
51
+ )
52
+ ```
53
+
39
54
  ### Resolve a constant
40
55
 
41
56
  Resolve a constant from the contents of your load paths:
@@ -1,8 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- lib = File.expand_path("../lib", __FILE__)
4
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
- require "constant_resolver/version"
3
+ require_relative "lib/constant_resolver/version"
6
4
 
7
5
  Gem::Specification.new do |spec|
8
6
  spec.name = "constant_resolver"
@@ -22,6 +20,10 @@ Gem::Specification.new do |spec|
22
20
  spec.metadata["homepage_uri"] = spec.homepage
23
21
  spec.metadata["source_code_uri"] = "https://github.com/Shopify/constant_resolver"
24
22
  spec.metadata["changelog_uri"] = "https://github.com/Shopify/constant_resolver/releases"
23
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
24
+ else
25
+ raise "RubyGems 2.0 or newer is required to protect against " \
26
+ "public gem pushes."
25
27
  end
26
28
 
27
29
  # Specify which files should be added to the gem when it is released.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ConstantResolver
4
- VERSION = "0.1.5"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -41,10 +41,9 @@ class ConstantResolver
41
41
  # )
42
42
  def initialize(root_path:, load_paths:, inflector: DefaultInflector.new)
43
43
  root_path += "/" unless root_path.end_with?("/")
44
- load_paths = load_paths.map { |p| p.end_with?("/") ? p : p + "/" }.uniq
45
44
 
46
45
  @root_path = root_path
47
- @load_paths = load_paths
46
+ @load_paths = coerce_load_paths(load_paths)
48
47
  @file_map = nil
49
48
  @inflector = inflector
50
49
  end
@@ -76,10 +75,11 @@ class ConstantResolver
76
75
  @file_map = {}
77
76
  duplicate_files = {}
78
77
 
79
- @load_paths.each do |load_path|
80
- Dir[@root_path + load_path + "**/*.rb"].each do |file_path|
78
+ @load_paths.each_pair do |load_path, default_ns|
79
+ Dir[glob_path(load_path)].each do |file_path|
81
80
  root_relative_path = file_path.delete_prefix!(@root_path)
82
81
  const_name = @inflector.camelize(root_relative_path.delete_prefix(load_path).delete_suffix!(".rb"))
82
+ const_name = "#{default_ns}::#{const_name}" unless default_ns == "Object"
83
83
  existing_entry = @file_map[const_name]
84
84
 
85
85
  if existing_entry
@@ -90,13 +90,22 @@ class ConstantResolver
90
90
  end
91
91
  end
92
92
 
93
- unless duplicate_files.empty?
94
- message = duplicate_files.map do |const_name, full_paths|
95
- "ERROR: '#{const_name}' could refer to any of\n#{full_paths.map { |p| ' ' + p }.join("\n")}"
96
- end.join("\n")
97
- raise(Error, message)
93
+ if duplicate_files.any?
94
+ raise(Error, <<~MSG)
95
+ Ambiguous constant definition:
96
+
97
+ #{duplicate_files.map { |const_name, paths| ambiguous_constant_message(const_name, paths) }.join("\n")}
98
+ MSG
99
+ end
100
+
101
+ if @file_map.empty?
102
+ raise(Error, <<~MSG)
103
+ Could not find any ruby files. Searched in:
104
+
105
+ - #{@load_paths.keys.map { |load_path| glob_path(load_path) }.join("\n- ")}
106
+ MSG
98
107
  end
99
- raise(Error, "could not find any files") if @file_map.empty?
108
+
100
109
  @file_map
101
110
  end
102
111
 
@@ -110,6 +119,26 @@ class ConstantResolver
110
119
 
111
120
  private
112
121
 
122
+ def coerce_load_paths(load_paths)
123
+ load_paths = Hash[load_paths.map { |p| [p, "Object"] }] unless load_paths.respond_to?(:transform_keys)
124
+
125
+ load_paths.transform_keys! { |p| p.end_with?("/") ? p : p + "/" }
126
+ load_paths.transform_values! { |ns| ns.to_s.delete_prefix("::") }
127
+
128
+ load_paths
129
+ end
130
+
131
+ def ambiguous_constant_message(const_name, paths)
132
+ <<~MSG.chomp
133
+ "#{const_name}" could refer to any of
134
+ #{paths.join("\n ")}
135
+ MSG
136
+ end
137
+
138
+ def glob_path(path)
139
+ @root_path + path + "**/*.rb"
140
+ end
141
+
113
142
  def resolve_constant(const_name, current_namespace_path, original_name: const_name)
114
143
  namespace, location = resolve_traversing_namespace_path(const_name, current_namespace_path)
115
144
  if location
data/service.yml CHANGED
@@ -1,6 +1,2 @@
1
- ---
2
1
  classification: library
3
- owners:
4
- - Shopify/component-patterns
5
- slack_channels:
6
- - architecture-patterns
2
+ ci_url: https://github.com/Shopify/constant_resolver/actions?query=workflow%3ACI
data/shipit.yml CHANGED
@@ -1 +1 @@
1
-
1
+ # defaults
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: constant_resolver
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Philip Müller
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-18 00:00:00.000000000 Z
11
+ date: 2022-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -71,6 +71,7 @@ metadata:
71
71
  homepage_uri: https://github.com/Shopify/constant_resolver
72
72
  source_code_uri: https://github.com/Shopify/constant_resolver
73
73
  changelog_uri: https://github.com/Shopify/constant_resolver/releases
74
+ allowed_push_host: https://rubygems.org
74
75
  post_install_message:
75
76
  rdoc_options: []
76
77
  require_paths:
@@ -86,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
86
87
  - !ruby/object:Gem::Version
87
88
  version: '0'
88
89
  requirements: []
89
- rubygems_version: 3.0.3
90
+ rubygems_version: 3.2.20
90
91
  signing_key:
91
92
  specification_version: 4
92
93
  summary: Statically resolve any ruby constant