Sha256: 8fd18141ab419af68f11757f5b5fffe3ad0065d45ef1e2e41af4512446e63d57

Contents?: true

Size: 1.36 KB

Versions: 7

Compression:

Stored size: 1.36 KB

Contents

module Heirloom
  module CLI
    module Formatter
      class Show

        def format(args)
          @all        = args[:all]
          @attributes = args[:attributes]
          format_attributes
        end

        private

        def format_attributes
          remove_internal_attributes unless @all
          formated = @attributes.each_pair.map do |key,value|
            "#{padded_key(key)}: #{value}"
          end
          formated.join("\n")
        end

        def padded_key(key)
          max_length ||= longest_attribute_length
          key + (" " * (max_length - key.length + 1))
        end

        def longest_attribute_length
          longest_length = 0
          @attributes.keys.each do |k|
            longest_length = k.length if k.length > longest_length
          end
          longest_length 
        end

        def remove_internal_attributes
          @attributes.delete_if { |key| is_internal_attribute? key }
        end

        def is_internal_attribute?(attribute)
          return true if is_reserved? attribute
          return true if is_endpoint? attribute
          false
        end

        def is_reserved?(attribute)
          ['bucket_prefix', 'built_at', 'built_by'].include? attribute
        end
        
        def is_endpoint?(attribute)
          attribute.match('^.*-.*-\d*-s3|http|https-url$')
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
heirloom-0.10.1 lib/heirloom/cli/formatter/show.rb
heirloom-0.10.0 lib/heirloom/cli/formatter/show.rb
heirloom-0.9.0 lib/heirloom/cli/formatter/show.rb
heirloom-0.8.3 lib/heirloom/cli/formatter/show.rb
heirloom-0.8.2 lib/heirloom/cli/formatter/show.rb
heirloom-0.8.1 lib/heirloom/cli/formatter/show.rb
heirloom-0.8.0 lib/heirloom/cli/formatter/show.rb