Class: Argumenta::Objects::Proposition

Inherits:
Object
  • Object
show all
Defined in:
lib/argumenta/objects/proposition.rb

Overview

A proposition models an assertion as a string of text. It has an associated object record and SHA-1 hash. The object record is similar to Git’s blob. The SHA-1 identifies the proposition.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ Proposition

Initializes a new proposition.

text = "My proposition!"
proposition = Proposition.new text


16
17
18
# File 'lib/argumenta/objects/proposition.rb', line 16

def initialize(text)
  @text = text
end

Instance Attribute Details

#errorObject

Returns the value of attribute error.



9
10
11
# File 'lib/argumenta/objects/proposition.rb', line 9

def error
  @error
end

#textObject

Returns the value of attribute text.



9
10
11
# File 'lib/argumenta/objects/proposition.rb', line 9

def text
  @text
end

Class Method Details

.validate(object) ⇒ Object

Validates an object as a proposition.

valid = Proposition.validate object

Raises:



41
42
43
44
45
46
# File 'lib/argumenta/objects/proposition.rb', line 41

def self.validate(object)
  unless object.is_a? self
    raise ValidationError "Object must be a proposition."
  end
  object.validate
end

Instance Method Details

#recordObject

Gets the proposition’s object record.

record = proposition.record


24
25
26
# File 'lib/argumenta/objects/proposition.rb', line 24

def record
  "proposition " + @text
end

#sha1Object

Gets the proposition’s SHA-1.

sha1 = proposition.sha1


32
33
34
# File 'lib/argumenta/objects/proposition.rb', line 32

def sha1
  Digest::SHA1.hexdigest self.record
end

#valid?Boolean

Checks whether proposition is valid.

valid = proposition.valid?

Returns:

  • (Boolean)

    The validation status.



74
75
76
77
78
79
80
81
82
# File 'lib/argumenta/objects/proposition.rb', line 74

def valid?
  begin
    self.validate
    return true
  rescue ValidationError => err
    @error = err
    return false
  end
end

#validateObject

Validates the proposition.

begin
  proposition.validate
rescue Argumenta::ValidationError => err
  puts "Validation failed: ", err.message
end

Raises:



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/argumenta/objects/proposition.rb', line 57

def validate
  unless @text.is_a? String
    raise ValidationError, "Proposition text must be a string."
  end
  unless @text.length > 0 and text.match /\S+/
    raise ValidationError, "Proposition text must not be empty."
  end
  unless @text.length <= 240
    raise ValidationError, "Proposition text must be 240 characters or less."
  end
end