Skip to content
Permalink
Browse files

Merge pull request #668 from krekoten/module_include

Module#include, Module#include? and Module#constants
  • Loading branch information
alex committed May 2, 2013
2 parents 32c4127 + 215c56d commit b1b3d1676d0572d37e266154977fe989b1ddd302

This file was deleted.

@@ -1,10 +1,8 @@
fails:Module.constants returns an array of the names of all toplevel constants
fails:Module.constants returns an array of Symbol names
fails:Module.constants returns Module's constants when given a parameter
fails:Module#constants returns an array of Symbol names of all constants defined in the moduleand all included modules
fails:Module#constants returns all constants including inherited when passed true
fails:Module#constants returns all constants including inherited when passed some object
fails:Module#constants includes names of constants defined after a module is included
fails:Module#constants doesn't returns inherited constants when passed false
fails:Module#constants doesn't returns inherited constants when passed nil
fails:Module#constants returns only public constants
@@ -1,14 +1,4 @@
fails:Module#include calls #append_features(self) in reversed order on each module
fails:Module#include raises a TypeError when the argument is not a Module
fails:Module#include imports constants to modules and classes
fails:Module#include imports instance methods to modules and classes
fails:Module#include does not import methods to modules and classes
fails:Module#include doesn't include module if it is included in a super class
fails:Module#include recursively includes new mixins
fails:Module#include preserves ancestor order
fails:Module#include detects cyclic includes
fails:Module#include returns the class it's included into
fails:Module#include? returns true if the given module is included by self or one of it's ancestors
fails:Module#include? returns false if given module is equal to self
fails:Module#include? raises a TypeError when no module was given
fails:Module#include accepts no-arguments
@@ -886,9 +886,9 @@ def test_regexp_sytnax_error(self, space):
with self.raises(space, "SyntaxError"):
space.execute("/(/")

def test_class_variable_accessed_from_instance_side(self, space):
def test_class_variable_from_module_accessed_from_instance_side(self, space):
w_res = space.execute("""
class A
module A
@@foo = 'a'
end
@@ -903,6 +903,8 @@ def get
return B.new.get
""")
assert space.str_w(w_res) == 'a'

def test_class_variable_accessed_from_instance_side(self, space):
w_res = space.execute("""
class A; end
class B < A
@@ -175,6 +175,15 @@ def find_included_const(self, space, name):
break
return w_res

def included_constants(self, space):
consts = {}
for const in self.constants_w.keys():
consts[const] = None
for w_mod in self.included_modules:
for const in w_mod.included_constants(space):
consts[const] = None
return consts.keys()

def find_local_const(self, space, name):
return self._find_const_pure(name, self.version)

@@ -319,11 +328,34 @@ def method_to_s(self, space):
return space.newstr_fromstr(name)

@classdef.method("include")
def method_include(self, space, w_mod):
space.send(w_mod, space.newsymbol("append_features"), [self])
def method_include(self, space, args_w):
for w_mod in args_w:
if type(w_mod) is not W_ModuleObject:
raise space.error(
space.w_TypeError,
"wrong argument type %s (expected Module)" % space.obj_to_s(space.getclass(w_mod))
)

for w_mod in reversed(args_w):
space.send(w_mod, space.newsymbol("append_features"), [self])

return self

@classdef.method("include?")
def method_includep(self, space, w_mod):
if type(w_mod) is not W_ModuleObject:
raise space.error(
space.w_TypeError,
"wrong argument type %s (expected Module)" % space.obj_to_s(space.getclass(w_mod))
)
if w_mod is self:
return space.w_false
return space.newbool(w_mod in self.ancestors())

@classdef.method("append_features")
def method_append_features(self, space, w_mod):
if w_mod in self.ancestors():
raise space.error(space.w_ArgumentError, "cyclic include detected")
for module in reversed(self.ancestors()):
w_mod.include_module(space, module)

@@ -429,7 +461,7 @@ def method_private_constant(self, space, args_w):

@classdef.method("constants")
def method_constants(self, space):
return space.newarray([space.newsymbol(n) for n in self.constants_w])
return space.newarray([space.newsymbol(n) for n in self.included_constants(space)])

@classdef.method("const_missing", name="symbol")
def method_const_missing(self, space, name):

0 comments on commit b1b3d16

Please sign in to comment.
You can’t perform that action at this time.