class Aspen::Discourse

Attributes

data[R]
grammar[R]

Public Class Methods

assert(data) click to toggle source
# File lib/aspen/discourse.rb, line 29
def self.assert(data)
  return nil if data.nil?
  case data.class.to_s
  when "Aspen::Discourse" then data
  when "Hash"   then from_hash(data)
  when "String" then from_yaml(data)
  else
    raise ArgumentError, "Must be a Hash, string (containing YAML), or Aspen::Discourse, got:\n\t#{data} (#{data.class})"
  end
end
from_hash(data = {}) click to toggle source
# File lib/aspen/discourse.rb, line 18
def self.from_hash(data = {})
  raise ArgumentError, "Must be a hash, was a #{data.class}" unless data.is_a?(Hash)
  result = Schemas::DiscourseSchema.call(data)
  if result.success?
    new(data)
  else
    # TODO: Improve this output for human readability
    raise Aspen::Error, result.errors.messages.to_s
  end
end
from_yaml(yaml) click to toggle source
# File lib/aspen/discourse.rb, line 14
def self.from_yaml(yaml)
  from_hash YAML.load(yaml)
end
new(data = {}) click to toggle source
# File lib/aspen/discourse.rb, line 40
def initialize(data = {})
  @data = data.with_indifferent_access
  @grammar = Aspen::CustomGrammar::Grammar.new
  process_grammar
end

Public Instance Methods

add_grammar(grammar) click to toggle source
# File lib/aspen/discourse.rb, line 84
def add_grammar(grammar)
  @grammar = grammar
end
allowed_edges() click to toggle source
# File lib/aspen/discourse.rb, line 60
def allowed_edges
  @ae ||= whitelist_for(:edges)
end
allowed_labels() click to toggle source
# File lib/aspen/discourse.rb, line 56
def allowed_labels
  @al ||= whitelist_for(:nodes)
end
allows_edge?(edge) click to toggle source
# File lib/aspen/discourse.rb, line 68
def allows_edge?(edge)
  allowed_edges.empty? || allowed_edges.include?(edge)
end
allows_label?(label) click to toggle source
# File lib/aspen/discourse.rb, line 64
def allows_label?(label)
  allowed_labels.empty? || allowed_labels.include?(label)
end
default_attr_name(label) click to toggle source
# File lib/aspen/discourse.rb, line 51
def default_attr_name(label)
  maybe_attr = Maybe(@data.dig(:default, :attributes, label.to_sym))
  maybe_attr.value_or(primary_default_attr_name)
end
default_label() click to toggle source
# File lib/aspen/discourse.rb, line 46
def default_label
  maybe_label = Maybe(@data.dig(:default, :label))
  maybe_label.value_or(Aspen::SystemDefault.label)
end
mutual() click to toggle source
# File lib/aspen/discourse.rb, line 72
def mutual
  maybe_list = Maybe(@data.dig(:mutual) || @data.dig(:reciprocal))
  maybe_list.value_or(Array.new)
end
Also aliased as: reciprocal
mutual?(edge_name) click to toggle source
# File lib/aspen/discourse.rb, line 77
def mutual?(edge_name)
  mutual.include? edge_name
end
Also aliased as: reciprocal?
reciprocal()
Alias for: mutual
reciprocal?(edge_name)
Alias for: mutual?

Private Instance Methods

configured_grammar() click to toggle source
# File lib/aspen/discourse.rb, line 117
def configured_grammar
  @cg ||= Maybe(@data.dig(:grammar)).value_or(false)
end
primary_default_attr_name() click to toggle source
# File lib/aspen/discourse.rb, line 90
def primary_default_attr_name
  maybe_attr = Maybe(@data.dig(:default, :attribute))
  maybe_attr.value_or(Aspen::SystemDefault.attr_name)
end
process_grammar() click to toggle source

Converts multiple lines

# File lib/aspen/discourse.rb, line 103
def process_grammar
  return false unless configured_grammar
  configured_grammar.each do |block|
    Array(block.fetch(:match)).each do |expression|
      matcher = Aspen::CustomGrammar::Matcher.new(
        expression: expression,
        template:   block.fetch(:template),
        pattern:    Aspen::CustomGrammar.compile_pattern(expression)
      )
      grammar.add(matcher)
    end
  end
end
whitelist_for(stuff) click to toggle source
# File lib/aspen/discourse.rb, line 95
def whitelist_for(stuff)
  maybe_whitelist = Maybe(@data.dig(:allow_only, stuff))
  list = maybe_whitelist.value_or([])
  return list if list.is_a? Array  # If it's already a YAML list, great.
  list.split(",").map(&:strip)      # Otherwise, split the comma-separated string
end