class Rtml::Dom::Property < Rtml::DocumentModelObject include Rtml::Links #set_table_name 'rtml_dom_properties' #serialize :options, HashWithIndifferentAccess #serialize :value #belongs_to :element, :class_name => 'Rtml::Dom::Element' #validates_presence_of :name #validates_uniqueness_of :name, :scope => :element_id attr_accessor :options, :name, :element attr_writer :value def document element ? element.document : nil end def initialize(options = {}) options.reverse_merge!(:options => HashWithIndifferentAccess.new) self.options = options[:options].with_indifferent_access self.value = options[:value] self.name = options[:name] after_initialize if respond_to?(:after_initialize) end def value @value.kind_of?(Symbol) ? @value.to_s : @value end def to_s value.to_s end def blank? value.blank? end def nil? value.nil? end # Returns the TML counterpart of just the value of this property. # If this is a TML ID property, the return value is a call to Rtml::Dom::Element.find_or_create_tml_id(name). # If this is has the :id_ref option, the return value is a reference to the element with this ID. # In all other cases, the value of this property is returned. def tml_value val = value_or_id(tml_valid(@value)) val.kind_of?(String) ? val : val.to_s end def value_or_id(value) #:nodoc: id_ref = options.key?(:id_ref) ? options[:id_ref] : self.name == 'uri' if self.name == 'id' Rtml::Dom::Element.find_or_create_tml_id(value) else if value.kind_of?(String) if value[0] == ?# id_ref = true value = value[1..-1] else return value unless id_ref end end if document && (screen = document.find_screen(value)) id_for(screen.tml_id) else id_ref && local?(value) ? "##{value}" : value end end end # Produces the TML for this property. def to_tml val = tml_value return val if val.blank? and options[:drop_if_blank] return "" if val.nil? # always drop nils "#{self.name}=#{val.inspect.ljust(15)}" # the ljust is for pretty formatting during my tests and could be removed end # Handles various special-case values, like "true" and "false", which must be converted to 1, 0, and so on. def tml_valid(i) case i when true, :true then 1 when false, :false then 0 else i end end end