Class: TablePrint::ConfigResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/table_print/config_resolver.rb

Instance Method Summary collapse

Constructor Details

#initialize(klass, default_column_names, *options) ⇒ ConfigResolver

Returns a new instance of ConfigResolver.



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/table_print/config_resolver.rb', line 3

def initialize(klass, default_column_names, *options)
  @column_hash = {}

  @default_columns = default_column_names.collect { |name| option_to_column(name) }

  @included_columns = []
  @excepted_columns = []
  @only_columns = []

  process_option_set(TablePrint::Config.for(klass))
  process_option_set(options)
end

Instance Method Details

#columnsObject



92
93
94
95
96
# File 'lib/table_print/config_resolver.rb', line 92

def columns
  usable_column_names.collect do |name|
    @column_hash[name]
  end
end

#get_and_remove(options_array, key) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/table_print/config_resolver.rb', line 44

def get_and_remove(options_array, key)
  except = options_array.select do |option|
    option.is_a? Hash and option.keys.include? key
  end

  return [] if except.empty?
  except = except.first

  option_of_interest = except.fetch(key)
  except.delete(key)

  options_array.delete(except) if except.keys.empty?  # if we've taken all the info from this option, get rid of it

  option_of_interest
end

#option_to_column(option) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/table_print/config_resolver.rb', line 60

def option_to_column(option)
  if option.is_a? Hash
    name = option.keys.first
    if option[name].is_a? Proc
      option = {:name => name, :display_method => option[name]}
    else
      option = option[name].merge(:name => name)
    end
  else
    option = {:name => option}
  end

  if option.has_key? :width
    option[:default_width] = option.delete(:width)
  end

  if option.has_key? :display_name
    option[:display_method] = option[:name]
    option[:name] = option.delete(:display_name)
  end

  c = Column.new(option)
  @column_hash[c.name] = c
  c
end

#process_option_set(options) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/table_print/config_resolver.rb', line 16

def process_option_set(options)

  options = [options].flatten
  options.delete_if { |o| o == {} }

  # process special symbols

  @included_columns.concat [get_and_remove(options, :include)].flatten
  @included_columns.map! do |option|
    if option.is_a? Column
      option
    else
      option_to_column(option)
    end
  end

  @included_columns.each do |c|
    @column_hash[c.name] = c
  end

  # excepted columns don't need column objects since we're just going to throw them out anyway
  @excepted_columns.concat [get_and_remove(options, :except)].flatten

  # anything that isn't recognized as a special option is assumed to be a column name
  options.compact!
  @only_columns = options.collect { |name| option_to_column(name) } unless options.empty?
end

#usable_column_namesObject



86
87
88
89
90
# File 'lib/table_print/config_resolver.rb', line 86

def usable_column_names
  base = @default_columns
  base = @only_columns unless @only_columns.empty?
  Array(base).collect(&:name) + Array(@included_columns).collect(&:name) - Array(@excepted_columns).collect(&:to_s)
end