Sha256: d50b008e079ca23f62930681ef2a46f7a51fdfb3419db013417c750fca3d4e4d

Contents?: true

Size: 1.24 KB

Versions: 35

Compression:

Stored size: 1.24 KB

Contents

module Aptible
  module CLI
    module Formatter
      class GroupedKeyedList < KeyedList
        attr_reader :group

        class InvalidGroup
          def initialize(group)
            m = 'group argument must be a string or a hash with one key ' \
                "and a string value. #{group} is invalid"
            super(m)
          end
        end

        def initialize(group, y)
          @group = group
          validate_group!
          super(y)
        end

        def groups
          children.group_by(&grouper)
        end

        private

        def grouper
          case group
          when String
            lambda do |n|
              n.children.fetch(group)
            end
          when Hash
            first, second = group.to_a.first
            lambda do |n|
              n.children.fetch(first).children.fetch(second)
            end
          end
        end

        def validate_group!
          return if group.is_a?(String)
          if group.is_a?(Hash)
            keys = group.keys
            raise InvalidGroup, group if keys.size != 1
            raise InvalidGroup, group unless group[keys.first].is_a?(String)
            return
          end
          raise InvalidGroup(group)
        end
      end
    end
  end
end

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
aptible-cli-0.24.2 lib/aptible/cli/formatter/grouped_keyed_list.rb
aptible-cli-0.24.1 lib/aptible/cli/formatter/grouped_keyed_list.rb
aptible-cli-0.24.0 lib/aptible/cli/formatter/grouped_keyed_list.rb
aptible-cli-0.23.0 lib/aptible/cli/formatter/grouped_keyed_list.rb
aptible-cli-0.22.0 lib/aptible/cli/formatter/grouped_keyed_list.rb
aptible-cli-0.21.0 lib/aptible/cli/formatter/grouped_keyed_list.rb
aptible-cli-0.20.0 lib/aptible/cli/formatter/grouped_keyed_list.rb
aptible-cli-0.19.9 lib/aptible/cli/formatter/grouped_keyed_list.rb
aptible-cli-0.19.7 lib/aptible/cli/formatter/grouped_keyed_list.rb
aptible-cli-0.19.6 lib/aptible/cli/formatter/grouped_keyed_list.rb
aptible-cli-0.19.4 lib/aptible/cli/formatter/grouped_keyed_list.rb
aptible-cli-0.19.3 lib/aptible/cli/formatter/grouped_keyed_list.rb
aptible-cli-0.19.2 lib/aptible/cli/formatter/grouped_keyed_list.rb
aptible-cli-0.19.1 lib/aptible/cli/formatter/grouped_keyed_list.rb
aptible-cli-0.19.0 lib/aptible/cli/formatter/grouped_keyed_list.rb
aptible-cli-0.18.3 lib/aptible/cli/formatter/grouped_keyed_list.rb
aptible-cli-0.18.2 lib/aptible/cli/formatter/grouped_keyed_list.rb
aptible-cli-0.18.1 lib/aptible/cli/formatter/grouped_keyed_list.rb
aptible-cli-0.18.0 lib/aptible/cli/formatter/grouped_keyed_list.rb
aptible-cli-0.17.2 lib/aptible/cli/formatter/grouped_keyed_list.rb