class Bob::Compiler::Base
Public Class Methods
new()
click to toggle source
# File lib/bob/compiler/base.rb, line 9 def initialize @buffer = Buffer.new @warnings = [] @errors = [] @compiled = false end
Public Instance Methods
errors()
click to toggle source
# File lib/bob/compiler/base.rb, line 20 def errors ensure_compiled { @errors } end
errors?()
click to toggle source
# File lib/bob/compiler/base.rb, line 32 def errors? ensure_compiled { @errors.present? } end
result()
click to toggle source
# File lib/bob/compiler/base.rb, line 40 def result ensure_compiled { @buffer.string } end
Also aliased as: to_s
valid?()
click to toggle source
# File lib/bob/compiler/base.rb, line 36 def valid? ensure_compiled { @errors.blank? } end
warnings()
click to toggle source
# File lib/bob/compiler/base.rb, line 16 def warnings ensure_compiled { @warnings } end
warnings?()
click to toggle source
# File lib/bob/compiler/base.rb, line 24 def warnings? ensure_compiled { @warnings.present? } end
well_formed?()
click to toggle source
# File lib/bob/compiler/base.rb, line 28 def well_formed? ensure_compiled { valid? && @warnings.blank? } end
Protected Instance Methods
buffer()
click to toggle source
# File lib/bob/compiler/base.rb, line 47 def buffer ensure_compiled { @buffer } end
Private Instance Methods
compile()
click to toggle source
# File lib/bob/compiler/base.rb, line 66 def compile end
compile!()
click to toggle source
# File lib/bob/compiler/base.rb, line 61 def compile! @compiled = true compile end
compiled?()
click to toggle source
# File lib/bob/compiler/base.rb, line 57 def compiled? @compiled end
ensure_compiled() { || ... }
click to toggle source
# File lib/bob/compiler/base.rb, line 52 def ensure_compiled compile! unless compiled? yield end
error(message, node = nil)
click to toggle source
# File lib/bob/compiler/base.rb, line 73 def error(message, node = nil) @errors << Message.new(message, node) end
identifier_from_attribute(node, attribute, required = true)
click to toggle source
# File lib/bob/compiler/base.rb, line 77 def identifier_from_attribute(node, attribute, required = true) sanitize = nil if node.has_attribute?(attribute) identifier = node[attribute] sanitized = identifier && sanitize_identifier(identifier) if required && identifier.blank? error "Required attribute `#{attribute}` cannot be blank", node elsif required && sanitized.blank? error "Attribute `#{attribute}` does not contain a valid identifier", node elsif sanitized != identifier warn "Attribute `#{attribute}` does not contain a valid identifier, assuming you meant #{sanitized.inspect}", node end elsif required error "Missing required attribute `#{attribute}`", node end sanitized end
pretty_quote(arg)
click to toggle source
# File lib/bob/compiler/base.rb, line 127 def pretty_quote(arg) JSON.pretty_generate(arg, quirks_mode: true) end
quote(arg)
click to toggle source
# File lib/bob/compiler/base.rb, line 123 def quote(arg) JSON.generate(arg, quirks_mode: true) end
sanitize_identifier(identifier)
click to toggle source
# File lib/bob/compiler/base.rb, line 99 def sanitize_identifier(identifier) sanitized = identifier.downcase # Replace all disallowed characters with a hyphen sanitized.gsub! /[^a-z0-9\-\/]+/, '-' # Each part must begin with a letter sanitized.gsub! /(?<=\A|\/)[^a-z]+/, '' # Remove any trailing hyphens sanitized.gsub! /-+(?=\z|\/)/, '' # Collapse multiple hyphens into one sanitized.gsub! /-+/, '-' # Collapse multiple slashes into one sanitized.gsub! /\/+/, '/' # Remove leading or trailing slashes sanitized.gsub! /\A\/|\/\z/, '' sanitized end
warn(message, node = nil)
click to toggle source
# File lib/bob/compiler/base.rb, line 69 def warn(message, node = nil) @warnings << Message.new(message, node) end