Class: Wordmove::SqlAdapter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sql_path, source_config, dest_config) ⇒ SqlAdapter

Returns a new instance of SqlAdapter.



6
7
8
9
10
# File 'lib/wordmove/sql_adapter.rb', line 6

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.



4
5
6
# File 'lib/wordmove/sql_adapter.rb', line 4

def dest_config
  @dest_config
end

#source_configObject (readonly)

Returns the value of attribute source_config.



4
5
6
# File 'lib/wordmove/sql_adapter.rb', line 4

def source_config
  @source_config
end

#sql_contentObject

Returns the value of attribute sql_content.



3
4
5
# File 'lib/wordmove/sql_adapter.rb', line 3

def sql_content
  @sql_content
end

#sql_pathObject (readonly)

Returns the value of attribute sql_path.



4
5
6
# File 'lib/wordmove/sql_adapter.rb', line 4

def sql_path
  @sql_path
end

Instance Method Details

#adapt!Object



16
17
18
19
20
# File 'lib/wordmove/sql_adapter.rb', line 16

def adapt!
  replace_vhost!
  replace_wordpress_path!
  write_sql!
end

#replace_field!(source_field, dest_field) ⇒ Object



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

def replace_field!(source_field, dest_field)
  if source_field && dest_field
    serialized_replace!(source_field, dest_field)
    simple_replace!(source_field, dest_field)
  end
end

#replace_vhost!Object



22
23
24
25
26
# File 'lib/wordmove/sql_adapter.rb', line 22

def replace_vhost!
  source_vhost = source_config[:vhost]
  dest_vhost = dest_config[:vhost]
  replace_field!(source_vhost, dest_vhost)
end

#replace_wordpress_path!Object



28
29
30
31
32
# File 'lib/wordmove/sql_adapter.rb', line 28

def replace_wordpress_path!
  source_path = source_config[:wordpress_absolute_path] || source_config[:wordpress_path]
  dest_path = dest_config[:wordpress_absolute_path] || dest_config[:wordpress_path]
  replace_field!(source_path, dest_path)
end

#serialized_replace!(source_field, dest_field) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/wordmove/sql_adapter.rb', line 41

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

  sql_content.gsub!(/s:(\d+):([\\]*['"])(.*?)\2;/) do |match|
    length = $1.to_i
    delimiter = $2
    string = $3

    string.gsub!(/#{Regexp.escape(source_field)}/) do |match|
      length -= length_delta
      dest_field
    end

    %(s:#{length}:#{delimiter}#{string}#{delimiter};)
  end
end

#simple_replace!(source_field, dest_field) ⇒ Object



58
59
60
# File 'lib/wordmove/sql_adapter.rb', line 58

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

#write_sql!Object



62
63
64
# File 'lib/wordmove/sql_adapter.rb', line 62

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