Sha256: 040bd7e8c6fb5df1436474e5d31162a77e24c998dbfda4abd6955bf04942c35d

Contents?: true

Size: 1.42 KB

Versions: 8

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

require 'active_support/hash_with_indifferent_access'
require 'ostruct'

module EacCli
  module Speaker
    class List
      class << self
        def build(list)
          return List.new(hash_to_values(list)) if list.is_a?(::Hash)
          return List.new(array_to_values(list)) if list.is_a?(::Array)

          raise "Invalid list: #{list} (#{list.class})"
        end

        private

        def hash_to_values(list)
          list.map { |key, value| ::OpenStruct.new(key: key, label: key, value: value) }
        end

        def array_to_values(list)
          list.map { |value| ::OpenStruct.new(key: value, label: value, value: value) }
        end
      end

      attr_reader :values

      def initialize(values)
        @values = values.map do |v|
          ::OpenStruct.new(key: to_key(v.key), label: to_label(v.label), value: v.value)
        end
      end

      def valid_labels
        values.map(&:label)
      end

      def valid_value?(value)
        values.any? { |v| v.key == to_key(value) }
      end

      def to_key(value)
        to_label(value).downcase
      end

      def to_label(value)
        value.to_s.strip
      end

      def build_value(value)
        key = to_key(value)
        values.each do |v| # rubocop:disable Style/HashEachMethods
          return v.value if v.key == key
        end
        raise "Value not found: \"#{value}\" (#{values})"
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 3 rubygems

Version Path
eac_cli-0.19.0 lib/eac_cli/speaker/list.rb
avm-tools-0.101.0 vendor/eac_cli/lib/eac_cli/speaker/list.rb
eac_cli-0.18.0 lib/eac_cli/speaker/list.rb
avm-tools-0.100.0 vendor/eac_cli/lib/eac_cli/speaker/list.rb
ehbrs-tools-0.21.0 vendor/eac_cli/lib/eac_cli/speaker/list.rb
avm-tools-0.99.1 vendor/eac_cli/lib/eac_cli/speaker/list.rb
eac_cli-0.17.0 lib/eac_cli/speaker/list.rb
avm-tools-0.99.0 vendor/eac_cli/lib/eac_cli/speaker/list.rb