Class: Codeball::Ball
- Inherits:
-
Object
- Object
- Codeball::Ball
- 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
- #add_entry(entry) ⇒ Object
- #all_text? ⇒ Boolean
- #each_entry ⇒ Object
- #each_non_text_entry ⇒ Object
- #each_text_entry ⇒ Object
- #each_warning ⇒ Object
-
#initialize ⇒ Ball
constructor
A new instance of Ball.
- #serialize ⇒ Object
- #validate! ⇒ Object
- #warning_count ⇒ Object
Constructor Details
#initialize ⇒ Ball
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
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
43 |
# File 'lib/codeball/ball.rb', line 43 def all_text? = entries.select(&:valid?).all?(&:text?) |
#each_entry ⇒ Object
39 |
# File 'lib/codeball/ball.rb', line 39 def each_entry(&) = entries.select(&:valid?).each(&) |
#each_non_text_entry ⇒ Object
41 |
# File 'lib/codeball/ball.rb', line 41 def each_non_text_entry(&) = entries.select(&:valid?).reject(&:text?).each(&) |
#each_text_entry ⇒ Object
40 |
# File 'lib/codeball/ball.rb', line 40 def each_text_entry(&) = entries.select(&:valid?).select(&:text?).each(&) |
#each_warning ⇒ Object
42 |
# File 'lib/codeball/ball.rb', line 42 def each_warning(&) = warnings.each(&) |
#serialize ⇒ Object
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_count ⇒ Object
44 |
# File 'lib/codeball/ball.rb', line 44 def warning_count = warnings.length |