Sha256: fb8ce34c6a5aaac76b38221ecd0063164db1a6800203a5e3738531802c616e91

Contents?: true

Size: 1.43 KB

Versions: 12

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

require "forwardable"
require "set"

require "pakyow/support/safe_string"

require "pakyow/presenter/attributes/attribute"

module Pakyow
  module Presenter
    class Attributes
      # Wraps the value for a set-type view attribute (e.g. class).
      #
      # Behaves just like a normal +Set+.
      #
      class Set < Attribute
        VALUE_SEPARATOR = " ".freeze

        extend Forwardable
        def_delegators :@value, :to_a, :any?, :empty?, :clear

        include Support::SafeStringHelpers

        def include?(value)
          @value.include?(value.to_s)
        end

        def <<(value)
          @value << ensure_html_safety(value)
        end

        def add(value)
          @value.add(ensure_html_safety(value))
        end

        def delete(value)
          @value.delete(value.to_s)
        end

        def to_s
          @value.to_a.join(VALUE_SEPARATOR)
        end

        class << self
          include Support::SafeStringHelpers

          def parse(value)
            if value.is_a?(Array) || value.is_a?(::Set)
              new(::Set.new(value.map { |v| ensure_html_safety(v) }))
            elsif value.respond_to?(:to_s)
              new(::Set.new(value.to_s.split(VALUE_SEPARATOR).map { |v| ensure_html_safety(v) }))
            else
              raise ArgumentError.new("expected value to be an Array, Set, or String")
            end
          end
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
pakyow-presenter-1.0.6 lib/pakyow/presenter/attributes/set.rb
pakyow-presenter-1.0.5 lib/pakyow/presenter/attributes/set.rb
pakyow-presenter-1.0.4 lib/pakyow/presenter/attributes/set.rb
pakyow-presenter-1.0.3 lib/pakyow/presenter/attributes/set.rb
pakyow-presenter-1.0.2 lib/pakyow/presenter/attributes/set.rb
pakyow-presenter-1.0.1 lib/pakyow/presenter/attributes/set.rb
pakyow-presenter-1.0.0 lib/pakyow/presenter/attributes/set.rb
pakyow-presenter-1.0.0.rc5 lib/pakyow/presenter/attributes/set.rb
pakyow-presenter-1.0.0.rc4 lib/pakyow/presenter/attributes/set.rb
pakyow-presenter-1.0.0.rc3 lib/pakyow/presenter/attributes/set.rb
pakyow-presenter-1.0.0.rc2 lib/pakyow/presenter/attributes/set.rb
pakyow-presenter-1.0.0.rc1 lib/pakyow/presenter/attributes/set.rb