lib/amee-data-abstraction/term.rb in amee-data-abstraction-2.1.1 vs lib/amee-data-abstraction/term.rb in amee-data-abstraction-2.2.0
- old
+ new
@@ -82,20 +82,10 @@
#
# my_term.path #=> 'mass'
#
attr_property :path
- # String representing an annotation for <tt>self</tt>. Set a value by
- # passing an argument. Retrieve a value by calling without an argument,
- # e.g.,
- #
- # my_term.note 'Enter the mass of cement produced in the reporting period'
- #
- # my_term.note #=> 'Enter the mass of cement ...'
- #
- attr_property :note
-
# Symbol representing the owning parent calculation of <tt>self</tt>. Set
# the owning calculation object by passing as an argument. Retrieve it by
# calling without an argument, e.g.,
#
# my_calculation = <AMEE::DataAbstraction::OngoingCalculation ... >
@@ -209,10 +199,23 @@
@value_before_cast = args.first
@value = @type ? self.class.convert_value_to_type(args.first, @type) : args.first
end
@value
end
+
+ # String representing an annotation for <tt>self</tt>. Set a value by
+ # passing an argument. Retrieve a value by calling without an argument,
+ # e.g.,
+ #
+ # my_term.note 'Enter the mass of cement produced in the reporting period'
+ #
+ # my_term.note #=> 'Enter the mass of cement ...'
+ #
+ def note(string=nil)
+ instance_variable_set("@note",string.gsub('"',"'")) unless string.nil?
+ instance_variable_get("@note")
+ end
# Symbols representing the attributes of <tt>self</tt> which are concerned
# with quantity units.
#
# Each symbol also represents <b>dynamically defined method<b> name for
@@ -262,23 +265,37 @@
instance_variable_get("@#{field}")
end
end
[:unit,:per_unit].each do |field|
+
+ # If no argument provided, returns the alternative units which are valid
+ # for <tt>self</tt>. If a list of units are provided as an argument, these
+ # override the dynamically assigned alternative units for <tt>self</tt>.
+ #
define_method("alternative_#{field}s") do |*args|
ivar = "@alternative_#{field}s"
- default = send("default_#{field}".to_sym)
unless args.empty?
- args << default if default
units = args.map {|arg| Unit.for(arg) }
Term.validate_dimensional_equivalence?(*units)
instance_variable_set(ivar, units)
else
return instance_variable_get(ivar) if instance_variable_get(ivar)
- return instance_variable_set(ivar, (default.alternatives << default)) if default
+ default = send("default_#{field}".to_sym)
+ return instance_variable_set(ivar, (default.alternatives)) if default
end
end
+
+ # Returns the list of unit choices for <tt>self</tt>, including both the
+ # default unit and all alternative units.
+ #
+ define_method("#{field}_choices") do |*args|
+ choices = send("alternative_#{field}s".to_sym)
+ default = send("default_#{field}".to_sym)
+ choices = [default] + choices if default
+ return choices
+ end
end
# Returns <tt>true</tt> if <tt>self</tt> has a populated value attribute.
# Otherwise, returns <tt>false</tt>.
#
@@ -501,8 +518,9 @@
when :time then Time.parse(value.to_s) rescue nil
when :date then Date.parse(value.to_s) rescue nil
else value
end
end
+
end
end
end
\ No newline at end of file