class SXP::Generator::Block
A basic block containing constituent objects, either blocks or strings.
Constants
- BLOCK_MIN_LENGTH
Attributes
indent[R]
@attr [Integer] amount of indent applied to this block
Public Class Methods
new(obj, indent)
click to toggle source
@param [Object] obj
# File lib/sxp/generator.rb, line 18 def initialize(obj, indent) @indent = indent @elements = [] if obj.is_a?(Array) obj.compact.each {|o| @elements << Block.new(o, indent + 1)} else @elements = obj end end
Public Instance Methods
formatted()
click to toggle source
Format block @return [String]
# File lib/sxp/generator.rb, line 60 def formatted # Output individual block elements on separate lines buffer = "" if sxp? && length > BLOCK_MIN_LENGTH buffer += do_indent + '(' first, *elems = @elements unless first.sxp? # It's atomic, write out after paren buffer += first.to_sxp + "\n" else buffer += "\n" elems.unshift(first) end elems.each do |e| buffer += e.formatted end buffer += do_indent + ")\n" else buffer += do_indent + @elements.to_sxp + "\n" end buffer end
length()
click to toggle source
Agregate length over each element accounting for spaces
@return [Integer]
If indent is not not nil, returns zero
# File lib/sxp/generator.rb, line 33 def length if @elements.is_a?(Array) @elements.map(&:length).inject(:+).to_i + @elements.length - 1 else @elements.to_sxp.length end end
sxp?()
click to toggle source
Determins if this block is an SXP
, or not @return [Boolean]
# File lib/sxp/generator.rb, line 53 def sxp? @elements.is_a?(Array) end
to_sxp()
click to toggle source
Turn block into a string in S-expression form This should only be called on a block when no indentation is to be applied @return [String]
# File lib/sxp/generator.rb, line 46 def to_sxp @elements.to_sxp end
Private Instance Methods
do_indent(offset = 0)
click to toggle source
# File lib/sxp/generator.rb, line 85 def do_indent(offset = 0); ' ' * (indent + offset); end