Class: Aspen::Discourse

Inherits:
Object
  • Object
show all
Defined in:
lib/aspen/discourse.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Discourse

Returns a new instance of Discourse.



40
41
42
43
44
# File 'lib/aspen/discourse.rb', line 40

def initialize(data = {})
  @data = data.with_indifferent_access
  @grammar = Aspen::CustomGrammar::Grammar.new
  process_grammar
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



12
13
14
# File 'lib/aspen/discourse.rb', line 12

def data
  @data
end

#grammarObject (readonly)

Returns the value of attribute grammar.



12
13
14
# File 'lib/aspen/discourse.rb', line 12

def grammar
  @grammar
end

Class Method Details

.assert(data) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# 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 = {}) ⇒ Object

Raises:



18
19
20
21
22
23
24
25
26
27
# 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) ⇒ Object



14
15
16
# File 'lib/aspen/discourse.rb', line 14

def self.from_yaml(yaml)
  from_hash YAML.load(yaml)
end

Instance Method Details

#add_grammar(grammar) ⇒ Object



84
85
86
# File 'lib/aspen/discourse.rb', line 84

def add_grammar(grammar)
  @grammar = grammar
end

#allowed_edgesObject



60
61
62
# File 'lib/aspen/discourse.rb', line 60

def allowed_edges
  @ae ||= whitelist_for(:edges)
end

#allowed_labelsObject



56
57
58
# File 'lib/aspen/discourse.rb', line 56

def allowed_labels
  @al ||= whitelist_for(:nodes)
end

#allows_edge?(edge) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/aspen/discourse.rb', line 68

def allows_edge?(edge)
  allowed_edges.empty? || allowed_edges.include?(edge)
end

#allows_label?(label) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/aspen/discourse.rb', line 64

def allows_label?(label)
  allowed_labels.empty? || allowed_labels.include?(label)
end

#default_attr_name(label) ⇒ Object



51
52
53
54
# 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_labelObject



46
47
48
49
# 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

#mutualObject Also known as: reciprocal



72
73
74
75
# 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

#mutual?(edge_name) ⇒ Boolean Also known as: reciprocal?

Returns:

  • (Boolean)


77
78
79
# File 'lib/aspen/discourse.rb', line 77

def mutual?(edge_name)
  mutual.include? edge_name
end