Class: SXP::Generator::Block

Inherits:
Object
  • Object
show all
Defined in:
vendor/bundler/ruby/2.5.0/bundler/gems/sxp.rb-77c5073a7739/lib/sxp/generator.rb

Overview

A basic block containing constituent objects, either blocks or strings.

Constant Summary

BLOCK_MIN_LENGTH =
80

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj, indent) ⇒ Block

Returns a new instance of Block

Parameters:



19
20
21
22
23
24
25
26
27
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sxp.rb-77c5073a7739/lib/sxp/generator.rb', line 19

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

Instance Attribute Details

#indentObject (readonly)



15
16
17
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sxp.rb-77c5073a7739/lib/sxp/generator.rb', line 15

def indent
  @indent
end

Instance Method Details

#formattedString

Format block

Returns:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sxp.rb-77c5073a7739/lib/sxp/generator.rb', line 61

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

#lengthInteger

Agregate length over each element accounting for spaces

Returns:

  • (Integer)

    If indent is not not nil, returns zero



34
35
36
37
38
39
40
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sxp.rb-77c5073a7739/lib/sxp/generator.rb', line 34

def length
  if @elements.is_a?(Array)
    @elements.map(&:length).inject(:+).to_i + @elements.length - 1
  else
    @elements.to_sxp.length
  end
end

#sxp?Boolean

Determins if this block is an SXP, or not

Returns:

  • (Boolean)


54
55
56
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sxp.rb-77c5073a7739/lib/sxp/generator.rb', line 54

def sxp?
  @elements.is_a?(Array)
end

#to_sxpString

Turn block into a string in S-expression form This should only be called on a block when no indentation is to be applied

Returns:



47
48
49
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sxp.rb-77c5073a7739/lib/sxp/generator.rb', line 47

def to_sxp
  @elements.to_sxp
end