Class: SXP::Generator::Block

Inherits:
Object
  • Object
show all
Defined in:
lib/sxp/generator.rb

Overview

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

Constant Summary collapse

BLOCK_MIN_LENGTH =
80

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj, indent, prefixes: nil, base_uri: nil) ⇒ Block

Returns a new instance of Block.

Parameters:

  • obj (Object)
  • indent (Integer)
  • prefixes (Hash{Symbol => RDF::URI}) (defaults to: nil)

    (nil)

  • base_uri (RDF::URI) (defaults to: nil)

    (nil)



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sxp/generator.rb', line 22

def initialize(obj, indent, prefixes: nil, base_uri: nil)
  @indent = indent
  @elements = []
  @prefixes = prefixes
  @base_uri = base_uri
  if obj.is_a?(Array)
    # If this is a base or prefix element, update our representations
    if obj.first == :base && obj.length == 3 && obj[1].is_a?(RDF::URI)
      base_uri = obj[1]
      @elements << Block.new(:base, indent + 1)
      @elements << Block.new(obj[1], indent + 1)
      @elements << Block.new(obj.last, indent + 1, prefixes: prefixes, base_uri: base_uri)
    elsif obj.first == :prefix && obj.length == 3 && obj[1].is_a?(Array)
      prefixes = prefixes ? prefixes.dup : {}
      obj[1].each do |defn|
        prefixes[defn.first.to_s.chomp(':').to_sym] = RDF::URI(defn.last) if defn.is_a?(Array) && defn.length == 2
      end
      @elements << Block.new(:prefix, indent + 1)
      @elements << Block.new(obj[1], indent + 1)
      @elements << Block.new(obj.last, indent + 1, prefixes: prefixes, base_uri: base_uri)
    else          
      obj.compact.each do |o|
        @elements << Block.new(o, indent + 1, prefixes: prefixes, base_uri: base_uri)
      end
    end
  else
    @elements = obj
  end
end

Instance Attribute Details

#indentObject (readonly)



15
16
17
# File 'lib/sxp/generator.rb', line 15

def indent
  @indent
end

Instance Method Details

#formattedString

Format block

Returns:



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/sxp/generator.rb', line 84

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(prefixes: @prefixes, base_uri: @base_uri) + "\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(prefixes: @prefixes, base_uri: @base_uri) + "\n"
  end
  buffer
end

#lengthInteger

Aggregate length over each element accounting for spaces

Returns:

  • (Integer)

    If indent is not not nil, returns zero



57
58
59
60
61
62
63
# File 'lib/sxp/generator.rb', line 57

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

#sxp?Boolean

Determins if this block is an SXP, or not

Returns:

  • (Boolean)


77
78
79
# File 'lib/sxp/generator.rb', line 77

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

#to_sxp(prefixes: nil, base_uri: nil) ⇒ String

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:



70
71
72
# File 'lib/sxp/generator.rb', line 70

def to_sxp(prefixes: nil, base_uri: nil)
  @elements.to_sxp(prefixes: prefixes || @prefixes, base_uri: base_uri || @base_uri)
end