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 12 13 |
# File 'lib/rgen/environment.rb', line 7 def initialize @elements = {} @subClasses = {} @subClassesUpdated = {} @deleted = {} @deletedClasses = {} end |
Instance Method Details
#<<(el) ⇒ Object
Add a model element. Returns the environment so <<
can be chained.
17 18 19 20 21 22 23 |
# File 'lib/rgen/environment.rb', line 17 def <<(el) clazz = el.class @elements[clazz] ||= [] @elements[clazz] << el updateSubClasses(clazz) self end |
#delete(el) ⇒ Object
Removes model element from environment.
26 27 28 29 |
# File 'lib/rgen/environment.rb', line 26 def delete(el) @deleted[el] = true @deletedClasses[el.class] = true end |
#each(&b) ⇒ Object
Iterates each element
33 34 35 36 |
# File 'lib/rgen/environment.rb', line 33 def each(&b) removeDeleted @elements.values.flatten.each(&b) end |
#elements ⇒ Object
Return the elements of the environment as an array
40 41 42 43 |
# File 'lib/rgen/environment.rb', line 40 def elements removeDeleted @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.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/rgen/environment.rb', line 63 def find(desc) removeDeleted 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.
48 49 50 51 52 |
# File 'lib/rgen/environment.rb', line 48 def new(clazz, *args) obj = clazz.new(*args) self << obj obj end |