class Module

Public Instance Methods

container_for(*fields, &block) click to toggle source
# File lib/adsl/util/general.rb, line 133
def container_for(*fields, &block)
  all_fields = Set.new(fields)
  if respond_to? :container_for_fields
    prev_fields = send :container_for_fields
    all_fields.merge prev_fields
  end
  singleton_class.send :define_method, :container_for_fields, lambda{ all_fields }

  attr_accessor *fields

  send :define_method, :initialize do |*options|
    options = options.empty? ? {} : options[0]
    options ||= {}
    options_trimmed = Hash[options]

    all_fields.each do |field|
      instance_variable_set "@#{field}".to_sym, options_trimmed.delete(field.to_sym)
    end
    if block
      instance_eval &block
    elsif not options_trimmed.empty?
      raise ArgumentError, "Undefined fields mentioned in initializer of #{self.class}: #{options_trimmed.keys.map{ |key| ":#{key.to_s}"}.join(", ")}" + "\n[#{all_fields.to_a.join ' ' }]"
    end
  end

  send :define_method, :recursively_gather do |method|
    to_inspect = [self]
    inspected = Set[]
    while not to_inspect.empty?
      elem = to_inspect.pop
      inspected << elem
      if elem.class.respond_to? :container_for_fields
        elem.class.container_for_fields.each do |field|
          field_val = elem.send(field)
          next if field_val.nil?
          [field_val].flatten.each do |subval|
            to_inspect << subval unless inspected.include?(subval)
          end
        end
      end
    end
    result = Set[]
    inspected.each do |val|
      result = result + [val.send(method)].flatten if val.class.method_defined?(method)
    end
    result.delete_if{ |a| a.nil? }.flatten
  end
end
lookup_const(const) click to toggle source
# File lib/adsl/util/general.rb, line 99
def lookup_const(const)
  lookup_container = self
  const.to_s.split('::').each do |portion|
    portion = 'Object' if portion.empty?
    return nil unless lookup_container.const_defined? portion
    lookup_container = lookup_container.const_get portion
  end
  lookup_container
end
lookup_or_create_class(name, superclass) click to toggle source
# File lib/adsl/util/general.rb, line 122
def lookup_or_create_class(name, superclass)
  already_defined = lookup_const name
  return already_defined unless already_defined.nil?

  container_name = name.match(/^(.*)::\w+$/) ? $1 : 'Object'
  container = lookup_or_create_module container_name
  new_class = Class.new(superclass)
  container.const_set name.to_s.split('::').last, new_class
  new_class
end
lookup_or_create_module(name) click to toggle source
# File lib/adsl/util/general.rb, line 109
def lookup_or_create_module(name)
  lookup_container = self
  name.to_s.split('::').each do |portion|
    portion = 'Object' if portion.empty?
    unless lookup_container.const_defined? portion
      new_module = Module.new
      lookup_container.const_set portion, new_module
    end
    lookup_container = lookup_container.const_get portion 
  end
  lookup_container
end
parent_module() click to toggle source
# File lib/adsl/util/general.rb, line 95
def parent_module
  name.split('::')[0..-2].join('::').constantize
end
to_sexp() click to toggle source
# File lib/adsl/extract/sexp_utils.rb, line 46
def to_sexp
  parts = self.name.split('::')
  sexp = s(:colon3, parts.shift.to_sym)
  parts.each do |part|
    sexp = s(:colon2, sexp, part.to_sym)
  end
  sexp
end