Class: DbTransformer::Transform

Inherits:
Object
  • Object
show all
Defined in:
lib/db_transformer/transform.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_name, row, rules) ⇒ Transform

Returns a new instance of Transform.



7
8
9
10
11
# File 'lib/db_transformer/transform.rb', line 7

def initialize(table_name, row, rules)
  @table_name = table_name
  @row = row
  @rules = rules
end

Class Method Details

.execute!(*args) ⇒ Object



3
4
5
# File 'lib/db_transformer/transform.rb', line 3

def self.execute!(*args)
  new(*args).execute!
end

Instance Method Details

#applicable_rulesObject



26
27
28
29
30
# File 'lib/db_transformer/transform.rb', line 26

def applicable_rules
  @applicable_rules ||= @rules.select do |rule|
    @table_name.to_s == rule['table']
  end
end

#execute!Object



13
14
15
16
17
18
19
20
# File 'lib/db_transformer/transform.rb', line 13

def execute!
  return @row unless match_rules?

  applicable_rules.inject(@row) do |row, rule|
    rule_model = Rules.const_get(rule['type'].split('_').map(&:capitalize).join, false)
    rule_model.apply(rule['mode'], row, rule['column'], rule)
  end
end

#match_rules?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/db_transformer/transform.rb', line 22

def match_rules?
  applicable_rules.any?
end