Sha256: d0a3c6c8a666692b1a4d043b3baa83e2eef9374b1db6a3b966d13461303aea4a

Contents?: true

Size: 1.79 KB

Versions: 10

Compression:

Stored size: 1.79 KB

Contents

module HammerCLI::Output::Adapter
  class TreeStructure < Abstract

    def initialize(context={}, formatters={})
      super
      @paginate_by_default = false
    end

    def prepare_collection(fields, collection)
      collection.map do |element|
        render_fields(fields, element)
      end
    end

    protected
    def field_filter
      filtered = []
      filtered << Fields::Id unless @context[:show_ids]
      HammerCLI::Output::FieldFilter.new(filtered)
    end

    def filter_fields(fields, data)
      field_filter.filter(fields).reject do |field|
        field_data = data_for_field(field, data)
        not field.display?(field_data)
      end
    end

    def render_fields(fields, data)
      fields = filter_fields(fields, data)

      fields.reduce({}) do |hash, field|
        field_data = data_for_field(field, data)
        next unless field.display?(field_data)
        hash.update(field.label => render_field(field, field_data))
      end
    end

    def render_field(field, data)
      if field.is_a? Fields::ContainerField
        data = [data] unless data.is_a? Array
        fields_data = data.map do |d|
          render_fields(field.fields, d)
        end
        render_data(field, map_data(fields_data))
      else
        data
      end
    end

    def render_data(field, data)
      if field.is_a?(Fields::Collection)
        if(field.parameters[:numbered])
          numbered_data(data)
        else # necislovana kolekce je pole
          data
        end
      else
        data.first
      end
    end

    def map_data(data)
      if data.any? { |d| d.key?(nil) }
        data.map! { |d| d.values.first }
      end
      data
    end

    def numbered_data(data)
      i = 0
      data.inject({}) do |hash, value|
        i += 1
        hash.merge(i => value)
      end
    end

  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
hammer_cli-0.13.1 lib/hammer_cli/output/adapter/tree_structure.rb
hammer_cli-0.13.0 lib/hammer_cli/output/adapter/tree_structure.rb
hammer_cli-0.12.0 lib/hammer_cli/output/adapter/tree_structure.rb
hammer_cli-0.11.0 lib/hammer_cli/output/adapter/tree_structure.rb
hammer_cli-0.10.2 lib/hammer_cli/output/adapter/tree_structure.rb
hammer_cli-0.10.1 lib/hammer_cli/output/adapter/tree_structure.rb
hammer_cli-0.10.0 lib/hammer_cli/output/adapter/tree_structure.rb
hammer_cli-0.9.0 lib/hammer_cli/output/adapter/tree_structure.rb
hammer_cli-0.8.0 lib/hammer_cli/output/adapter/tree_structure.rb
hammer_cli-0.7.0 lib/hammer_cli/output/adapter/tree_structure.rb