Class: Codeball::Ball

Inherits:
Object
  • Object
show all
Defined in:
lib/codeball/ball.rb

Overview

A codeball – the aggregate root.

Ball starts empty and grows as entries are added, like a snowball. It does not touch the filesystem. Parse is a thin factory that wires Cursor -> Stream -> Ball.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBall

Returns a new instance of Ball.



19
20
21
22
# File 'lib/codeball/ball.rb', line 19

def initialize
  @entries = []
  @warnings = []
end

Class Method Details

.parse(text) ⇒ Object

Raises:



9
10
11
12
13
14
15
16
17
# File 'lib/codeball/ball.rb', line 9

def self.parse(text)
  raise MalformedBallError, "empty input, nothing to extract" if text.nil? || text.strip.empty?

  ball = new
  stream = Stream.new(cursor: Cursor.new(text))
  stream.each_entry { |entry| ball.add_entry(entry) }
  ball.validate!
  ball
end

Instance Method Details

#add_entry(entry) ⇒ Object



24
25
26
27
28
# File 'lib/codeball/ball.rb', line 24

def add_entry(entry)
  @entries << entry
  @warnings << entry.error if entry.errors?
  @warnings << "truncated entry for #{entry.path.inspect} - missing END marker" if entry.truncated?
end

#all_text?Boolean

Returns:

  • (Boolean)


43
# File 'lib/codeball/ball.rb', line 43

def all_text? = entries.select(&:valid?).all?(&:text?)

#each_entryObject



39
# File 'lib/codeball/ball.rb', line 39

def each_entry(&) = entries.select(&:valid?).each(&)

#each_non_text_entryObject



41
# File 'lib/codeball/ball.rb', line 41

def each_non_text_entry(&) = entries.select(&:valid?).reject(&:text?).each(&)

#each_text_entryObject



40
# File 'lib/codeball/ball.rb', line 40

def each_text_entry(&) = entries.select(&:valid?).select(&:text?).each(&)

#each_warningObject



42
# File 'lib/codeball/ball.rb', line 42

def each_warning(&) = warnings.each(&)

#serializeObject



46
47
48
# File 'lib/codeball/ball.rb', line 46

def serialize
  entries.select(&:valid?).select(&:text?).map(&:serialize).join
end

#validate!Object



30
31
32
33
34
35
36
37
# File 'lib/codeball/ball.rb', line 30

def validate!
  valid = entries.select(&:valid?)
  if valid.empty? && warnings.any?
    raise MalformedBallError, "no valid entries found (#{warnings.length} malformed)"
  elsif valid.empty?
    raise MalformedBallError, "no content found - is this a codeball?"
  end
end

#warning_countObject



44
# File 'lib/codeball/ball.rb', line 44

def warning_count = warnings.length