Sha256: d57e2b40d816e8e2f775f00eb2f37cd2f7ecbf6ce7edeca0a72e29a307ac46ab

Contents?: true

Size: 1.51 KB

Versions: 12

Compression:

Stored size: 1.51 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
          return true if are_permissions? 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

        def are_permissions?(attribute)
          attribute.match('^.*permissions')
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
heirloom-0.12.7 lib/heirloom/cli/formatter/show.rb
heirloom-0.12.5 lib/heirloom/cli/formatter/show.rb
heirloom-0.12.4 lib/heirloom/cli/formatter/show.rb
heirloom-0.12.3 lib/heirloom/cli/formatter/show.rb
heirloom-0.12.2 lib/heirloom/cli/formatter/show.rb
heirloom-0.12.1 lib/heirloom/cli/formatter/show.rb
heirloom-0.12.0 lib/heirloom/cli/formatter/show.rb
heirloom-0.11.2 lib/heirloom/cli/formatter/show.rb
heirloom-0.11.1 lib/heirloom/cli/formatter/show.rb
heirloom-0.11.0 lib/heirloom/cli/formatter/show.rb
heirloom-0.11.0.beta.2 lib/heirloom/cli/formatter/show.rb
heirloom-0.11.0.beta.1 lib/heirloom/cli/formatter/show.rb