Sha256: 79e0c01b619796c965365bb30a3ae3e3c0d409abbf71b899e5f3b5a8c5284be0

Contents?: true

Size: 2 KB

Versions: 4

Compression:

Stored size: 2 KB

Contents

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

  def after_initialize
    self.options ||= HashWithIndifferentAccess.new
  end

  def value
    r = super
    return r.to_s if r.kind_of? Symbol
    r
  end

  # See +Rtml::Dom::Element#lookup_tml_id(name)+
  def self.lookup_tml_id(name)
    Rtml::Dom::Element.lookup_tml_id(name)
  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 #lookup_tml_id.
  # 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
    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)}"
      end
    end
    val
  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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rtml-2.0.3 builtin/models/rtml/dom/property.rb
rtml-2.0.2 builtin/models/rtml/dom/property.rb
rtml-2.0.1 builtin/models/rtml/dom/property.rb
rtml-2.0.0.alpha.1 builtin/models/rtml/dom/property.rb