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_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.



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

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.



51
52
53
# File 'lib/cloudformation-ruby-dsl/table.rb', line 51

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.



43
44
45
# File 'lib/cloudformation-ruby-dsl/table.rb', line 43

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.



60
61
62
# File 'lib/cloudformation-ruby-dsl/table.rb', line 60

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

#matches(predicate_value, record_value) ⇒ Object



69
70
71
72
73
# File 'lib/cloudformation-ruby-dsl/table.rb', line 69

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