Class: Blocklist

Inherits:
Object
  • Object
show all
Defined in:
lib/blocklist.rb,
lib/blocklist/cli.rb

Defined Under Namespace

Classes: Block, Cli, Line

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBlocklist

Returns a new instance of Blocklist.



4
5
6
# File 'lib/blocklist.rb', line 4

def initialize
  self.blocks = []
end

Instance Attribute Details

#blocksObject

Returns the value of attribute blocks.



2
3
4
# File 'lib/blocklist.rb', line 2

def blocks
  @blocks
end

Instance Method Details

#block(name) ⇒ Object



30
31
32
# File 'lib/blocklist.rb', line 30

def block(name)
  blocks.find {|block| block.name == name}
end

#parse(content) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/blocklist.rb', line 8

def parse(content)
  block = Block.new
  content.each_line do |line|
    parsed_line = Line.parse(line)
    case parsed_line
    when nil
      self.blocks << block
      block = Block.new
    when String
      block.name ||= parsed_line
    when Line
      block.lines << parsed_line
    else
      raise "Unexpected line: #{line}"
    end
  end
  if block.name or block.lines.size > 0
    self.blocks << block
  end
  nil
end

#to_sObject



34
35
36
# File 'lib/blocklist.rb', line 34

def to_s
  blocks.join("\n\n")
end