Class: ClearElection::Ballot

Inherits:
Object
  • Object
show all
Defined in:
lib/clear-election-sdk/ballot.rb

Defined Under Namespace

Classes: Choice, Contest

Constant Summary collapse

SCHEMA_VERSION =
0.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ballotId:, uniquifier:, contests:, errors: [], demographic: nil) ⇒ Ballot

Returns a new instance of Ballot.



25
26
27
28
29
30
31
# File 'lib/clear-election-sdk/ballot.rb', line 25

def initialize(ballotId:, uniquifier:, contests:, errors: [], demographic: nil)
  @ballotId = ballotId
  @uniquifier = uniquifier
  @contests = contests
  @demographic = demographic
  @errors = errors
end

Instance Attribute Details

#ballotIdObject (readonly)

Returns the value of attribute ballotId.



7
8
9
# File 'lib/clear-election-sdk/ballot.rb', line 7

def ballotId
  @ballotId
end

#contestsObject (readonly)

Returns the value of attribute contests.



7
8
9
# File 'lib/clear-election-sdk/ballot.rb', line 7

def contests
  @contests
end

#demographicObject (readonly)

Returns the value of attribute demographic.



7
8
9
# File 'lib/clear-election-sdk/ballot.rb', line 7

def demographic
  @demographic
end

#errorsObject (readonly)

Returns the value of attribute errors.



7
8
9
# File 'lib/clear-election-sdk/ballot.rb', line 7

def errors
  @errors
end

#uniquifierObject (readonly)

Returns the value of attribute uniquifier.



7
8
9
# File 'lib/clear-election-sdk/ballot.rb', line 7

def uniquifier
  @uniquifier
end

Class Method Details

.from_json(data) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/clear-election-sdk/ballot.rb', line 13

def self.from_json(data)
  errors = ClearElection::Schema.validate(schema, data, insert_defaults: true)
  return self.new(ballotId: nil, uniquifier: nil, contests: [], errors: errors) unless errors.empty?

  self.new(
    ballotId: data["ballotId"],
    uniquifier: data["uniquifier"],
    contests: data["contests"].map { |data| Contest.from_json(data) },
    demographic: data["demographic"]
  )
end

.schemaObject



9
10
11
# File 'lib/clear-election-sdk/ballot.rb', line 9

def self.schema
  ClearElection::Schema.ballot(version: SCHEMA_VERSION)
end

Instance Method Details

#<=>(other) ⇒ Object



58
59
60
# File 'lib/clear-election-sdk/ballot.rb', line 58

def <=>(other)
  [self.ballotId, self.contests] <=> [other.ballotId, other.contests]
end

#as_jsonObject



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/clear-election-sdk/ballot.rb', line 46

def as_json()
  data = {
    "clearelection-ballot" => "v0.0",
    "ballotId" => @ballotId,
    "uniquifier" => @uniquifier,
    "contests" => @contests.map(&:as_json),
  }
  data["demographic"] = @demographic if @demographic
  ClearElection::Schema.validate!(Ballot.schema, data)
  data
end

#valid?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/clear-election-sdk/ballot.rb', line 42

def valid?
  @errors.empty?
end

#validate(election, complete: true) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/clear-election-sdk/ballot.rb', line 33

def validate(election, complete: true)
  @contests.each do |contest|
    @errors += contest.validate(election)
  end
  if complete and not (missing = election.contests.map(&:contestId) - @contests.map(&:contestId)).empty?
    @errors += missing.map { |contestId| { contestId: contestId, message: "missing contest" } }
  end
end