Class: AMEE::DataAbstraction::Usage

Inherits:
Input
  • Object
show all
Defined in:
lib/amee-data-abstraction/usage.rb

Overview

Subclass of Input providing methods and attributes appropriate for representing adjustable calculation usages specifically.

Only one instance of Usage can be assocaited with a particular calucaltion object. When the value of self is changed, profile item value terms which are forbidden in the new usage will be inactivated and optional/compulsory flags are set on the remaining terms.

Constant Summary

Constants inherited from Term

Term::Interfaces, Term::UnitFields

Instance Attribute Summary

Attributes inherited from Input

#dirty

Attributes inherited from Term

#parent, #value_before_cast

Instance Method Summary collapse

Methods inherited from Input

#choice_validation_message, #compulsory!, #compulsory?, #dirty?, #disabled?, #fixed, #fixed?, #invalid, #optional!, #optional?, #options_for_select, #validate!, #validation, #validation_message

Methods inherited from Term

#==, #after?, #before?, #convert_unit, convert_value_to_type, #disable!, #disabled?, #enable!, #enabled?, #has_numeric_value?, #hidden?, #hide!, #initialize_copy, #inspect, #interface, #note, #set?, #show!, #to_quantity, #to_s, #unset?, validate_dimensional_equivalence?, #visible?

Constructor Details

#initialize(options = {}, &block) ⇒ Usage

Initialization of Usage objects follows that of the parent Input class, with a number of differences.

If the parent caluclation already contains a usage term, a TwoUsages exception is raised.

The label<tt> attribute is set by default to <tt>:usage.

The interface attribute of self is set to :drop_down by default, but can be manually configured if required.

The inactive property of self is set to :invisible by default.



34
35
36
37
38
39
40
# File 'lib/amee-data-abstraction/usage.rb', line 34

def initialize(options={},&block)
  raise Exceptions::TwoUsages if options[:parent].current_usage
  label :usage
  @inactive=:invisible
  super
  interface :drop_down unless interface
end

Instance Method Details

#activate_selected(usage = nil) ⇒ Object

Activate and deactivate terms in the parent calculation according to the compulsory/optional/forbidden status’ of each in the usage indicated by usage



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/amee-data-abstraction/usage.rb', line 65

def activate_selected(usage=nil)
  parent.profiles.in_use(usage).each do |term|
    case @inactive
    when :invisible
      term.show!
    when :disabled
      term.enable!
    end
  end
  parent.profiles.out_of_use(usage).each do |term|
    case @inactive
    when :invisible
      term.hide!
    when :disabled
      term.disable!
    end
  end
end

#choicesObject

Returns an array of available valid values for self.



85
86
87
# File 'lib/amee-data-abstraction/usage.rb', line 85

def choices
  parent.amee_usages
end

#value(*args) ⇒ Object

Adjust the value of self indicating that a new usage should be switch to in the parent caluclation. This method has the effect of (de)activating terms in the parent calculation as appropriate.



53
54
55
56
57
58
59
# File 'lib/amee-data-abstraction/usage.rb', line 53

def value(*args)
  unless args.empty?
    @value=args.first
    activate_selected(value)
  end
  super
end