Class: Table

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudformation-ruby-dsl/table.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_as_text) ⇒ Table

Returns a new instance of Table.



22
23
24
25
26
# File 'lib/cloudformation-ruby-dsl/table.rb', line 22

def initialize(table_as_text)
  raw_header, *raw_data = Detabulator.new.detabulate table_as_text
  @header = raw_header.map(&:to_sym)
  @records = raw_data.map { |row| Hash[@header.zip(row)] }
end

Class Method Details

.load(filename) ⇒ Object



18
19
20
# File 'lib/cloudformation-ruby-dsl/table.rb', line 18

def self.load(filename)
  self.new File.read filename
end

Instance Method Details

#get(key, predicate) ⇒ Object

Selects all rows in the table which match the name/value pairs of the predicate object and returns the single distinct value from those rows for the specified key.



30
31
32
# File 'lib/cloudformation-ruby-dsl/table.rb', line 30

def get(key, predicate)
  distinct_values(filter(predicate), key, false)
end

#get_header(*exclude) ⇒ Object

Select the headers as list. Argument(s) will be excluded from output.



35
36
37
# File 'lib/cloudformation-ruby-dsl/table.rb', line 35

def get_header(*exclude)
  @header.reject{ |key| key if exclude.include?(key) }
end

#get_list(key, predicate) ⇒ Object

Selects all rows in the table which match the name/value pairs of the predicate object and returns all distinct values from those rows for the specified key.



41
42
43
# File 'lib/cloudformation-ruby-dsl/table.rb', line 41

def get_list(key, predicate)
  distinct_values(filter(predicate), key, true)
end

#get_map(predicate, *keys) ⇒ Object

Selects all rows in the table which match the name/value pairs of the predicate object and returns a set of nested maps, where the key for the map at level n is the key at index n in the specified keys, except for the last key in the specified keys which is used to determine the value of the leaf-level map. In the simple case where keys is a list of 2 elements, this returns a map from key to key.



56
57
58
# File 'lib/cloudformation-ruby-dsl/table.rb', line 56

def get_map(predicate, *keys)
  build_nested_map(filter(predicate), keys, false)
end

#get_multihash(key, predicate, *keys) ⇒ Object

Selects all rows in the table which match the name/value pairs of the predicate object and returns a hash of hashes, where the key for the top-level hash is the key paramter and the second-level hash keys are those in the keys paramter. This is useful when you want multiple column values for a given row.



48
49
50
# File 'lib/cloudformation-ruby-dsl/table.rb', line 48

def get_multihash(key, predicate, *keys)
  build_nested_hash(filter(predicate), key, keys)
end

#get_multimap(predicate, *keys) ⇒ Object

Selects all rows in the table which match the name/value pairs of the predicate object and returns a set of nested maps, where the key for the map at level n is the key at index n in the specified keys, except for the last key in the specified keys which is used to determine the list of values in the leaf-level map. In the simple case where keys is a list of 2 elements, this returns a map from key to a list of values for key.



65
66
67
# File 'lib/cloudformation-ruby-dsl/table.rb', line 65

def get_multimap(predicate, *keys)
  build_nested_map(filter(predicate), keys, true)
end

#matches(predicate_value, record_value) ⇒ Object



74
75
76
77
78
# File 'lib/cloudformation-ruby-dsl/table.rb', line 74

def matches(predicate_value, record_value)
  if predicate_value.is_a?(Array); predicate_value.include?(record_value)
  else; predicate_value == record_value
  end
end