Class: AMEE::DataAbstraction::Input

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

Overview

Subclass of Term providing methods and attributes appropriate for representing calculation inputs specifically

Direct Known Subclasses

Drill, Metadatum, Profile, Usage

Constant Summary

Constants inherited from Term

Term::Interfaces, Term::UnitFields

Instance Attribute Summary collapse

Attributes inherited from Term

#parent, #value_before_cast

Instance Method Summary collapse

Methods inherited from Term

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

Constructor Details

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

Initialization of Input objects follows that of the parent Term class.



30
31
32
33
34
35
# File 'lib/amee-data-abstraction/input.rb', line 30

def initialize(options={},&block)
  @validation = nil
  validation_message {"#{name} is invalid."}
  super
  @dirty = false
end

Instance Attribute Details

#dirtyObject

Returns the value of attribute dirty.



14
15
16
# File 'lib/amee-data-abstraction/input.rb', line 14

def dirty
  @dirty
end

Instance Method Details

#choice_validation_messageObject

Set a default validation message appropriate for input terms which have a list of choices.



53
54
55
# File 'lib/amee-data-abstraction/input.rb', line 53

def choice_validation_message
  validation_message {"#{name} is invalid because #{value} is not one of #{choices.join(', ')}."}
end

#choicesObject

Returns the valid choices for this input (Abstract, implemented only for subclasses of input.)

Raises:

  • (NotImplementedError)


18
19
20
# File 'lib/amee-data-abstraction/input.rb', line 18

def choices
  raise NotImplementedError
end

#clean!Object



205
206
207
# File 'lib/amee-data-abstraction/input.rb', line 205

def clean!
  @dirty = false
end

#compulsory!Object

Manually set the term as compuslory



167
168
169
# File 'lib/amee-data-abstraction/input.rb', line 167

def compulsory!
  @optional=false
end

#compulsory?(usage = nil) ⇒ Boolean

Returns true if the value of self is required in order for the parent calculation to calculate a result. Otherwise, returns false

Returns:

  • (Boolean)


157
158
159
# File 'lib/amee-data-abstraction/input.rb', line 157

def compulsory?(usage=nil)
  !optional?(usage)
end

#dirty?Boolean

Returns:

  • (Boolean)


201
202
203
# File 'lib/amee-data-abstraction/input.rb', line 201

def dirty?
  @dirty
end

#disabled?Boolean

Returns true if the UI element of self is disabled. Otherwise, returns false.

Returns:

  • (Boolean)


197
198
199
# File 'lib/amee-data-abstraction/input.rb', line 197

def disabled?
  super || fixed?
end

#fixed(val) ⇒ Object

Configures the value of self to be fixed to val, i.e. the value is read-only.



40
41
42
43
44
# File 'lib/amee-data-abstraction/input.rb', line 40

def fixed val
  value(val)
  @fixed=true
  @optional=false
end

#fixed?Boolean

Returns true if self is configured to contain a fixed (read-only) value

Returns:

  • (Boolean)


141
142
143
# File 'lib/amee-data-abstraction/input.rb', line 141

def fixed?
  @fixed
end

#invalidObject

Declare the calculation invalid, reporting to the parent calculation or raising an exception, as appropriate.



186
187
188
189
190
191
192
# File 'lib/amee-data-abstraction/input.rb', line 186

def invalid
  if parent
    parent.invalid(label,instance_eval(&@validation_block))
  else
    raise AMEE::DataAbstraction::Exceptions::ChoiceValidation.new(instance_eval(&@validation_block))
  end
end

#optional!Object

Manually set the term as optional



162
163
164
# File 'lib/amee-data-abstraction/input.rb', line 162

def optional!
  @optional=true
end

#optional?(usage = nil) ⇒ Boolean

Returns true if the value of self does not need to be specified for the parent calculation to calculate a result. Otherwise, returns false

Returns:

  • (Boolean)


149
150
151
# File 'lib/amee-data-abstraction/input.rb', line 149

def optional?(usage=nil)
  @optional
end

#options_for_selectObject

Returns an ppropriate data structure for a rails select list form helper.



23
24
25
# File 'lib/amee-data-abstraction/input.rb', line 23

def options_for_select
  [[nil,nil]]+choices.map{|x|[x.underscore.humanize,x] unless x.nil? }.compact
end

#validate!Object

Check that the value of self is valid. If invalid, and is defined as part of a calculation, add the invalidity message to the parent calculation’s error list. Otherwise, raise a ChoiceValidation exception.



176
177
178
179
180
181
# File 'lib/amee-data-abstraction/input.rb', line 176

def validate!
  # Typically, you just wipe yourself if supplied value not valid, but
  # deriving classes might want to raise an exception
  #
  invalid unless fixed? || valid?
end

#validation(*args) ⇒ Object

Represents a custom object, symbol or pattern (to be called via ===) to determine the whether value set for self should be considered acceptable. The following symbols are acceptable :numeric, :date or :datetime If validation is specified using a Proc object, the term value should be initialized as the block variable. E.g.,

my_input.validation 20

my_input.valid?             #=> true

my_input.value 'some string'
my_input.valid?             #=> false

my_input.value 21
my_input.valid?             #=> false

my_input.value 20
my_input.valid?             #=> true

---

my_input.validation lambda{ |value| value.is_a? Numeric }

my_input.valid?             #=> true

my_input.value 'some string'
my_input.valid?             #=> false

my_input.value 12345
my_input.valid?             #=> true

---

my_input.validation :numeric

my_input.valid?             #=> false

my_input.value 21
my_input.valid?             #=> true

my_input.value "20"
my_input.valid?             #=> true

my_input.value "e"
my_input.valid?             #=> false


123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/amee-data-abstraction/input.rb', line 123

def validation(*args)
  unless args.empty?
    if args.first.is_a?(Symbol)
      @validation = case args.first
        when :numeric then lambda{|v| v.is_a?(Fixnum) || v.is_a?(Integer) || v.is_a?(Float) || v.is_a?(BigDecimal) || (v.is_a?(String) && v.match(/^[\d\.]+$/))}
        when :date then lambda{|v| v.is_a?(Date) || v.is_a?(DateTime) || Date.parse(v) rescue nil}
        when :datetime then lambda{|v| v.is_a?(Time) || v.is_a?(DateTime) || DateTime.parse(v) rescue nil}
      end
    else
      @validation = args.first
    end
  end
  @validation
end

#validation_message(&block) ⇒ Object

Block to define custom complaint message for an invalid value.



47
48
49
# File 'lib/amee-data-abstraction/input.rb', line 47

def validation_message(&block)
  @validation_block=block
end

#value(*args) ⇒ Object

Represents the value of self. Set a value by passing an argument. Retrieve a value by calling without an argument, e.g.,

my_term.value 12345

my_term.value                      #=> 12345

If self is configured to have a fixed value and a value is passed which does not correspond to the fixed value, a FixedValueInterference exception is raised.



68
69
70
71
72
73
74
75
76
# File 'lib/amee-data-abstraction/input.rb', line 68

def value(*args)
  unless args.empty?
    if args.first.to_s != @value.to_s
      raise Exceptions::FixedValueInterference if fixed?
      mark_as_dirty
    end
  end
  super
end