Sha256: 2288360fcbbca6327716ef1e6ccbf524b010f5c1337f488157f46821d00c5cac

Contents?: true

Size: 995 Bytes

Versions: 2

Compression:

Stored size: 995 Bytes

Contents

require 'movingsign_api/commands/internal/utilities'

module MovingsignApi
  module PrettyKeyable
    include Utilities

    # @!visibility private
    def self.included(other)
      other.class_variable_set(:@@KEYS, [])
      other.class_variable_set(:@@CODES, [])

      other.extend ClassMethods
    end

    # @!visibility private
    def to_bytes
      string_to_ascii_bytes self.class.codes[self.class.keys.index(self.key)]
    end

    private

    module ClassMethods
      # registers the specified key and code pair as synonymous
      def register(key, code)
        keys << key
        codes << code

        define_singleton_method(key) do
          key
        end
      end

      def parse_to_key(input)
        if index = (keys.index(input) || codes.index(input))
          keys[index]
        else
          nil
        end
      end

      def keys
        class_variable_get(:@@KEYS)
      end

      def codes
        class_variable_get(:@@CODES)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
movingsign_api-0.0.2 lib/movingsign_api/commands/internal/pretty_keyable.rb
movingsign_api-0.0.1 lib/movingsign_api/commands/internal/pretty_keyable.rb