Sha256: 183e52e735c8dd4c78e97ac53160ae72343f6bfc8a53ccf9170db3758a28108d

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 KB

Contents

class Rtml::Widgets::ScreenVariableProcessing < Rtml::Widget
  include Rtml::TmlizedConditions
  affects :screen
  entry_point :set

  # Sets one or more TML variables to specified values.
  # Examples:
  #   screen :main do
  #     set :counter => 'tmlvar:counter + 1',
  #         :amount => 100
  #   end
  #
  def set(assignments)
    variables = parent.document.variable_definitions(:all).collect { |c| c[:name] }
    format = assignments.delete(:format)

    assignments.each do |key, value|
      if variables.include?(key.to_s)
        options = options_for(value_or_condition(value)).merge(:name => key, :format => format)
        #options.each { |key, value| options.delete(key) if value.nil? }
        build :setvar, validate_options(options)
      else
        raise Rtml::Errors::InvalidOptionError, "Variable named #{key} is not defined"
      end
    end
  end

  private
  def validate_options(hash)
    hash.each do |key, value|
      if key == :lo || key == :ro
        case value
          when Numeric, String, Symbol, TrueClass, FalseClass, NilClass
          else
            raise ArgumentError, "Incompatible type: #{value}"
        end
      end
    end
    hash
  end

  def options_for(value_or_array)
    if value_or_array.kind_of? Array
      value_or_array = tmlize_condition(*value_or_array)
      { :lo => (value_or_array[0]), :op => value_or_array[1], :ro => (value_or_array[2]) }
    else
      { :lo => (value_or_array) }
    end
  end

  def value_or_condition(value)
    if value.kind_of? String
      operands = value.split
      if operands.length == 3
        operands
      else
        value
      end
    else
      value
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rtml-2.0.4 builtin/widgets/screen_variable_processing.rb