rails_module_unification 0.5.1 → 0.5.2

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
  SHA1:
3
- metadata.gz: 71501a9317c80c5f091da00abee3b55755542e8f
4
- data.tar.gz: 1e2cfebbba3b4671ee89580093392eb92eb12efb
3
+ metadata.gz: 4fc7b8b01f6fb330a6e84c81c3ceed48335bc1c5
4
+ data.tar.gz: 38a60cb1df143dc03726f967c4eb0e718fb9b42a
5
5
  SHA512:
6
- metadata.gz: b40c277e2ba4676f2b1a77d74f9aeb75de6f4bf95a3bfd5627d12fad840aea2b6a9def255d7bb75ba56765d0b19f62a6fc604a79f7ab5deb347f7ebe65402994
7
- data.tar.gz: 3eda030e1c103c2d27c3b4cf0398f98b09a87243a6556e3e33a5980ccef1a9be3f0e3508830ceeac93384328b08e417b49be803a1767a801b639b75de37588d7
6
+ metadata.gz: 81974c0aca20c76a00f39624220c518c77cfdea57c17689bcd2eff0f490782dd699e588a5bafee99c9cd75520372f544f0a7873911d8e28e97fe5f8d0c3b82f4
7
+ data.tar.gz: 6532e3639b354387b8827c2998554cdcab19475c43737af3ab6c4352f48252e61b30330a39a495ccdd634dd7651c03ae6ef45bd5395ca51938f0e29a967867f1
data/README.md CHANGED
@@ -1,6 +1,12 @@
1
1
  # rails_module_unification
2
2
  Ember's module unification brought to Rails.
3
3
 
