lazy_names 0.2.0 → 2.0.0

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.
@@ -1,88 +0,0 @@
1
- require 'yaml'
2
-
3
- module LazyNames
4
- class ConfigLoader
5
- class NoConfig < StandardError; end
6
- class ConfigNotResolved < StandardError; end
7
- class NamespaceNotFound < StandardError; end
8
- class NoDefinitions < StandardError; end
9
-
10
- class << self
11
- BasicConfig = Struct.new(:path, :definitions)
12
- HOME_PATH = '~/.lazy_names.yml'.freeze
13
-
14
- def call(namespace:, path: nil)
15
- return read_from_path(namespace, path) if path
16
-
17
- config = read_from_project if config_in_project_path?
18
- config ||= read_from_home_dir(namespace)
19
-
20
- config
21
- end
22
-
23
- private
24
-
25
- def read_from_path(namespace, path)
26
- definitions = find_definitions(path, namespace)
27
-
28
- BasicConfig.new(path, definitions)
29
- rescue Errno::ENOENT, Errno::ENOTDIR
30
- raise NoConfig, "No config found by given path: #{path}"
31
- end
32
-
33
- def read_from_home_dir(namespace)
34
- definitions = find_definitions(home_path, namespace)
35
-
36
- BasicConfig.new(home_path, definitions)
37
- rescue Errno::ENOENT
38
- raise NoConfig, 'No config found in your home directory. ' \
39
- 'Create ~/.lazy_names.yml'
40
- end
41
-
42
- def read_from_project
43
- definitions = find_project_definitions
44
-
45
- BasicConfig.new(project_path, definitions)
46
- end
47
-
48
- def find_project_definitions
49
- read_config(project_path)['definitions'].to_hash
50
-
51
- rescue NoMethodError
52
- raise NoDefinitions, "No definitions found in #{project_path}. " \
53
- 'See config example .lazy_names.tt.project.yml'
54
- end
55
-
56
- def find_definitions(path, namespace)
57
- find_namespace_contents(path, namespace)['definitions'].to_hash
58
-
59
- rescue NoMethodError
60
- raise NoDefinitions, "No definitions found in #{path}. " \
61
- 'See config example in .lazy_names.tt.yml'
62
- end
63
-
64
- def find_namespace_contents(path, namespace)
65
- read_config(path)[namespace].to_hash
66
- rescue NoMethodError
67
- raise NamespaceNotFound, "No namespace found in #{path}. " \
68
- 'See config example in .lazy_names.tt.yml and check README'
69
- end
70
-
71
- def config_in_project_path?
72
- File.exist?(project_path)
73
- end
74
-
75
- def read_config(path)
76
- YAML.safe_load(File.read(path))
77
- end
78
-
79
- def project_path
80
- File.expand_path(Pathname.new(Dir.pwd).join('.lazy_names.yml'))
81
- end
82
-
83
- def home_path
84
- File.expand_path(HOME_PATH)
85
- end
86
- end
87
- end
88
- end
@@ -1,59 +0,0 @@
1
- module LazyNames
2
- class ConfigValidator
3
- attr_reader :errors
4
-
5
- Errors = Struct.new(:undefined, :already_defined)
6
-
7
- def initialize(lazy_names, constants)
8
- @errors = Errors.new([], [])
9
- @constants = constants
10
- @lazy_names = lazy_names
11
- end
12
-
13
- def call
14
- validate_constants!
15
- validate_lazy_names!
16
-
17
- self
18
- end
19
-
20
- private
21
-
22
- attr_reader :lazy_names, :constants
23
-
24
- def validate_constants!
25
- constants.each do |c|
26
- begin
27
- resolve_const_in_project(c)
28
- rescue NameError
29
- self.errors.undefined << c
30
- end
31
- end
32
- end
33
-
34
- def validate_lazy_names!
35
- a = lazy_names.uniq
36
- b = lazy_names
37
-
38
- diff = difference(b, a)
39
-
40
- return unless diff
41
-
42
- diff.each { |name| self.errors.already_defined << name }
43
- end
44
-
45
- def resolve_const_in_project(const)
46
- Module.const_get(const)
47
- end
48
-
49
- def difference(arr, other)
50
- copy = arr.dup
51
- other.each do |e|
52
- i = copy.rindex(e)
53
- copy.delete_at(i) if i
54
- end
55
-
56
- copy
57
- end
58
- end
59
- end
@@ -1,13 +0,0 @@
1
- module LazyNames
2
- class Definer
3
- class << self
4
- def call(config, top_level_binding)
5
- config.constants.each do |origin|
6
- eval <<-RUBY, top_level_binding, __FILE__, __LINE__ + 1
7
- #{config.lazy_name(origin)} = #{origin}
8
- RUBY
9
- end
10
- end
11
- end
12
- end
13
- end
@@ -1,12 +0,0 @@
1
- module LazyNames
2
- class FindNamespace
3
- class << self
4
- ##
5
- # Find project namespace by folder name
6
- #
7
- def call(path = Dir.pwd)
8
- path.split('/').last
9
- end
10
- end
11
- end
12
- end
@@ -1,48 +0,0 @@
1
- module LazyNames
2
- class Logger
3
- class << self
4
- def warn_undefined(errors, config_path)
5
- return if errors.empty?
6
-
7
- message = <<~MSG
8
- Error loading lazy_names gem.
9
- Found #{errors.size} undefined constants.
10
- Please check spelling for #{errors.join(', ')}
11
- #{config_path}
12
- \n
13
- MSG
14
-
15
- warn(message)
16
- end
17
-
18
- def warn_duplicate_definition(errors, config_path)
19
- return if errors.empty?
20
-
21
- message = <<~MSG
22
- Error loading lazy_names gem.
23
- Found #{errors.size} already defined constants.
24
- Using same lazy names for different constants may lead to unexpected results
25
- Avoid duplications in your config file.
26
- #{config_path}
27
- \n
28
- MSG
29
-
30
- warn(message)
31
- end
32
-
33
- def warn_empty_definitions(errors, config_path)
34
- return unless errors
35
-
36
- message = <<~MSG
37
- Error loading lazy_names gem.
38
- Seems like you misspelled namespace in config.
39
- #{config_path}
40
- Please ensure word definitions exists in config
41
- or check .lazy_names.tt.yml for consistency.
42
- MSG
43
-
44
- warn(message)
45
- end
46
- end
47
- end
48
- end