builtin/models/rtml/dom/property.rb in rtml-2.0.3 vs builtin/models/rtml/dom/property.rb in rtml-2.0.4

- old
+ new

@@ -1,27 +1,30 @@ -class Rtml::Dom::Property < ActiveRecord::Base - set_table_name 'rtml_dom_properties' - serialize :options, HashWithIndifferentAccess - serialize :value - belongs_to :element, :class_name => 'Rtml::Dom::Element' - delegate :document, :to => :element - validates_presence_of :name - validates_uniqueness_of :name, :scope => :element_id +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 after_initialize - self.options ||= HashWithIndifferentAccess.new + def document + element ? element.document : nil end - def value - r = super - return r.to_s if r.kind_of? Symbol - r + 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 - # See +Rtml::Dom::Element#lookup_tml_id(name)+ - def self.lookup_tml_id(name) - Rtml::Dom::Element.lookup_tml_id(name) + def value + @value.kind_of?(Symbol) ? @value.to_s : @value end def to_s value.to_s end @@ -33,24 +36,38 @@ 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 #lookup_tml_id. + # 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' - val = tml_valid(self.value) - val = val.to_s if val.kind_of?(Symbol) or val.kind_of?(Numeric) - val = self.class.lookup_tml_id(value) if name == 'id' - if id_ref && element && document - match = document.screens.select { |s| s.property(:id).to_s == val.to_s }.first - if match - val = "##{self.class.lookup_tml_id(val)}" + + 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 - val end # Produces the TML for this property. def to_tml val = tml_value