Class: Snippet

Inherits:
Object
  • Object
show all
Defined in:
lib/bunch/url_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Snippet

Returns a new instance of Snippet.



169
170
171
172
173
174
175
176
# File 'lib/bunch/url_generator.rb', line 169

def initialize(file)
  if File.exist?(File.expand_path(file))
    @contents = IO.read(File.expand_path(file))
    @fragments = find_fragments
  else
    throw 'Tried to initialize snippet with invalid file'
  end
end

Instance Attribute Details

#contentsObject

Returns the value of attribute contents.



167
168
169
# File 'lib/bunch/url_generator.rb', line 167

def contents
  @contents
end

#fragmentsObject

Returns the value of attribute fragments.



167
168
169
# File 'lib/bunch/url_generator.rb', line 167

def fragments
  @fragments
end

Instance Method Details

#choose_fragmentObject



191
192
193
194
195
196
197
198
199
# File 'lib/bunch/url_generator.rb', line 191

def choose_fragment
  unless @fragments.empty?
    items = []
    @fragments.each { |k, v| items << MenuItem.new(k, k, v) }
    menu = Menu.new(items)
    return menu.choose('Select fragment')
  end
  nil
end

#find_fragmentsObject



178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/bunch/url_generator.rb', line 178

def find_fragments
  rx = /(?i-m)(?:[-#]+)\[([\s\S]*?)\][-# ]*\n([\s\S]*?)(?=\n(?:-+\[|#+\[|$))/
  matches = @contents.scan(rx)
  fragments = {}
  matches.each do |m|
    key = m[0]
    value = m[1]

    fragments[key] = value
  end
  fragments
end