Module: Flydata::TableAttribute

Defined in:
lib/flydata/table_attribute.rb

Constant Summary collapse

USER_PK_OVERRIDE =
'pk_override'
UK_AS_PK_OVERRIDE =
'uk_as_pk'
INVALID_TABLE_REASON =
'invalid_table_reason'

Class Method Summary collapse

Class Method Details

.append_only_table?(table_attributes, tbl_name) ⇒ Boolean

Returns:

  • (Boolean)

62
63
64
65
66
# File 'lib/flydata/table_attribute.rb', line 62

def self.append_only_table?(table_attributes, tbl_name)
  attr = find_table(table_attributes, tbl_name)
  raise "Sync type for invalid table `#{tbl_name}` not known" unless attr
  attr.has_key?('omit_events')
end

.delete_attribute(table_attributes, key) ⇒ Object


14
15
16
17
# File 'lib/flydata/table_attribute.rb', line 14

def self.delete_attribute(table_attributes, key)
  table_attributes.each {|tbl_attr| tbl_attr.delete(key.to_s)}
  table_attributes
end

.extract_attribute(table_attributes, key) ⇒ Object

Extract => <value_for_a_given_key> pairs from table attributes arguments:

  • table_attributes (hash):

    { "<table-name1>" => {
        "invalid_table_reason" => "<error reason>",
        "pk_override" => ['id1', ...],
        "uk_as_pk" => ['uid', ...],...
      }, 
      "<table-name2> => {...},
    }
    
  • key (symbol or String): hash key in table_attributes

return value (hash):

{ "table_1" => <value_1>, "table_2" => <value_2>,...}

34
35
36
37
38
39
40
41
42
43
# File 'lib/flydata/table_attribute.rb', line 34

def self.extract_attribute(table_attributes, key)
  tbl_value_hash = {}
  table_attributes.each do |tbl_attr|

    table_name = tbl_attr['table_name']
    next unless tbl_attr.has_key?(key.to_s)
    tbl_value_hash[table_name] = tbl_attr[key.to_s]
  end
  tbl_value_hash
end

.find_table(table_attributes, tbl_name) ⇒ Object


58
59
60
# File 'lib/flydata/table_attribute.rb', line 58

def self.find_table(table_attributes, tbl_name)
  table_attributes.find {|tbl_attr| tbl_attr['table_name'] == tbl_name}
end

.generate_pk_override(table_attributes) ⇒ Object


7
8
9
10
11
12
# File 'lib/flydata/table_attribute.rb', line 7

def self.generate_pk_override(table_attributes)
    user_pk_override = extract_attribute(table_attributes, USER_PK_OVERRIDE)
    uk_as_pk_override = extract_attribute(table_attributes, UK_AS_PK_OVERRIDE)

    uk_as_pk_override.merge(user_pk_override)
end

.save_attribute(table_attributes, values_hash, key) ⇒ Object


45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/flydata/table_attribute.rb', line 45

def self.save_attribute(table_attributes, values_hash, key)
  return unless values_hash
  values_hash.each_pair do |tbl_name, val|
    tbl_attr = table_attributes.find { |ta| ta['table_name'] == tbl_name }
    unless tbl_attr
      # Add a new table
      tbl_attr = { 'table_name' => tbl_name }
      table_attributes << tbl_attr
    end
    tbl_attr[key.to_s] = val
  end
end