Class: AnswerFactory::Answer

Inherits:
Object
  • Object
show all
Defined in:
lib/answers/answer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(blueprint, options = {}) ⇒ Answer

Returns a new instance of Answer.

Raises:

  • (ArgumentError)


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, options = {})
  raise ArgumentError, "Answer cannot be initialized with a #{blueprint.class}" unless
    blueprint.kind_of?(String) || blueprint.kind_of?(NudgeProgram)
  build_from_blueprint!(blueprint)
  
  @scores = options[: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 = options[:couch_id] || ""
  @location = options[:location] || :NOWHERE
  @couch_rev = options[:couch_rev] || ""
  @progress = options[:progress] || 0
  @ancestors = options[:ancestors] || []
  @tags = Set.new(options[:tags]) || Set.new
  
  @initialization_options = options
end

Instance Attribute Details

#ancestorsObject (readonly)

Returns the value of attribute ancestors.



6
7
8
# File 'lib/answers/answer.rb', line 6

def ancestors
  @ancestors
end

#couch_idObject

Returns the value of attribute couch_id.



8
9
10
# File 'lib/answers/answer.rb', line 8

def couch_id
  @couch_id
end

#couch_revObject

Returns the value of attribute couch_rev.



8
9
10
# File 'lib/answers/answer.rb', line 8

def couch_rev
  @couch_rev
end

#draft_blueprintObject (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_optionsObject (readonly)

Returns the value of attribute initialization_options.



7
8
9
# File 'lib/answers/answer.rb', line 7

def initialization_options
  @initialization_options
end

#locationObject

Returns the value of attribute location.



5
6
7
# File 'lib/answers/answer.rb', line 5

def location
  @location
end

#programObject (readonly)

Returns the value of attribute program.



6
7
8
# File 'lib/answers/answer.rb', line 6

def program
  @program
end

#progressObject (readonly)

Returns the value of attribute progress.



7
8
9
# File 'lib/answers/answer.rb', line 7

def progress
  @progress
end

#scoresObject

Returns the value of attribute scores.



5
6
7
# File 'lib/answers/answer.rb', line 5

def scores
  @scores
end

#tagsObject

Returns the value of attribute tags.



5
6
7
# File 'lib/answers/answer.rb', line 5

def tags
  @tags
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



6
7
8
# File 'lib/answers/answer.rb', line 6

def timestamp
  @timestamp
end

Instance Method Details

#blueprintObject



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

Returns:

  • (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_criteriaObject



53
54
55
# File 'lib/answers/answer.rb', line 53

def known_criteria
  @scores.keys.sort
end

#parses?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/answers/answer.rb', line 48

def parses?
  @program.parses?
end

#pointsObject



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