class Engrel::Sentence

Public Class Methods

claim(subject, verb, direct_object) { |s| ... } click to toggle source

How we make a new sentence while ensuring an existing one doesn’t exist.

# File lib/engrel/sentence.rb, line 66
def self.claim(subject, verb, direct_object)
  params = {:subject_type => subject.class.to_s, :subject_id => subject[:id], :direct_object_type => direct_object.class.to_s, :direct_object_id => direct_object[:id], :verb => verb.to_s}.stringify_keys
  s = self.where(params).first
  s = create(params) if s.blank?
  s.reload
  block_given? ? yield(s) : s
end

Public Instance Methods

has_direct_object?() click to toggle source
# File lib/engrel/sentence.rb, line 89
def has_direct_object?
  (self.direct_object.present? && self.direct_object[:id].present?) rescue false
end
has_subject?() click to toggle source
# File lib/engrel/sentence.rb, line 85
def has_subject?
  (self.subject.present? && self.subject[:id].present?) rescue false
end
object_list() click to toggle source
# File lib/engrel/sentence.rb, line 81
def object_list
  [ subject, direct_object ].concat(indirect_objects).compact
end
only(*args) click to toggle source
# File lib/engrel/sentence.rb, line 102
def only(*args)
  self.prepositional_phrases.clear
  prep(*args)
  self
end
prep(prep, indirect_object) click to toggle source
# File lib/engrel/sentence.rb, line 108
def prep(prep, indirect_object)
  if self.prepositional_phrases.include?(preposition: prep, indirect_object_id: indirect_object.id, indirect_object_type: indirect_object.class.to_s)
    warn "PREP ALREADY EXISTS!"
  else
    self.prepositional_phrases.create(preposition: prep, indirect_object: indirect_object)
  end
  self
end
to_sentence() click to toggle source

TODO: Properly strip ANSI color chars

# File lib/engrel/sentence.rb, line 128
def to_sentence
  to_sentence_colored
end
to_sentence_colored() click to toggle source
# File lib/engrel/sentence.rb, line 117
def to_sentence_colored
  sen = "#{subject_type.humanize}[#{subject.name rescue subject_id}\##{subject_id}]".green +
        " #{verb_label.downcase} " + "#{direct_object_type.humanize}[#{direct_object.name rescue direct_object_id}\##{direct_object_id}]".cyan
  sen += " " unless prepositional_phrases.blank? rescue true
  preps = prepositional_phrases.collect { |p| "#{p.preposition.to_s.humanize.downcase} " + "#{p.indirect_object_type.humanize}[#{p.object.name rescue p.indirect_object_id}\##{p.indirect_object_id}]".yellow }.join(" ") rescue nil
  sen << preps unless preps.blank?
  sen += '.'
  sen
end
verb_enums() click to toggle source
# File lib/engrel/sentence.rb, line 93
def verb_enums
  self.enums(:verb)
end
verb_label(element = nil) click to toggle source
# File lib/engrel/sentence.rb, line 97
def verb_label(element = nil)
  element ||= self.verb
  verb_enums.label(element.to_sym)
end