Class: Bmg::OutputPreferences

Inherits:
Object
  • Object
show all
Defined in:
lib/bmg/support/output_preferences.rb

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ OutputPreferences

Returns a new instance of OutputPreferences.



10
11
12
# File 'lib/bmg/support/output_preferences.rb', line 10

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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/bmg/support/output_preferences.rb', line 13

def options
  @options
end

Class Method Details

.dress(arg) ⇒ Object



15
16
17
18
19
# File 'lib/bmg/support/output_preferences.rb', line 15

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

Instance Method Details

#attributes_orderingObject



27
28
29
# File 'lib/bmg/support/output_preferences.rb', line 27

def attributes_ordering
  options[:attributes_ordering]
end

#erase_redundance_in_group(before, current) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/bmg/support/output_preferences.rb', line 39

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

#extra_attributesObject



31
32
33
# File 'lib/bmg/support/output_preferences.rb', line 31

def extra_attributes
  options[:extra_attributes]
end

#grouping_attributesObject



35
36
37
# File 'lib/bmg/support/output_preferences.rb', line 35

def grouping_attributes
  options[:grouping_attributes]
end

#order_attrlist(attrlist) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/bmg/support/output_preferences.rb', line 51

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

#tuple_orderingObject



21
22
23
24
25
# File 'lib/bmg/support/output_preferences.rb', line 21

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

  @tuple_ordering = Ordering.new(to)
end