Skip to content

Commit

Permalink
some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed Sep 18, 2016
1 parent 6dea5bb commit a63ed91
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 31 deletions.
69 changes: 38 additions & 31 deletions lib/rails_module_unification/active_support_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,42 @@ def load_from_path(file_path, qualified_name, from_mod, const_name)
end
end

def load_from_parent(from_mod, const_name)
# If our parents do not have a constant named +const_name+ then we are free
# to attempt to load upwards. If they do have such a constant, then this
# const_missing must be due to from_mod::const_name, which should not
# return constants from from_mod's parents.
parent = from_mod.parent
present_in_ancestry = (
parent &&
parent != from_mod &&
!from_mod.parents.any? { |p| p.const_defined?(const_name, false) }
)

# Since Ruby does not pass the nesting at the point the unknown
# constant triggered the callback we cannot fully emulate constant
# name lookup and need to make a trade-off: we are going to assume
# that the nesting in the body of Foo::Bar is [Foo::Bar, Foo] even
# though it might not be. Counterexamples are
#
# class Foo::Bar
# Module.nesting # => [Foo::Bar]
# end
#
# or
#
# module M::N
# module S::T
# Module.nesting # => [S::T, M::N]
# end
# end
#
# for example.
return parent.const_missing(const_name) if present_in_ancestry
rescue NameError => e
raise unless e.missing_name? qualified_name_for(parent, const_name)
end

# Load the constant named +const_name+ which is missing from +from_mod+. If
# it is not possible to load the constant into from_mod, try its parent
# module using +const_missing+.
Expand Down Expand Up @@ -65,37 +101,8 @@ def load_missing_constant(from_mod, const_name)
mod = autoload_module!(from_mod, const_name, qualified_name, file_name)
return mod if mod

if (parent = from_mod.parent) && parent != from_mod &&
!from_mod.parents.any? { |p| p.const_defined?(const_name, false) }
# If our parents do not have a constant named +const_name+ then we are free
# to attempt to load upwards. If they do have such a constant, then this
# const_missing must be due to from_mod::const_name, which should not
# return constants from from_mod's parents.
begin
# Since Ruby does not pass the nesting at the point the unknown
# constant triggered the callback we cannot fully emulate constant
# name lookup and need to make a trade-off: we are going to assume
# that the nesting in the body of Foo::Bar is [Foo::Bar, Foo] even
# though it might not be. Counterexamples are
#
# class Foo::Bar
# Module.nesting # => [Foo::Bar]
# end
#
# or
#
# module M::N
# module S::T
# Module.nesting # => [S::T, M::N]
# end
# end
#
# for example.
return parent.const_missing(const_name)
rescue NameError => e
raise unless e.missing_name? qualified_name_for(parent, const_name)
end
end
from_parent = load_from_parent(from_mod, const_name)
return from_parent if from_parent

name_error = NameError.new("uninitialized constant #{qualified_name}", const_name)
name_error.set_backtrace(caller.reject { |l| l.starts_with? __FILE__ })
Expand Down
22 changes: 22 additions & 0 deletions spec/support/rails_app/log/development.log
Original file line number Diff line number Diff line change
Expand Up @@ -802,3 +802,25 @@ DEPRECATION WARNING: Passing #original_exception is deprecated and has no effect
 (0.0ms) begin transaction
SQL (0.1ms) INSERT INTO "ar_internal_metadata" ("key", "value", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["key", "environment"], ["value", "development"], ["created_at", 2016-09-18 01:34:41 UTC], ["updated_at", 2016-09-18 01:34:41 UTC]]
 (0.0ms) commit transaction
 (0.2ms) CREATE TABLE "authors" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar)
 (0.1ms) CREATE TABLE "posts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "body" text, "title" varchar, "author_id" integer)
 (0.1ms) select sqlite_version(*)
 (0.1ms) CREATE INDEX "index_posts_on_author_id" ON "posts" ("author_id")
 (0.1ms) CREATE TABLE "profiles" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "project_url" text, "bio" text, "birthday" date, "author_id" integer)
 (0.1ms) CREATE INDEX "index_profiles_on_author_id" ON "profiles" ("author_id")
 (0.1ms) CREATE TABLE "ar_internal_metadata" ("key" varchar NOT NULL PRIMARY KEY, "value" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
ActiveRecord::InternalMetadata Load (0.1ms) SELECT "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = ? LIMIT ? [["key", :environment], ["LIMIT", 1]]
 (0.1ms) begin transaction
SQL (0.1ms) INSERT INTO "ar_internal_metadata" ("key", "value", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["key", "environment"], ["value", "development"], ["created_at", 2016-09-18 01:34:50 UTC], ["updated_at", 2016-09-18 01:34:50 UTC]]
 (0.0ms) commit transaction
 (0.2ms) CREATE TABLE "authors" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar)
 (0.1ms) CREATE TABLE "posts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "body" text, "title" varchar, "author_id" integer)
 (0.1ms) select sqlite_version(*)
 (0.1ms) CREATE INDEX "index_posts_on_author_id" ON "posts" ("author_id")
 (0.1ms) CREATE TABLE "profiles" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "project_url" text, "bio" text, "birthday" date, "author_id" integer)
 (0.1ms) CREATE INDEX "index_profiles_on_author_id" ON "profiles" ("author_id")
 (0.1ms) CREATE TABLE "ar_internal_metadata" ("key" varchar NOT NULL PRIMARY KEY, "value" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
ActiveRecord::InternalMetadata Load (0.1ms) SELECT "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = ? LIMIT ? [["key", :environment], ["LIMIT", 1]]
 (0.0ms) begin transaction
SQL (0.1ms) INSERT INTO "ar_internal_metadata" ("key", "value", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["key", "environment"], ["value", "development"], ["created_at", 2016-09-18 02:06:06 UTC], ["updated_at", 2016-09-18 02:06:06 UTC]]
 (0.0ms) commit transaction

0 comments on commit a63ed91

Please sign in to comment.