Module: Laborantin::Metaprog::Selector

Included in:
Analysis, Scenario
Defined in:
lib/laborantin/core/selector.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#environmentsObject

An array of the Environments that could be loaded from the result directory.



27
28
29
# File 'lib/laborantin/core/selector.rb', line 27

def environments
  @environments
end

#scenariiObject (readonly)

An array of the Scenarii that could be loaded from the result directory.



30
31
32
# File 'lib/laborantin/core/selector.rb', line 30

def scenarii
  @scenarii
end

Class Method Details

.included(klass) ⇒ Object



5
6
7
# File 'lib/laborantin/core/selector.rb', line 5

def self.included(klass)
  klass.extend ClassMethods
end

Instance Method Details

#load_environmentsObject

Will try to load environments and set the @environments variable to an array of all the Environment instance that match the :environments class selector (set with Analysis#select).



41
42
43
44
45
46
# File 'lib/laborantin/core/selector.rb', line 41

def load_environments 
  envs = Laborantin::Environment.scan_resdir('results')
  @environments = envs.select do |env| 
    select_instance?(env, self.class.selectors[:environments])
  end 
end

#load_prior_resultsObject

Load first the environments, then the scenarii.



33
34
35
36
# File 'lib/laborantin/core/selector.rb', line 33

def load_prior_results
  load_environments
  load_scenarii
end

#load_scenariiObject

Same as load_environments, but for Scenario instances and :scenarii selector.



50
51
52
53
54
55
# File 'lib/laborantin/core/selector.rb', line 50

def load_scenarii
  scii = @environments.map{|e| e.populate}.flatten
  @scenarii = scii.select do |sc|
    select_instance?(sc, self.class.selectors[:scenarii])
  end
end

#select_instance?(obj, selector) ⇒ Boolean

Handy test to see if an object (that should be an instance of Environment or Scenario) matches the selector.

Returns:

  • (Boolean)


59
60
61
62
63
64
# File 'lib/laborantin/core/selector.rb', line 59

def select_instance?(obj, selector)
  return true unless selector
  blk = selector[:blk]
  (selector[:klasses].any?{|k| obj.is_a? k} ) and
  (blk ? blk.call(obj) : true)
end