Class: Masking::InsertStatement

Inherits:
Object
  • Object
show all
Defined in:
lib/masking/insert_statement.rb,
lib/masking/insert_statement/sql_builder.rb

Defined Under Namespace

Classes: SQLBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_statement, sql_builder: SQLBuilder) ⇒ InsertStatement

Returns a new instance of InsertStatement.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/masking/insert_statement.rb', line 10

def initialize(raw_statement, sql_builder: SQLBuilder)
  @raw_statement = raw_statement
  @sql_builder = sql_builder

  PARSE_REGEXP.match(raw_statement).tap do |match_data|
    raise Error::InsertStatementParseError if match_data.nil?

    @table           = match_data[:table]
    @columns_section = match_data[:columns_section]
    @values_section  = match_data[:values_section]
  end
end

Instance Attribute Details

#raw_statementObject (readonly)

Returns the value of attribute raw_statement.



8
9
10
# File 'lib/masking/insert_statement.rb', line 8

def raw_statement
  @raw_statement
end

#tableObject (readonly)

Returns the value of attribute table.



8
9
10
# File 'lib/masking/insert_statement.rb', line 8

def table
  @table
end

Instance Method Details

#column_index(column_name) ⇒ Object



27
28
29
# File 'lib/masking/insert_statement.rb', line 27

def column_index(column_name)
  columns.index(column_name)
end

#columnsObject



23
24
25
# File 'lib/masking/insert_statement.rb', line 23

def columns
  @columns ||= columns_section.scan(COLUMNS_REGEXP).flatten.map(&:to_sym)
end

#mask_value(column_index:, mask_method:) ⇒ Object



41
42
43
44
45
# File 'lib/masking/insert_statement.rb', line 41

def mask_value(column_index:, mask_method:)
  values.each do |value|
    value[column_index] = mask_method.call(value[column_index])
  end
end

#sqlObject



47
48
49
# File 'lib/masking/insert_statement.rb', line 47

def sql
  sql_builder.new(table: table, columns: columns, values: values).sql
end

#valuesObject



31
32
33
34
35
36
37
38
39
# File 'lib/masking/insert_statement.rb', line 31

def values
  # NOTE: the reason to use `rows.each_with_index()`
  #   another simple implementations (e.g. `rows.count.time`) doesn't work.because during the block loop,
  #   the `rows` array object can be destructively changed by the #recursive_pattern_value_concat! method and
  #   it make different number of count to loop during the block loop, so it needs to be checked by the object size
  @values ||= values_section.split(VALUE_ROW_SPLITTER)
                            .tap { |rows| rows.each_with_index { |_, i| recursive_pattern_value_concat!(rows, i) } }
                            .flat_map { |row| row.scan(values_regexp) }
end