Class: SXP::Generator::Block
Overview
A basic block containing constituent objects, either blocks or strings.
Constant Summary collapse
- BLOCK_MIN_LENGTH =
80
Instance Attribute Summary collapse
- #indent ⇒ Object readonly
Instance Method Summary collapse
-
#formatted ⇒ String
Format block.
-
#initialize(obj, indent, prefixes: nil, base_uri: nil) ⇒ Block
constructor
A new instance of Block.
-
#length ⇒ Integer
Aggregate length over each element accounting for spaces.
-
#sxp? ⇒ Boolean
Determins if this block is an SXP, or not.
-
#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.
Constructor Details
#initialize(obj, indent, prefixes: nil, base_uri: nil) ⇒ Block
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
#indent ⇒ Object (readonly)
15 16 17 |
# File 'lib/sxp/generator.rb', line 15 def indent @indent end |
Instance Method Details
#formatted ⇒ String
Format block
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 |
#length ⇒ Integer
Aggregate length over each element accounting for spaces
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
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
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 |