Module: OpenNamespace::ClassMethods

Defined in:
lib/open_namespace/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#const_defined?(name, *inherit) ⇒ Boolean

Checks if a constant is defined or attempts loading the constant.

Parameters:

  • name (String)

    The name of the constant.

  • inherit (Boolean)

    Specifies whether to search the ancestors for the constant.

Returns:

  • (Boolean)

    Specifies whether the constant is defined.



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/open_namespace/class_methods.rb', line 96

def const_defined?(name,*inherit)
  if super(name,*inherit)
    true
  else
    # attempt to load the file that might have the constant
    require_file(OpenNamespace.const_path(name))

    # check for the constant again
    return super(name,*inherit)
  end
end

#const_lookup(name) ⇒ Object?

Finds the exact constant.

Parameters:

  • name (String)

    The name of the constant.

Returns:

  • (Object, nil)

    The exact constant or nil if the constant could not be found.

Since:

  • 0.4.0



119
120
121
# File 'lib/open_namespace/class_methods.rb', line 119

def const_lookup(name)
  OpenNamespace.const_lookup(self,name)
end

#const_missing(name) ⇒ Class, Module (protected)

Provides transparent access to require_const.

Parameters:

  • name (Symbol)

    The name of the constant to load.

Returns:

  • (Class, Module)

    The loaded constant.

Raises:

  • (NameError)

    Could not load or find the required constant.

See Also:



155
156
157
158
159
160
161
162
# File 'lib/open_namespace/class_methods.rb', line 155

def const_missing(name)
  if self.const_defined?(name)
    # get the exact constant name that was requested
    return self.const_get(name)
  end
  
  return super(name)
end

#const_search(file_name) ⇒ Object?

Finds the constant with a name similar to the given file name.

Parameters:

  • file_name (Symbol, String)

    The file name that represents the constant.

Returns:

  • (Object, nil)

    Returns the found constants, or nil if a NameError exception was encountered.

Since:

  • 0.3.0



135
136
137
# File 'lib/open_namespace/class_methods.rb', line 135

def const_search(file_name)
  OpenNamespace.const_search(self,file_name)
end

#namespace_rootString

The file path that represents the namespace.

Returns:

  • (String)

    The file path used to load constants into the namespace.



11
12
13
# File 'lib/open_namespace/class_methods.rb', line 11

def namespace_root
  @namespace_root ||= OpenNamespace.const_path(self.name)
end

#namespace_root=(new_path) ⇒ String

Sets the file path of the namespace.

Parameters:

  • new_path (String)

    The new path of the namespace.

Returns:

  • (String)

    The file path used to load constants into the namespace.



24
25
26
# File 'lib/open_namespace/class_methods.rb', line 24

def namespace_root=(new_path)
  @namespace_root = new_path.to_s
end

#require_const(name) ⇒ Class, ...

Requires the file and finds the newly defined constant.

Examples:

Loading a constant.

require_const :helper
# => Fully::Qualiffied::Namespace::Helper

Loading a constant from a sub-path.

require_const 'network/helper'
# => Fully::Qualified::Namespace::Network::Helper

Parameters:

  • name (String, Symbol)

    The name or sub-path of the file.

Returns:

  • (Class, Module, nil)

    The constant loaded from the file. If +nil+ is returned, then either the file could not be loaded or the required constant could not be found.



47
48
49
50
51
# File 'lib/open_namespace/class_methods.rb', line 47

def require_const(name)
  require_file(name)

  return const_search(name)
end

#require_file(name) ⇒ true?

Requires the file with the given name, within the namespace root directory.

Parameters:

  • name (Symbol, String)

    The name of the file to require.

Returns:

  • (true, nil)

    Returns true if the file was successfully loaded, returns nil on a LoadError exception.

Raises:

  • (Gem::LoadError)

    A dependency needed by the file could not be satisfied by RubyGems.

Since:

  • 0.3.0



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/open_namespace/class_methods.rb', line 69

def require_file(name)
  name = name.to_s
  path = File.join(namespace_root,File.expand_path(File.join('',name)))

  begin
    require path
  rescue Gem::LoadError => e
    raise(e)
  rescue ::LoadError
    return nil
  end

  return true
end