Class: Yummi::TableBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/yummi/table_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ TableBuilder

Returns a new instance of TableBuilder.



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/yummi/table_builder.rb', line 28

def initialize config = {}
  if config.is_a? String
    config = Yummi::Helpers::symbolize_keys(YAML::load_file(config))
  end
  @config = config
  @repositories = {}

  @repositories[:formatters] = [Yummi::Formatters]    
  @repositories[:colorizers] = [Yummi::Colorizers]    
  @repositories[:row_colorizers] = [Yummi::Colorizers]    
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



26
27
28
# File 'lib/yummi/table_builder.rb', line 26

def config
  @config
end

#repositoriesObject

Returns the value of attribute repositories.



26
27
28
# File 'lib/yummi/table_builder.rb', line 26

def repositories
  @repositories
end

Instance Method Details

#build_components(table, config) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/yummi/table_builder.rb', line 89

def build_components(table, config)
  components.each do |key, component_config|
    block = lambda do |params|
      table.send component_config[:invoke], *params
    end
    if component_config[:row_based]
      parse_row_based_component config[key], component_config, &block
    else
      parse_component config[key], component_config, &block
    end
  end
end

#build_tableObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/yummi/table_builder.rb', line 65

def build_table
  table = Yummi::Table::new
  table.title = config[:title]
  table.description= config[:description]
  table.aliases = config[:aliases] if config[:aliases]
  table.header = config[:header] if config[:header]
  table.layout = config[:layout].to_sym if config[:layout]

  build_components table, config

  [:bottom, :top].each do |context|
    contexts = config[context]
    if contexts
      contexts.each do |context_config|
        table.send context, context_config do
          build_components table, context_config
        end
      end
    end
  end

  table
end

#component(keys, params) ⇒ Object



59
60
61
62
63
# File 'lib/yummi/table_builder.rb', line 59

def component keys, params
  [*keys].each do |key|
    components[key] = params
  end
end

#componentsObject



54
55
56
57
# File 'lib/yummi/table_builder.rb', line 54

def components
  @components ||= {}
  @components
end

#create_component(component_config, config) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/yummi/table_builder.rb', line 129

def create_component(component_config, config)
  repositories = @repositories[config[:repository]].reverse
  if component_config.is_a? Hash
    component_config = Yummi::Helpers::symbolize_keys(component_config)
    component = Yummi::GroupedComponent::new
    component_config.each do |component_name, params|
      repositories.each do |repository|
        if repository.respond_to? component_name
          component << repository.send(component_name, params)
          break
        end
      end
    end
    return component
  else
    repositories.each do |repository|
      if repository.respond_to? component_config
        return repository.send(component_config)
      end
    end
  end
end

#defaultsObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/yummi/table_builder.rb', line 40

def defaults
  component [:color, :colorize, :state, :health], :repository => :colorizers,
                                                  :invoke     => :colorize

  component :format, :repository => :formatters,
                     :invoke     => :format

  component [:row_color, :colorize_row], :repository => :row_colorizers,
                                         :invoke     => :colorize_row,
                                         :row_based  => true

  self
end

#parse_component(definitions, config) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/yummi/table_builder.rb', line 102

def parse_component(definitions, config)
  if definitions
    definitions.each do |column, component_config|
      callback = []
      column = column.to_s.split(/,/)
      callback << column
      component = create_component(component_config, config)
      callback << {:using => component}
      yield(callback)
    end
  end
end

#parse_row_based_component(definitions, config) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/yummi/table_builder.rb', line 115

def parse_row_based_component(definitions, config)
  if definitions
    if definitions.is_a? Hash
      definitions.each do |component_name, params|
        component = create_component({component_name => params}, config)
        yield([{:using => component}])
      end
    else
      component = create_component(definitions, config)
      yield([{:using => component}])
    end
  end
end