Class: Snippet
- Inherits:
-
Object
- Object
- Snippet
- Defined in:
- lib/bunch/url_generator.rb
Instance Attribute Summary collapse
-
#contents ⇒ Object
Returns the value of attribute contents.
-
#fragments ⇒ Object
Returns the value of attribute fragments.
Instance Method Summary collapse
- #choose_fragment ⇒ Object
- #find_fragments ⇒ Object
-
#initialize(file) ⇒ Snippet
constructor
A new instance of Snippet.
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.(file)) @contents = IO.read(File.(file)) @fragments = find_fragments else throw 'Tried to initialize snippet with invalid file' end end |
Instance Attribute Details
#contents ⇒ Object
Returns the value of attribute contents.
167 168 169 |
# File 'lib/bunch/url_generator.rb', line 167 def contents @contents end |
#fragments ⇒ Object
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_fragment ⇒ Object
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.new(items) return .choose('Select fragment') end nil end |
#find_fragments ⇒ Object
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 |