Module: TestConsole::Utility::ClassMethods
- Included in:
- TestConsole::Utility
- Defined in:
- lib/test_console/utility.rb
Overview
Returns an array of class components from a filename
Common folder names are dropped from the array to avoid confusion.
Examples:
class_from_filename module/controller # ['Module', 'Controller']
class_from_filename unit/module/controller # ['Module', 'Controller']
class_from_filename parent/module/controller # ['Parent', 'Module', 'Controller']
Instance Method Summary collapse
- #class_from_filename(filename) ⇒ Object
-
#const_defined?(klass) ⇒ Boolean
Returns boolean as to wether the specified constant is defined.
-
#const_get(klass) ⇒ Object
Returns the specified class.
-
#const_remove(klass) ⇒ Object
Removes the specified class.
Instance Method Details
#class_from_filename(filename) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/test_console/utility.rb', line 18 def class_from_filename(filename) segs = filename.split('/') segs.delete '' segs.delete '.' segs.last.gsub!('.rb', '') # drop the test folder segs = segs[1..-1] while TestConsole.drop_folders.include?(segs[0]) ret = segs.map {|seg| seg.camelize} return ret end |
#const_defined?(klass) ⇒ Boolean
Returns boolean as to wether the specified constant is defined.
Examples:
const_defined? 'String' # true
const_defined? 'WierdClass' # false
const_defined? ['ActiveRecord', 'Base'] # true (assuming Active Record is installed)
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/test_console/utility.rb', line 39 def const_defined?(klass) klass = [klass] unless klass.kind_of? Array klass.inject(Object) do |context, scope| if context.const_defined?(scope) context.const_get(scope) else return false end end end |
#const_get(klass) ⇒ Object
Returns the specified class.
Examples:
const_get 'String' # String
const_get ['ActiveRecord', 'Base'] # ActiveRecord::Base
57 58 59 60 61 62 |
# File 'lib/test_console/utility.rb', line 57 def const_get(klass) klass = [klass] unless klass.kind_of? Array klass.inject(Object) do |context, scope| context.const_get(scope) end end |
#const_remove(klass) ⇒ Object
Removes the specified class.
Examples:
const_get 'String' # String
const_get ['ActiveRecord', 'Base'] # ActiveRecord::Base
71 72 73 74 75 76 77 78 |
# File 'lib/test_console/utility.rb', line 71 def const_remove(klass) klass = [klass] unless klass.kind_of? Array if klass.length > 1 Utility.const_get(klass[0..-2]).send :remove_const, klass.last elsif klass.any? Object.send :remove_const, klass.last end end |