Class: ClearElection::Ballot
- Inherits:
-
Object
- Object
- ClearElection::Ballot
- Defined in:
- lib/clear-election-sdk/ballot.rb
Defined Under Namespace
Constant Summary collapse
- SCHEMA_VERSION =
0.0
Instance Attribute Summary collapse
-
#ballotId ⇒ Object
readonly
Returns the value of attribute ballotId.
-
#contests ⇒ Object
readonly
Returns the value of attribute contests.
-
#demographic ⇒ Object
readonly
Returns the value of attribute demographic.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#uniquifier ⇒ Object
readonly
Returns the value of attribute uniquifier.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #as_json ⇒ Object
-
#initialize(ballotId:, uniquifier:, contests:, errors: [], demographic: nil) ⇒ Ballot
constructor
A new instance of Ballot.
- #valid? ⇒ Boolean
- #validate(election, complete: true) ⇒ Object
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
#ballotId ⇒ Object (readonly)
Returns the value of attribute ballotId.
7 8 9 |
# File 'lib/clear-election-sdk/ballot.rb', line 7 def ballotId @ballotId end |
#contests ⇒ Object (readonly)
Returns the value of attribute contests.
7 8 9 |
# File 'lib/clear-election-sdk/ballot.rb', line 7 def contests @contests end |
#demographic ⇒ Object (readonly)
Returns the value of attribute demographic.
7 8 9 |
# File 'lib/clear-election-sdk/ballot.rb', line 7 def demographic @demographic end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
7 8 9 |
# File 'lib/clear-election-sdk/ballot.rb', line 7 def errors @errors end |
#uniquifier ⇒ Object (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 = JSON::Validator.fully_validate(schema, data, insert_defaults: true, errors_as_objects: 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 |
.schema ⇒ Object
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_json ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/clear-election-sdk/ballot.rb', line 46 def as_json() data = { "version" => "0.0", "ballotId" => @ballotId, "uniquifier" => @uniquifier, "contests" => @contests.map(&:as_json), } data["demographic"] = @demographic if @demographic JSON::Validator.validate!(Ballot.schema, data) data end |
#valid? ⇒ 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 |