4
+ [![Build Status](https://travis-ci.org/NullVoxPopuli/rails_module_unification.svg?branch=master)](https://travis-ci.org/NullVoxPopuli/rails_module_unification)
5
+ [![Code Climate](https://codeclimate.com/repos/57dddb2c50dac40e6900197c/badges/73a0a0761e417c655b68/gpa.svg)](https://codeclimate.com/repos/57dddb2c50dac40e6900197c/feed)
6
+ [![Test Coverage](https://codeclimate.com/repos/57dddb2c50dac40e6900197c/badges/73a0a0761e417c655b68/coverage.svg)](https://codeclimate.com/repos/57dddb2c50dac40e6900197c/coverage)
7
+ [![Dependency Status](https://gemnasium.com/badges/github.com/NullVoxPopuli/rails_module_unification.svg)](https://gemnasium.com/github.com/NullVoxPopuli/rails_module_unification)
8
+
9
+
4
10
  ## What is this about?
5
11
 
6
12
  With large rails application, the default architecture can result in a resource's related files being very spread out through the overall project structure. For example, lets say you have 50 controllers, serializers, policies, and operations. That's _four_ different top level folders that spread out all the related objects. It makes sense do it this way, as it makes rails' autoloading programmatically easy.
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
  module RailsModuleUnification
3
3
  module ActiveSupportExtensions
4
+ RESOURCE_SUFFIXES = /(Controller|Serializer|Operation|Policy)/
5
+
4
6
  def load_from_path(file_path, qualified_name, from_mod, const_name)
5
7
  expanded = File.expand_path(file_path)
6
8
  expanded.sub!(/\.rb\z/, '')
@@ -14,20 +16,43 @@ module RailsModuleUnification
14
16
  end
15
17
  end
16
18
 
17
- # Load the constant named +const_name+ which is missing from +from_mod+. If
18
- # it is not possible to load the constant into from_mod, try its parent
19
- # module using +const_missing+.
20
- def load_missing_constant(from_mod, const_name)
21
- # always default to the actual implementation
22
- super
23
- rescue LoadError, NameError
24
- suffixes = /(Controller|Serializer)\z/
19
+ def load_from_parent(from_mod, const_name)
20
+ # If our parents do not have a constant named +const_name+ then we are free
21
+ # to attempt to load upwards. If they do have such a constant, then this
22
+ # const_missing must be due to from_mod::const_name, which should not
23
+ # return constants from from_mod's parents.
24
+ parent = from_mod.parent
25
+ present_in_ancestry = (
26
+ parent &&
27
+ parent != from_mod &&
28
+ !from_mod.parents.any? { |p| p.const_defined?(const_name, false) }
29
+ )
25
30
 
26
- # examples
27
- # - Api::PostsController
28
- # - PostsController
29
- qualified_name = qualified_name_for from_mod, const_name
31
+ # Since Ruby does not pass the nesting at the point the unknown
32
+ # constant triggered the callback we cannot fully emulate constant
33
+ # name lookup and need to make a trade-off: we are going to assume
34
+ # that the nesting in the body of Foo::Bar is [Foo::Bar, Foo] even
35
+ # though it might not be. Counterexamples are
36
+ #
37
+ # class Foo::Bar
38
+ # Module.nesting # => [Foo::Bar]
39
+ # end
40
+ #
41
+ # or
42
+ #
43
+ # module M::N
44
+ # module S::T
45
+ # Module.nesting # => [S::T, M::N]
46
+ # end
47
+ # end
48
+ #
49
+ # for example.
50
+ return parent.const_missing(const_name) if present_in_ancestry
51
+ rescue NameError => e
52
+ raise unless e.missing_name? qualified_name_for(parent, const_name)
53
+ end
30
54
 
55
+ def resource_path_from_const_name(qualified_name)
31
56
  # examples
32
57
  # - api/posts_controller
33
58
  # - posts_controller
@@ -42,7 +67,7 @@ module RailsModuleUnification
42
67
  # examples:
43
68
  # - api/posts
44
69
  # - posts
45
- folder_name = qualified_name.split(suffixes).first.underscore.pluralize
70
+ folder_name = qualified_name.split(RESOURCE_SUFFIXES).first.underscore.pluralize
46
71
 
47
72
  # examples:
48
73
  # - posts/posts_controller
@@ -59,43 +84,32 @@ module RailsModuleUnification
59
84
  # the resource_name/resource_names_controller.rb naming scheme
60
85
  file_path ||= search_for_file(folder_named_type)
61
86
 
87
+ file_path
88
+ end
89
+
90
+ # Load the constant named +const_name+ which is missing from +from_mod+. If
91
+ # it is not possible to load the constant into from_mod, try its parent
92
+ # module using +const_missing+.
93
+ def load_missing_constant(from_mod, const_name)
94
+ # always default to the actual implementation
95
+ super
96
+ rescue LoadError, NameError
97
+
98
+ # examples
99
+ # - Api::PostsController
100
+ # - PostsController
101
+ qualified_name = qualified_name_for from_mod, const_name
102
+
103
+ file_path = resource_path_from_qualified_name(qualified_name)
104
+
62
105
  return load_from_path(file_path, qualified_name, from_mod, const_name) if file_path
63
106
 
64
107
  # TODO: what is the situation in which this is needed?
65
108
  mod = autoload_module!(from_mod, const_name, qualified_name, file_name)
66
109
  return mod if mod
67
110
 
68
- if (parent = from_mod.parent) && parent != from_mod &&
69
- !from_mod.parents.any? { |p| p.const_defined?(const_name, false) }
70
- # If our parents do not have a constant named +const_name+ then we are free
71
- # to attempt to load upwards. If they do have such a constant, then this
72
- # const_missing must be due to from_mod::const_name, which should not
73
- # return constants from from_mod's parents.
74
- begin
75
- # Since Ruby does not pass the nesting at the point the unknown
76
- # constant triggered the callback we cannot fully emulate constant
77
- # name lookup and need to make a trade-off: we are going to assume
78
- # that the nesting in the body of Foo::Bar is [Foo::Bar, Foo] even
79
- # though it might not be. Counterexamples are
80
- #
81
- # class Foo::Bar
82
- # Module.nesting # => [Foo::Bar]
83
- # end
84
- #
85
- # or
86
- #
87
- # module M::N
88
- # module S::T
89
- # Module.nesting # => [S::T, M::N]
90
- # end
91
- # end
92
- #
93
- # for example.
94
- return parent.const_missing(const_name)
95
- rescue NameError => e
96
- raise unless e.missing_name? qualified_name_for(parent, const_name)
97
- end
98
- end
111
+ from_parent = load_from_parent(from_mod, const_name)
112
+ return from_parent if from_parent
99
113
 
100
114
  name_error = NameError.new("uninitialized constant #{qualified_name}", const_name)
101
115
  name_error.set_backtrace(caller.reject { |l| l.starts_with? __FILE__ })
@@ -5,7 +5,7 @@ module RailsModuleUnification
5
5
  class Railtie < Rails::Railtie
6
6
  initializer 'activeservice.autoload', before: :set_autoload_paths do |app|
7
7
  # TODO: make the module unification root directory configurable
8
- mu_dir = "#{Rails.root}/app/mu"
8
+ mu_dir = "#{Rails.root}/app/#{RailsModuleUnification.directory}"
9
9
 
10
10
  # Data
11
11
  data_paths = Dir["#{mu_dir}/data/**/"]
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module RailsModuleUnification
3
- VERSION = '0.5.1'.freeze
3
+ VERSION = '0.5.2'.freeze
4
4
  end
@@ -6,6 +6,16 @@ require 'rails_module_unification/active_support_extensions'
6
6
  module RailsModuleUnification
7
7
  extend ActiveSupport::Autoload
8
8
 
9
+ module_function
10
+
11
+ def directory=(dir)
12
+ @directory = dir
13
+ end
14
+
15
+ def directory
16
+ @directory || ''
17
+ end
18
+
9
19
  require 'rails_module_unification/railtie'
10
- ActiveSupport::Dependencies.extend ActiveSupportExtensions
20
+ ActiveSupport::Dependencies.extend RailsModuleUnification::ActiveSupportExtensions
11
21
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_module_unification
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - L. Preston Sego III
@@ -52,20 +52,6 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: codeclimate-test-reporter
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '0'
69
55
  - !ruby/object:Gem::Dependency
70
56
  name: awesome_print
71
57
  requirement: !ruby/object:Gem::Requirement
@@ -199,5 +185,5 @@ rubyforge_project:
199
185
  rubygems_version: 2.5.1
200
186
  signing_key:
201
187
  specification_version: 4
202
- summary: RailsModuleUnification-0.5.1
188
+ summary: RailsModuleUnification-0.5.2
203
189
  test_files: []