Sha256: 05398f0e2db036f0908fdf8dc8c57912bd22d2e41e26ad32d4638e769035a2df

Contents?: true

Size: 1.25 KB

Versions: 10

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

module Pakyow
  module Presenter
    # @api private
    class BindingParts
      def initialize
        @parts = {}
      end

      def define_part(name, block)
        @parts[name] = block
      end

      def content?
        @parts.include?(:content)
      end

      def content(view)
        @parts[:content].call(view.text)
      end

      def values(view)
        values_for_parts(@parts, view)
      end

      def non_content_values(view)
        values_for_parts(@parts.reject { |name, _|
          name == :content
        }, view)
      end

      def reject(*parts)
        parts = parts.map(&:to_sym)
        @parts.delete_if { |key, _| parts.include? key }
      end

      def accept(*parts)
        return if parts.empty?
        parts = parts.map(&:to_sym)
        @parts.keep_if { |key, _| parts.include? key }
      end

      def to_json(*)
        @parts.to_json
      end

      private

      def values_for_parts(parts, view)
        Hash[parts.map { |name, block|
          value = if block.arity == 0
            block.call
          else
            view.attrs[name].tap do |current_value|
              block.call(current_value)
            end
          end

          [name, value]
        }]
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
pakyow-presenter-1.0.6 lib/pakyow/presenter/binding_parts.rb
pakyow-presenter-1.0.5 lib/pakyow/presenter/binding_parts.rb
pakyow-presenter-1.0.4 lib/pakyow/presenter/binding_parts.rb
pakyow-presenter-1.0.3 lib/pakyow/presenter/binding_parts.rb
pakyow-presenter-1.0.2 lib/pakyow/presenter/binding_parts.rb
pakyow-presenter-1.0.1 lib/pakyow/presenter/binding_parts.rb
pakyow-presenter-1.0.0 lib/pakyow/presenter/binding_parts.rb
pakyow-presenter-1.0.0.rc5 lib/pakyow/presenter/binding_parts.rb
pakyow-presenter-1.0.0.rc4 lib/pakyow/presenter/binding_parts.rb
pakyow-presenter-1.0.0.rc3 lib/pakyow/presenter/binding_parts.rb