Class: Embulk::Migrator
- Inherits:
-
Object
- Object
- Embulk::Migrator
- Defined in:
- lib/embulk/command/embulk_migrate_plugin.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #data(file) ⇒ Object
-
#initialize(path) ⇒ Migrator
constructor
A new instance of Migrator.
- #insert_line(glob, pattern, text = nil) ⇒ Object
- #match(glob, pattern) ⇒ Object
- #modified_files ⇒ Object
- #replace(glob, pattern, text = nil) ⇒ Object
- #write(file, data) ⇒ Object
Constructor Details
#initialize(path) ⇒ Migrator
Returns a new instance of Migrator.
86 87 88 89 |
# File 'lib/embulk/command/embulk_migrate_plugin.rb', line 86 def initialize(path) @path = path @modified_files = {} end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
91 92 93 |
# File 'lib/embulk/command/embulk_migrate_plugin.rb', line 91 def path @path end |
Instance Method Details
#data(file) ⇒ Object
144 145 146 |
# File 'lib/embulk/command/embulk_migrate_plugin.rb', line 144 def data(file) File.read(File.join(@path, file)) end |
#insert_line(glob, pattern, text = nil) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/embulk/command/embulk_migrate_plugin.rb', line 127 def insert_line(glob, pattern, text=nil) ms = Dir[File.join(@path, glob)].map do |file| data = File.read(file) if m = data.match(pattern) ln = m.pre_match.split("\n").count replace = text || yield(m) lines = data.split("\n") lines.insert(ln + 1, replace) data = lines.join("\n") modify(file, data) m end end.compact return nil if ms.empty? ms end |
#match(glob, pattern) ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/embulk/command/embulk_migrate_plugin.rb', line 97 def match(glob, pattern) ms = Dir[File.join(@path, glob)].map do |file| File.read(file).match(pattern) end.compact return nil if ms.empty? ms end |
#modified_files ⇒ Object
93 94 95 |
# File 'lib/embulk/command/embulk_migrate_plugin.rb', line 93 def modified_files @modified_files.keys end |
#replace(glob, pattern, text = nil) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/embulk/command/embulk_migrate_plugin.rb', line 105 def replace(glob, pattern, text=nil) ms = Dir[File.join(@path, glob)].map do |file| data = File.read(file) first = nil pos = 0 while pos < data.length m = data.match(pattern, pos) break unless m first ||= m replace = text || yield(m) data = m.pre_match + data[m.begin(0)..(m.begin(1)-1)] + replace + data[m.end(1)..(m.end(0)-1)] + m.post_match pos = m.begin(1) + replace.length + (m.end(0) - m.end(1)) end if first modify(file, data) end first end.compact return nil if ms.empty? ms end |
#write(file, data) ⇒ Object
148 149 150 |
# File 'lib/embulk/command/embulk_migrate_plugin.rb', line 148 def write(file, data) modify(File.join(@path, file), data) end |