Class: Wordmove::SqlMover

Inherits:
Object
  • Object
show all
Defined in:
lib/wordmove/sql_mover.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sql_path, source_config, dest_config) ⇒ SqlMover

Returns a new instance of SqlMover.



8
9
10
11
12
# File 'lib/wordmove/sql_mover.rb', line 8

def initialize(sql_path, source_config, dest_config)
  @sql_path = sql_path
  @source_config = source_config
  @dest_config = dest_config
end

Instance Attribute Details

#dest_configObject (readonly)

Returns the value of attribute dest_config.



6
7
8
# File 'lib/wordmove/sql_mover.rb', line 6

def dest_config
  @dest_config
end

#source_configObject (readonly)

Returns the value of attribute source_config.



6
7
8
# File 'lib/wordmove/sql_mover.rb', line 6

def source_config
  @source_config
end

#sql_contentObject

Returns the value of attribute sql_content.



5
6
7
# File 'lib/wordmove/sql_mover.rb', line 5

def sql_content
  @sql_content
end

#sql_pathObject (readonly)

Returns the value of attribute sql_path.



6
7
8
# File 'lib/wordmove/sql_mover.rb', line 6

def sql_path
  @sql_path
end

Instance Method Details

#move!Object



18
19
20
21
22
# File 'lib/wordmove/sql_mover.rb', line 18

def move!
  replace_vhost!
  replace_wordpress_path!
  write_sql!
end

#replace_field!(field_sym) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/wordmove/sql_mover.rb', line 32

def replace_field!(field_sym)
  source_field = source_config[field_sym]
  dest_field = dest_config[field_sym]
  if source_field && dest_field
    serialized_replace!(source_field, dest_field)
    simple_replace!(source_field, dest_field)
  end
end

#replace_vhost!Object



24
25
26
# File 'lib/wordmove/sql_mover.rb', line 24

def replace_vhost!
  replace_field!(:vhost)
end

#replace_wordpress_path!Object



28
29
30
# File 'lib/wordmove/sql_mover.rb', line 28

def replace_wordpress_path!
  replace_field!(:wordpress_path)
end

#serialized_replace!(source_field, dest_field) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/wordmove/sql_mover.rb', line 41

def serialized_replace!(source_field, dest_field)
  length_delta = source_field.length - dest_field.length

  sql_content.gsub!(/s:(\d+):"#{Regexp.escape(source_field)}/) do |match|
    source_length = $1.to_i
    dest_length = source_length - length_delta
    "s:#{dest_length}:\"#{dest_field}"
  end
end

#simple_replace!(source_field, dest_field) ⇒ Object



51
52
53
# File 'lib/wordmove/sql_mover.rb', line 51

def simple_replace!(source_field, dest_field)
  sql_content.gsub!(source_field, dest_field)
end

#write_sql!Object



55
56
57
# File 'lib/wordmove/sql_mover.rb', line 55

def write_sql!
  File.open(sql_path, 'w') {|f| f.write(sql_content) }
end