Sha256: f8ad84ce61c64a122526ebd0cf50d697c636cc690a54141aa869146fed9f3ff7

Contents?: true

Size: 1.65 KB

Versions: 6

Compression:

Stored size: 1.65 KB

Contents

class Roda
  class Component
    class Events < Struct.new(:klass, :component_opts, :scope)
      def on name, options = {}, &block
        limit_if = options.delete(:if) || []
        limit_if = [limit_if] unless limit_if.is_a? Array

        class_name   = options.delete(:for) || klass._name
        class_events = (events[class_name] ||= {})
        event        = (class_events[name.to_s] ||= [])

        # remove the type, if we have an on if and it isn't in the engine_type
        if limit_if.any? && !limit_if.include?(engine_type.to_sym)
          block = Proc.new {}
        end
        event << [block, klass._name, options]
      end

      def trigger name, options = {}
        content = ''

        events[klass._name][name.to_s].each do |event|
          block, comp, _ = event

          response = Component::Instance.new(component(comp), scope).instance_exec options, &block

          if response.is_a? Roda::Component::DOM
            content = response.to_html
          elsif response.is_a? String
            content = response
          end
        end

        content
      end

      private

      def component comp
        if server?
          Object.const_get(component_opts[:class_name][comp]).new scope
        else
          component_opts[:comp][comp]
        end
      end

      def events
        component_opts[:events]
      end

      def engine_type
        RUBY_ENGINE == 'ruby' ? 'server' : 'client'
      end

      def server?
        RUBY_ENGINE == 'ruby' ? 'server' : false
      end
      alias :server :server?

      def client?
        RUBY_ENGINE == 'opal' ? 'client' : false
      end
      alias :client :client?
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
roda-component-0.0.10 lib/roda/component/events.rb
roda-component-0.0.9 lib/roda/component/events.rb
roda-component-0.0.8 lib/roda/component/events.rb
roda-component-0.0.7 lib/roda/component/events.rb
roda-component-0.0.6 lib/roda/component/events.rb
roda-component-0.0.5 lib/roda/component/events.rb