Sha256: a85219fa9bd8b34ee6a785897c0fdfee04b7301f2b0ee49190dc29f4e45964a0

Contents?: true

Size: 1.01 KB

Versions: 7

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

module Kind
  module Enum
    METHODS = \
    <<-RUBY
    def to_h
      ENUM__HASH
    end

    def items
      ENUM__ITEMS
    end

    def ===(arg)
      ENUM__ITEMS.any? { |item| item === arg }
    end

    def refs
      ENUM__REFS.to_a
    end

    def keys
      ENUM__KEYS.to_a
    end

    def values
      ENUM__VALS.to_a
    end

    def ref?(arg)
      ENUM__REFS.include?(arg)
    end

    def key?(arg)
      arg.respond_to?(:to_sym) ? ref?(arg) && !value?(arg) : false
    end

    def value?(arg)
      ENUM__VALS.include?(arg)
    end

    def [](arg)
      return arg if ref?(arg)

      raise KeyError, "key or value not found: %p" % [arg]
    end

    def ref(arg)
      arg if ref?(arg)
    end

    def item(arg)
      ENUM__MAP[arg]
    end

    def key(arg)
      item(arg).key if value?(arg)
    end

    def value_at(arg)
      item(arg).value if key?(arg)
    end

    def self.included(base)
      base.extend(base)
    end
    RUBY

    private_constant :METHODS
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
kind-5.7.0 lib/kind/enum/methods.rb
kind-5.6.0 lib/kind/enum/methods.rb
kind-5.5.0 lib/kind/enum/methods.rb
kind-5.4.1 lib/kind/enum/methods.rb
kind-5.4.0 lib/kind/enum/methods.rb
kind-5.3.0 lib/kind/enum/methods.rb
kind-5.2.0 lib/kind/enum/methods.rb