Sha256: 3a87f41d11b16137182911fe4b031b31e5221de511fbe842af0739aa0ce338c2

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 KB

Contents

require "rubygems"
require "bundler/setup"

require 'ruby-event'

module RubyApp
  
  class Element
    
    class Event
      attr_reader :source

      def initialize(data = nil)
        @source = data ? RubyApp::Element.get_element(data['source_id']) : nil
        @statements = []
      end
      
      def alert(message)
        execute("RubyApp.alert('#{message.tr('`', '\"').tr('\'','\"')}');")
      end
      
      def refresh
        execute("RubyApp.refresh();")
      end
      
      def show_dialog(dialog)
        execute("RubyApp.showDialog('#{dialog.element_id}');")
      end
      
      def hide_dialog(dialog)
        execute("RubyApp.hideDialog('#{dialog.element_id}');")
      end
      
      def update(element)
        execute("RubyApp.update('#{element.element_id}');")
      end
      
      def execute(statement)
        @statements << statement
      end
      
      def self.from_hash(data)
        eval(data['_class']).new(data)
      end
      
      def to_hash
        {
          '_class' => self.class.to_s,
          'source_id' => @source ? @source.element_id : nil,
          'statements' => @statements
        }
      end

    end
    
    class ExceptionEvent < RubyApp::Element::Event

      def initialize(exception)
        super()
        self.alert(exception.message)
        self.refresh
      end

    end

    require 'rubyapp/mixins/render'
    require 'rubyapp/mixins/translate'
    
    extend RubyApp::Mixins::Translate
    extend RubyApp::Mixins::Render
    include RubyApp::Mixins::Render
    
    template_path(__FILE__)
    
    attr_reader :attributes
    
    event :evented
    
    def initialize
      @attributes = {:id => element_id}
    end
    
    def element_id
      "id_#{object_id}"
    end
    
    def self.get_element(element_id)
      element_id =~ /(\d+)/
      ObjectSpace._id2ref($1.to_i)
    end
    
    protected 
    
      def on_event(event)
        evented(event)
      end
    
  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
_rubyapp-0.0.3 lib/rubyapp/element.rb