Module: Rpack::Utils
- Included in:
- Rpack
- Defined in:
- lib/utils.rb
Instance Method Summary collapse
- #extract_contents(contents, key, begin_pattern = nil, end_pattern = nil) ⇒ Object
- #find_config_by_path(path) ⇒ Object
- #pattern_positions(contents, begin_pattern, end_pattern) ⇒ Object
Instance Method Details
#extract_contents(contents, key, begin_pattern = nil, end_pattern = nil) ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/utils.rb', line 11 def extract_contents(contents,key,begin_pattern=nil,end_pattern=nil) regexp = Regexp.new(":\\b#{key}\\b") if begin_pattern && end_pattern begin_p, end_p = pattern_positions(contents,begin_pattern,end_pattern) return [] if !begin_p || !end_p contents = contents[begin_p..end_p] end contents.select { |line| line =~ regexp } end |
#find_config_by_path(path) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/utils.rb', line 21 def find_config_by_path(path) for key,value in @config paths = value["paths"].map { |e| Regexp.new("^#{e}") } found = paths.any? {|e| e =~ path} return @config[key] if found end nil end |
#pattern_positions(contents, begin_pattern, end_pattern) ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/utils.rb', line 3 def pattern_positions(contents,begin_pattern,end_pattern) begin_e = Regexp.new(begin_pattern) end_e = Regexp.new(end_pattern) begin_p = contents.find_index {|e| e =~ begin_e} end_p = contents.find_index {|e| e =~ end_e} [begin_p,end_p] end |