Class: AnswerFactory::Answer
- Inherits:
-
Object
- Object
- AnswerFactory::Answer
- Defined in:
- lib/answers/answer.rb
Instance Attribute Summary collapse
-
#ancestors ⇒ Object
readonly
Returns the value of attribute ancestors.
-
#couch_id ⇒ Object
Returns the value of attribute couch_id.
-
#couch_rev ⇒ Object
Returns the value of attribute couch_rev.
-
#draft_blueprint ⇒ Object
readonly
Returns the value of attribute draft_blueprint.
-
#initialization_options ⇒ Object
readonly
Returns the value of attribute initialization_options.
-
#location ⇒ Object
Returns the value of attribute location.
-
#program ⇒ Object
readonly
Returns the value of attribute program.
-
#progress ⇒ Object
readonly
Returns the value of attribute progress.
-
#scores ⇒ Object
Returns the value of attribute scores.
-
#tags ⇒ Object
Returns the value of attribute tags.
-
#timestamp ⇒ Object
readonly
Returns the value of attribute timestamp.
Instance Method Summary collapse
- #blueprint ⇒ Object
- #build_from_blueprint!(blueprint) ⇒ Object
- #delete_point_or_clone(which) ⇒ Object
- #dominated_by?(other_answer, comparison_criteria = self.known_criteria) ⇒ Boolean
-
#initialize(blueprint, options = {}) ⇒ Answer
constructor
A new instance of Answer.
- #known_criteria ⇒ Object
- #parses? ⇒ Boolean
- #points ⇒ Object
- #replace_point_or_clone(which, object) ⇒ Object
- #score_vector(ordering = known_criteria) ⇒ Object
Constructor Details
#initialize(blueprint, options = {}) ⇒ Answer
Returns a new instance of Answer.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/answers/answer.rb', line 11 def initialize(blueprint, = {}) raise ArgumentError, "Answer cannot be initialized with a #{blueprint.class}" unless blueprint.kind_of?(String) || blueprint.kind_of?(NudgeProgram) build_from_blueprint!(blueprint) @scores = [:scores] || Hash.new do |hash, key| raise ArgumentError, "scores must use symbols as keys" unless key.kind_of?(Symbol) nil end @timestamp = Time.now @couch_id = [:couch_id] || "" @location = [:location] || :NOWHERE @couch_rev = [:couch_rev] || "" @progress = [:progress] || 0 @ancestors = [:ancestors] || [] @tags = Set.new([:tags]) || Set.new @initialization_options = end |
Instance Attribute Details
#ancestors ⇒ Object (readonly)
Returns the value of attribute ancestors.
6 7 8 |
# File 'lib/answers/answer.rb', line 6 def ancestors @ancestors end |
#couch_id ⇒ Object
Returns the value of attribute couch_id.
8 9 10 |
# File 'lib/answers/answer.rb', line 8 def couch_id @couch_id end |
#couch_rev ⇒ Object
Returns the value of attribute couch_rev.
8 9 10 |
# File 'lib/answers/answer.rb', line 8 def couch_rev @couch_rev end |
#draft_blueprint ⇒ Object (readonly)
Returns the value of attribute draft_blueprint.
6 7 8 |
# File 'lib/answers/answer.rb', line 6 def draft_blueprint @draft_blueprint end |
#initialization_options ⇒ Object (readonly)
Returns the value of attribute initialization_options.
7 8 9 |
# File 'lib/answers/answer.rb', line 7 def @initialization_options end |
#location ⇒ Object
Returns the value of attribute location.
5 6 7 |
# File 'lib/answers/answer.rb', line 5 def location @location end |
#program ⇒ Object (readonly)
Returns the value of attribute program.
6 7 8 |
# File 'lib/answers/answer.rb', line 6 def program @program end |
#progress ⇒ Object (readonly)
Returns the value of attribute progress.
7 8 9 |
# File 'lib/answers/answer.rb', line 7 def progress @progress end |
#scores ⇒ Object
Returns the value of attribute scores.
5 6 7 |
# File 'lib/answers/answer.rb', line 5 def scores @scores end |
#tags ⇒ Object
Returns the value of attribute tags.
5 6 7 |
# File 'lib/answers/answer.rb', line 5 def @tags end |
#timestamp ⇒ Object (readonly)
Returns the value of attribute timestamp.
6 7 8 |
# File 'lib/answers/answer.rb', line 6 def @timestamp end |
Instance Method Details
#blueprint ⇒ Object
43 44 45 |
# File 'lib/answers/answer.rb', line 43 def blueprint @program.blueprint end |
#build_from_blueprint!(blueprint) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/answers/answer.rb', line 32 def build_from_blueprint!(blueprint) if blueprint.kind_of?(String) @draft_blueprint = blueprint @program = NudgeProgram.new(blueprint) else @program = blueprint @draft_blueprint = @program.blueprint end end |
#delete_point_or_clone(which) ⇒ Object
89 90 91 92 93 |
# File 'lib/answers/answer.rb', line 89 def delete_point_or_clone(which) ((1..self.points).include?(which)) ? self.program.delete_point(which) : self.program.deep_copy end |
#dominated_by?(other_answer, comparison_criteria = self.known_criteria) ⇒ Boolean
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/answers/answer.rb', line 63 def dominated_by?(other_answer, comparison_criteria = self.known_criteria) return false unless (known_criteria & comparison_criteria) == (other_answer.known_criteria & comparison_criteria) could_be_identical = true comparison_criteria.each do |score| return false if (my_score = self.scores[score]) < (other_score = other_answer.scores[score]) if could_be_identical could_be_identical &&= (my_score == other_score) end end return !could_be_identical rescue NoMethodError false end |
#known_criteria ⇒ Object
53 54 55 |
# File 'lib/answers/answer.rb', line 53 def known_criteria @scores.keys.sort end |
#parses? ⇒ Boolean
48 49 50 |
# File 'lib/answers/answer.rb', line 48 def parses? @program.parses? end |
#points ⇒ Object
84 85 86 |
# File 'lib/answers/answer.rb', line 84 def points @program.points end |
#replace_point_or_clone(which, object) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/answers/answer.rb', line 96 def replace_point_or_clone(which, object) if object.kind_of?(String) prog = NudgeProgram.new(object) if !prog.parses? raise(ArgumentError, "Replacement point cannot be parsed") else new_point = prog.linked_code end elsif object.kind_of?(ProgramPoint) new_point = object else raise(ArgumentError, "Program points cannot be replaced by #{object.class} objects") end ((1..self.points).include?(which)) ? self.program.replace_point(which, new_point) : self.program.deep_copy end |
#score_vector(ordering = known_criteria) ⇒ Object
58 59 60 |
# File 'lib/answers/answer.rb', line 58 def score_vector(ordering = known_criteria) ordering.collect {|k| @scores[k]} end |