Sha256: 1795ff427fa3b0c43abc504c60d633341633eb0a2d9b97accb90938df87be3a7

Contents?: true

Size: 1.34 KB

Versions: 3

Compression:

Stored size: 1.34 KB

Contents

require_relative 'mutation_set'
require_relative 'mutate_context'

module Pakyow
  module UI
    # Performs mutations on views.
    #
    # @api private
    class Mutator
      include Singleton

      attr_reader :sets

      # @api private
      def initialize
        reset
      end

      def reset
        @sets = {}
        @mutables = {}
        self
      end

      def set(scope, &block)
        @sets[scope] = MutationSet.new(&block)
      end

      def mutable(scope, context = nil, &block)
        if block_given?
          @mutables[scope] = block
        else
          # TODO: inefficient to have to execute the block each time
          Mutable.new(context, scope, &@mutables[scope])
        end
      end

      def mutation(scope, name)
        if mutations = mutations_by_scope(scope)
          mutations.mutation(name)
        end
      end

      # TODO: rename to mutation_set_for_scope
      def mutations_by_scope(scope)
        @sets[scope]
      end

      def mutate(mutation_name, view, data)
        if mutation = mutation(view.scoped_as, mutation_name)
          if data.is_a?(MutableData)
            working_data = data.data
          else
            working_data = data
          end

          result = mutation[:fn].call(view, working_data)
          MutateContext.new(mutation, result, data)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pakyow-ui-0.10.2 pakyow-ui/lib/pakyow-ui/mutator.rb
pakyow-ui-0.10.1 pakyow-ui/lib/pakyow-ui/mutator.rb
pakyow-ui-0.10.0 pakyow-ui/lib/pakyow-ui/mutator.rb