Sha256: a233474c6dee3a9c5ed0f38a9506228453b568de079177436f928a7062f95b5f

Contents?: true

Size: 1.63 KB

Versions: 9

Compression:

Stored size: 1.63 KB

Contents

module Bmg
  class OutputPreferences

    DEFAULT_PREFS = {
      attributes_ordering: nil,
      tuple_ordering: nil,
      extra_attributes: :after
    }

    def initialize(options)
      @options = DEFAULT_PREFS.merge(options)
    end
    attr_reader :options

    def self.dress(arg)
      return arg if arg.is_a?(OutputPreferences)
      arg = {} if arg.nil?
      new(arg)
    end

    def tuple_ordering
      return nil unless to = options[:tuple_ordering]

      @tuple_ordering = Ordering.new(to)
    end

    def attributes_ordering
      options[:attributes_ordering]
    end

    def extra_attributes
      options[:extra_attributes]
    end

    def grouping_attributes
      options[:grouping_attributes]
    end

    def erase_redundance_in_group(before, current)
      return [nil, current] unless ga = grouping_attributes
      return [current, current] unless before

      new_before, new_current = current.dup, current.dup
      ga.each do |attr|
        return [new_before, new_current] unless before[attr] == current[attr]
        new_current[attr] = nil
      end
      [new_before, new_current]
    end

    def order_attrlist(attrlist)
      return attrlist if attributes_ordering.nil?

      index = Hash[attributes_ordering.each_with_index.to_a]
      base  = attrlist
      base  = attrlist & attributes_ordering if extra_attributes == :ignored
      base.sort{|a,b|
        ai, bi = index[a], index[b]
        if ai && bi
          ai <=> bi
        elsif ai
          extra_attributes == :after ? -1 : 1
        else
          extra_attributes == :after ? 1 : -1
        end
      }
    end

  end # class OutputPreferences
end # module Bmg

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
bmg-0.23.3 lib/bmg/support/output_preferences.rb
bmg-0.23.2 lib/bmg/support/output_preferences.rb
bmg-0.23.1 lib/bmg/support/output_preferences.rb
bmg-0.23.0 lib/bmg/support/output_preferences.rb
bmg-0.21.5 lib/bmg/support/output_preferences.rb
bmg-0.21.4 lib/bmg/support/output_preferences.rb
bmg-0.20.5 lib/bmg/support/output_preferences.rb
bmg-0.19.3 lib/bmg/support/output_preferences.rb
bmg-0.21.3 lib/bmg/support/output_preferences.rb