Class: Spanner::Translator::Rules::BaseRule

Inherits:
Parser::AST::Processor
  • Object
show all
Extended by:
RuboCop::AST::NodePattern::Macros
Includes:
RuboCop::AST::Traversal
Defined in:
lib/spanner/translator/rules/translation/base_rule.rb

Overview

rubocop:disable Style/Documentation

Constant Summary collapse

RAILS_COLUMN_TYPES =
i[bigint string text integer float decimal
datetime timestamp time date binary boolean].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rewriter, options: {}) ⇒ BaseRule

rubocop:disable Lint/MissingSuper



17
18
19
20
21
22
23
24
# File 'lib/spanner/translator/rules/translation/base_rule.rb', line 17

def initialize(rewriter, options: {})
  @rewriter = rewriter
  @seen = {}
  @column_nullity = {}
  @translation_options = options
  @primary_key_argument = nil
  @primary_key_properties = nil
end

Instance Attribute Details

#translation_optionsObject (readonly)

Returns the value of attribute translation_options.



14
15
16
# File 'lib/spanner/translator/rules/translation/base_rule.rb', line 14

def translation_options
  @translation_options
end

Instance Method Details

#any_index?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/spanner/translator/rules/translation/base_rule.rb', line 59

def any_index?
  @seen["index"]
end

#capture_column_nullity(node) ⇒ Object

track every columns' null acceptance



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/spanner/translator/rules/translation/base_rule.rb', line 46

def capture_column_nullity(node)
  is_typed_column_node = node.receiver &&
                         node.receiver.source == "t" &&
                         RAILS_COLUMN_TYPES.include?(node.method_name)
  return unless is_typed_column_node

  column_name = node.arguments[0].source.tr('"', "")
  null_option = node.find_hash_option(:null)

  # "may be null" means this column is not defined with explicit "null: false"
  @column_nullity[column_name] = null_option&.value&.source != "false"
end

#capture_primary_key_argument(node) ⇒ Object



35
36
37
38
39
40
# File 'lib/spanner/translator/rules/translation/base_rule.rb', line 35

def capture_primary_key_argument(node)
  return unless node.method_name == :create_table

  @primary_key_argument = node.find_hash_option(:primary_key)
  @primary_key_properties = node.find_hash_option(:id)
end

#capture_table_name(node) ⇒ Object



31
32
33
# File 'lib/spanner/translator/rules/translation/base_rule.rb', line 31

def capture_table_name(node)
  @table_name = node.arguments[0].source.tr('"', "") if node.method_name == :create_table
end

#conditionally_propagate_argument(arguments, node, name) ⇒ Object



70
71
72
# File 'lib/spanner/translator/rules/translation/base_rule.rb', line 70

def conditionally_propagate_argument(arguments, node, name)
  arguments << node.find_hash_option_source(name) if node.find_hash_option(name)
end

#create_range(begin_pos, end_pos) ⇒ Object

rubocop:enable Lint/MissingSuper



27
28
29
# File 'lib/spanner/translator/rules/translation/base_rule.rb', line 27

def create_range(begin_pos, end_pos)
  Parser::Source::Range.new(@rewriter.source_buffer, begin_pos, end_pos)
end

#unless_seen(condition) ⇒ Object



63
64
65
66
67
68
# File 'lib/spanner/translator/rules/translation/base_rule.rb', line 63

def unless_seen(condition)
  return if @seen[condition]

  yield
  @seen[condition] = true
end