Class: Wordmove::SqlAdapter::Default
- Inherits:
-
Object
- Object
- Wordmove::SqlAdapter::Default
- Defined in:
- lib/wordmove/sql_adapter/default.rb
Instance Attribute Summary collapse
-
#dest_config ⇒ Object
readonly
Returns the value of attribute dest_config.
-
#source_config ⇒ Object
readonly
Returns the value of attribute source_config.
- #sql_content ⇒ Object
-
#sql_path ⇒ Object
readonly
Returns the value of attribute sql_path.
Instance Method Summary collapse
- #adapt! ⇒ Object
-
#initialize(sql_path, source_config, dest_config) ⇒ Default
constructor
A new instance of Default.
- #replace_field!(source_field, dest_field) ⇒ Object
- #replace_vhost! ⇒ Object
- #replace_wordpress_path! ⇒ Object
- #serialized_replace!(source_field, dest_field) ⇒ Object
- #simple_replace!(source_field, dest_field) ⇒ Object
- #write_sql! ⇒ Object
Constructor Details
#initialize(sql_path, source_config, dest_config) ⇒ Default
Returns a new instance of Default.
7 8 9 10 11 |
# File 'lib/wordmove/sql_adapter/default.rb', line 7 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_config ⇒ Object (readonly)
Returns the value of attribute dest_config.
5 6 7 |
# File 'lib/wordmove/sql_adapter/default.rb', line 5 def dest_config @dest_config end |
#source_config ⇒ Object (readonly)
Returns the value of attribute source_config.
5 6 7 |
# File 'lib/wordmove/sql_adapter/default.rb', line 5 def source_config @source_config end |
#sql_content ⇒ Object
13 14 15 |
# File 'lib/wordmove/sql_adapter/default.rb', line 13 def sql_content @sql_content ||= File.open(sql_path).read end |
#sql_path ⇒ Object (readonly)
Returns the value of attribute sql_path.
5 6 7 |
# File 'lib/wordmove/sql_adapter/default.rb', line 5 def sql_path @sql_path end |
Instance Method Details
#adapt! ⇒ Object
17 18 19 20 21 |
# File 'lib/wordmove/sql_adapter/default.rb', line 17 def adapt! replace_vhost! replace_wordpress_path! write_sql! end |
#replace_field!(source_field, dest_field) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/wordmove/sql_adapter/default.rb', line 35 def replace_field!(source_field, dest_field) return false unless source_field && dest_field serialized_replace!(source_field, dest_field) simple_replace!(source_field, dest_field) end |
#replace_vhost! ⇒ Object
23 24 25 26 27 |
# File 'lib/wordmove/sql_adapter/default.rb', line 23 def replace_vhost! source_vhost = source_config[:vhost] dest_vhost = dest_config[:vhost] replace_field!(source_vhost, dest_vhost) end |
#replace_wordpress_path! ⇒ Object
29 30 31 32 33 |
# File 'lib/wordmove/sql_adapter/default.rb', line 29 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
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/wordmove/sql_adapter/default.rb', line 42 def serialized_replace!(source_field, dest_field) length_delta = source_field.length - dest_field.length sql_content.gsub!(/s:(\d+):([\\]*['"])(.*?)\2;/) do |_| length = Regexp.last_match(1).to_i delimiter = Regexp.last_match(2) string = Regexp.last_match(3) string.gsub!(/#{Regexp.escape(source_field)}/) do |_| length -= length_delta dest_field end %(s:#{length}:#{delimiter}#{string}#{delimiter};) end end |
#simple_replace!(source_field, dest_field) ⇒ Object
59 60 61 |
# File 'lib/wordmove/sql_adapter/default.rb', line 59 def simple_replace!(source_field, dest_field) sql_content.gsub!(source_field, dest_field) end |
#write_sql! ⇒ Object
63 64 65 |
# File 'lib/wordmove/sql_adapter/default.rb', line 63 def write_sql! File.open(sql_path, 'w') { |f| f.write(sql_content) } end |