Class: Module

Inherits:
Object show all
Defined in:
lib/adsl/util/general.rb,
lib/adsl/extract/sexp_utils.rb

Instance Method Summary collapse

Instance Method Details

#container_for(*fields, &block) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# 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) ⇒ Object



99
100
101
102
103
104
105
106
107
# 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) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# 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) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
# 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_moduleObject



95
96
97
# File 'lib/adsl/util/general.rb', line 95

def parent_module
  name.split('::')[0..-2].join('::').constantize
end

#to_sexpObject



46
47
48
49
50
51
52
53
# 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