class Class

Public Instance Methods

create_foorth_instance(vm) click to toggle source

Create an instance of this fOOrth class.
Parameters:

  • vm - The current fOOrth virtual machine.

# File lib/fOOrth/core/class.rb, line 59
def create_foorth_instance(vm)
  (obj = self.new).foorth_init(vm)
  obj
end
create_foorth_proxy(proxy_name = nil) click to toggle source

Add this class as a proxy class in the foorth class system.
Parameters:

  • proxy_name - the optional name of the proxy. Defaults to the Ruby name.


Returns:

  • The spec of the proxy class.

# File lib/fOOrth/core/class.rb, line 89
def create_foorth_proxy(proxy_name = nil)
  if proxy_name
    self.foorth_name = proxy_name
  end

  validate_class_name(foorth_name)

  error "F10: The class #{foorth_name} already exists." if $FOORTH_GLOBALS[foorth_name]

  install_foorth_class(foorth_name, self)
end
create_foorth_subclass(foorth_name) click to toggle source

Create a new fOOrth subclass of this class.
Parameters:


Returns:

  • The spec of the subclass.


Note:

  • If a sub-class with the given name already exists, an exception is raised.

# File lib/fOOrth/core/class.rb, line 71
def create_foorth_subclass(foorth_name)
  if $FOORTH_GLOBALS[XfOOrth::SymbolMap.map(foorth_name)]
    error "F10: The class #{foorth_name} already exists."
  end

  validate_class_name(foorth_name)

  new_class = Class.new(self) {self.foorth_name = foorth_name}

  XfOOrth.const_set('XfOOrth_' + foorth_name, new_class)
  install_foorth_class(foorth_name, new_class)
end
create_shared_method(name, spec_class, options, &block) click to toggle source

Create a shared method on this fOOrth class.
Parameters:

  • name - The name of the method to create.

  • spec_class - The specification class to use.

  • options - An array of options.

  • block - A block to associate with the name.


Returns

  • The spec created for the shared method.

# File lib/fOOrth/core/class.rb, line 40
def create_shared_method(name, spec_class, options, &block)
  sym = XfOOrth::SymbolMap.add_entry(name)
  spec = spec_class.new(name, sym, options, &block)

  unless self == Object && options.include?(:stub)
    define_method(sym, &spec.does)
  end

  foorth_shared[sym] = spec
end
foorth_class_name() click to toggle source

Get the name of the class or a safe default.

# File lib/fOOrth/core/class.rb, line 14
def foorth_class_name
  self.foorth_name || "AnonymousClass<#{self.object_id}>".freeze
end
foorth_method_info(name) click to toggle source

Investigate a method of this class.

# File lib/fOOrth/library/introspection/class.rb, line 28
def foorth_method_info(name)
  symbol, results = XfOOrth::SymbolMap.map_info(name)
  found = false

  if symbol
    if scrape_method_info(results, *map_foorth_shared_info(symbol))
      found = true
    end

    if scrape_method_info(results, *map_foorth_exclusive_info(symbol, "Class"))
      found = true
    end

    results << ["Scope", "not found."] unless found
  end

  results
end
foorth_name() click to toggle source

Get the foorth name of this class.
Decree!

  • These are to be the only references to @_private_foorth_name!

# File lib/fOOrth/core/class.rb, line 9
def foorth_name
  @_private_foorth_name ||= name.gsub(/.*::/, '').freeze
end
foorth_name=(new_name) click to toggle source

Set the foorth name of this class.
Decree!

  • These are to be the only references to @_private_foorth_name!

# File lib/fOOrth/core/class.rb, line 21
def foorth_name=(new_name)
  @_private_foorth_name = new_name.freeze
end
foorth_shared() click to toggle source

Access/create the class's shared fOOrth dictionary.
Decree!

  • This is to be the only reference to @_private_foorth_shared!

# File lib/fOOrth/core/class.rb, line 28
def foorth_shared
  @_private_foorth_shared ||= Hash.new
end
get_info() click to toggle source

Get introspection info.

# File lib/fOOrth/library/introspection/class.rb, line 48
def get_info
  results = [["Lineage", lineage]]

  get_exclusive_method_info(results, "Class")
  get_shared_method_info(results)

  results
end
lineage() click to toggle source

Get the lineage of this class.

# File lib/fOOrth/library/introspection/class.rb, line 18
def lineage
  #Ugly hack, sorry!
  if [Object, Module].include?(self)
    "Object"
  else
    foorth_class_name + " < " + superclass.lineage
  end
end
map_foorth_shared(symbol) click to toggle source

Map the symbol to a specification or nil if there is no mapping.

# File lib/fOOrth/core/class.rb, line 52
def map_foorth_shared(symbol)
  foorth_shared[symbol] || ((sc = superclass) && sc.map_foorth_shared(symbol))
end
map_foorth_shared_info(symbol, shallow=nil) click to toggle source

Get information about the mapping of the symbol.

# File lib/fOOrth/library/introspection/class.rb, line 7
def map_foorth_shared_info(symbol, shallow=nil)
  if (spec = foorth_shared[symbol])
    [spec, [["Class", foorth_class_name], ["Scope", "Shared"]]]
  elsif (sc = superclass) && !shallow
    sc.map_foorth_shared_info(symbol)
  else
    [nil, nil]
  end
end

Private Instance Methods

get_shared_method_info(results) click to toggle source

Get shared method info

# File lib/fOOrth/library/introspection/class.rb, line 72
def get_shared_method_info(results)
  unless foorth_shared.empty?
    results.concat([["", ""], ["Methods", "Shared"]])

    foorth_shared.extract_method_names(:all).sort.each do |name|
      symbol, info = XfOOrth::SymbolMap.map_info(name)
      (results << ["", ""]).concat(info)

      spec, info = map_foorth_shared_info(symbol, :shallow)
      results.concat(info).concat(spec.get_info)
    end
  end
end
install_foorth_class(new_name, new_class) click to toggle source

Connect the class named foorth_name to the foorth system.
Returns:

  • The newly created spec object.


Endemic Code Smells

  • :reek:UtilityFunction

# File lib/fOOrth/core/class.rb, line 108
def install_foorth_class(new_name, new_class)
  fail "Bad name" unless new_name
  symbol = XfOOrth::SymbolMap.add_entry(new_name)
  $FOORTH_GLOBALS[symbol] = XfOOrth::ClassSpec.new(new_class, nil, [:class])
end
scrape_method_info(results, spec, info) click to toggle source

Get method information
Endemic Code Smells

  • :reek:UtilityFunction

# File lib/fOOrth/library/introspection/class.rb, line 62
def scrape_method_info(results, spec, info)
  if spec && !spec.has_tag?(:stub)
    (results << ["", ""]).concat(info).concat(spec.get_info)
    true
  else
    false
  end
end
validate_class_name(name) click to toggle source

Is this a valid class name?

# File lib/fOOrth/core/class.rb, line 115
def validate_class_name(name)
  unless /^[A-Z][A-Za-z0-9]+$/ =~ name
    error "F10: Invalid class name #{name}"
  end
end