Class: RGen::Environment
- Inherits:
-
Object
- Object
- RGen::Environment
- Defined in:
- lib/rgen/environment.rb
Overview
An Environment is used to hold model elements.
Instance Method Summary collapse
-
#<<(el) ⇒ Object
Add a model element.
-
#delete(el) ⇒ Object
Removes model element from environment.
-
#each(&b) ⇒ Object
Iterates each element.
-
#elements ⇒ Object
Return the elements of the environment as an array.
-
#find(desc) ⇒ Object
Finds and returns model elements in the environment.
-
#initialize ⇒ Environment
constructor
A new instance of Environment.
-
#new(clazz, *args) ⇒ Object
This method can be used to instantiate a class and automatically put it into the environment.
Constructor Details
#initialize ⇒ Environment
Returns a new instance of Environment.
7 8 9 10 11 |
# File 'lib/rgen/environment.rb', line 7 def initialize @elements = {} @subClasses = {} @subClassesUpdated = {} end |
Instance Method Details
#<<(el) ⇒ Object
Add a model element. Returns the environment so <<
can be chained.
15 16 17 18 19 20 21 |
# File 'lib/rgen/environment.rb', line 15 def <<(el) clazz = el.class @elements[clazz] ||= [] @elements[clazz] << el updateSubClasses(clazz) self end |
#delete(el) ⇒ Object
Removes model element from environment.
24 25 26 27 |
# File 'lib/rgen/environment.rb', line 24 def delete(el) return unless @elements[el.class] @elements[el.class].delete(el) end |
#each(&b) ⇒ Object
Iterates each element
31 32 33 |
# File 'lib/rgen/environment.rb', line 31 def each(&b) @elements.values.flatten.each(&b) end |
#elements ⇒ Object
Return the elements of the environment as an array
37 38 39 |
# File 'lib/rgen/environment.rb', line 37 def elements @elements.values.flatten end |
#find(desc) ⇒ Object
Finds and returns model elements in the environment.
The search description argument must be a hash specifying attribute/value pairs. Only model elements are returned which respond to the specified attribute methods and return the specified values as result of these attribute methods.
As a special hash key :class can be used to look for model elements of a specific class. In this case an array of possible classes can optionally be given.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/rgen/environment.rb', line 59 def find(desc) result = [] classes = desc[:class] if desc[:class] and desc[:class].is_a?(Array) classes = [ desc[:class] ] if !classes and desc[:class] if classes hashKeys = classesWithSubClasses(classes) else hashKeys = @elements.keys end hashKeys.each do |clazz| next unless @elements[clazz] @elements[clazz].each do |e| failed = false desc.each_pair { |k,v| failed = true if k != :class and ( !e.respond_to?(k) or e.send(k) != v ) } result << e unless failed end end result end |
#new(clazz, *args) ⇒ Object
This method can be used to instantiate a class and automatically put it into the environment. The new instance is returned.
44 45 46 47 48 |
# File 'lib/rgen/environment.rb', line 44 def new(clazz, *args) obj = clazz.new(*args) self << obj obj end |