Skip to content
This repository
Browse code

Add support for tags.

  • Loading branch information...
commit a289d7eb948dbe6aa16b84dbf0b0624ffc92fac0 1 parent b46dcee
Louis Mullie authored

Showing 1 changed file with 51 additions and 26 deletions. Show diff stats Hide diff stats

  1. +51 26 lib/treat/core/problem.rb
77 lib/treat/core/problem.rb
@@ -4,41 +4,50 @@
4 4 # to attempt to answer that question?
5 5 class Treat::Core::Problem
6 6
  7 + # A unique identifier for the problem.
  8 + attr_accessor :id
7 9 # The question we are trying to answer.
8 10 attr_reader :question
9 11 # An array of features that will be
10 12 # looked at in trying to answer the
11 13 # problem's question.
12 14 attr_reader :features
  15 + attr_reader :tags
13 16 # Just the labels from the features.
14   - attr_reader :labels
15   - # A unique identifier for the problem.
16   - attr_accessor :id
  17 + attr_reader :feature_labels
  18 + attr_reader :tag_labels
17 19
18 20 # Initialize the problem with a question
19 21 # and an arbitrary number of features.
20   - def initialize(question, *features)
  22 + def initialize(question, *exports)
21 23 unless question.is_a?(Treat::Core::Question)
22 24 raise Treat::Exception,
23   - "The first argument to initialize should be " +
24   - "an instance of Treat::Core::Question."
  25 + "The first argument to initialize " +
  26 + "should be an instance of " +
  27 + "Treat::Core::Question."
25 28 end
26   - if features.any? { |f| !f.is_a?(Treat::Core::Feature) }
  29 + if exports.any? { |f| !f.is_a?(Treat::Core::Export) }
27 30 raise Treat::Exception,
28 31 "The second argument and all subsequent ones " +
29   - "to initialize should be instances of Treat::" +
30   - "Core::Feature."
  32 + "to initialize should be instances of subclasses " +
  33 + "of Treat::Core::Export."
  34 + end
  35 + @question, @id = question, object_id
  36 + @features = exports.select do |exp|
  37 + exp.is_a?(Treat::Core::Feature)
  38 + end
  39 + @tags = exports.select do |exp|
  40 + exp.is_a?(Treat::Core::Tag)
31 41 end
32   - @question = question
33   - @features = features
34   - @labels = @features.map { |f| f.name }
35   - @id = object_id
  42 + @feature_labels = @features.map { |f| f.name }
  43 + @tag_labels = @tags.map { |t| t.name }
36 44 end
37 45
38 46 # Custom comparison for problems.
39 47 def ==(problem)
40 48 @question == problem.question &&
41   - @features == problem.features
  49 + @features == problem.features &&
  50 + @tags == problem.tags
42 51 end
43 52
44 53 # Return an array of all the entity's
@@ -46,24 +55,33 @@ def ==(problem)
46 55 # If include_answer is set to true, will
47 56 # append the answer to the problem after
48 57 # all of the features.
49   - def export_item(e, what = :features, include_answer = true)
50   - line = []
51   - @features.each do |feature|
52   - r = feature.proc ?
53   - feature.proc.call(e) :
54   - e.send(feature.name)
55   - line << (r || feature.default)
56   - end
57   - return line unless include_answer
58   - line << (e.has?(@question.name) ?
  58 + def export_features(e, include_answer = true)
  59 + features = export(e, @features)
  60 + return features unless include_answer
  61 + features << (e.has?(@question.name) ?
59 62 e.get(@question.name) : @question.default)
60   - line
  63 + features
  64 + end
  65 +
  66 + def export_tags(e); export(e, @tags); end
  67 +
  68 + def export(entity, exports)
  69 + ret = []
  70 + exports.each do |export|
  71 + r = export.proc ?
  72 + export.proc.call(entity) :
  73 + entity.send(export.name)
  74 + ret << (r || export.default)
  75 + end
  76 + ret
61 77 end
62 78
63 79 def to_hash
64 80 {'question' => @question.to_hash,
65 81 'features' => @features.map { |f|
66 82 f.tap { |f| f.proc = nil }.to_hash },
  83 + 'tags' => @tags.map { |t|
  84 + t.tap { |t| t.proc = nil }.to_hash },
67 85 'id' => @id }
68 86 end
69 87
@@ -80,7 +98,14 @@ def self.from_hash(hash)
80 98 feature['name'], feature['default'],
81 99 feature['proc_string'])
82 100 end
83   - p = Treat::Core::Problem.new(question, *features)
  101 + tags = []
  102 + hash['tags'].each do |tag|
  103 + tags << Treat::Core::Tag.new(
  104 + tag['name'], tag['default'],
  105 + tag['proc_string'])
  106 + end
  107 + features_and_tags = features + tags
  108 + p = Treat::Core::Problem.new(question, *features_and_tags)
84 109 p.id = hash['id']
85 110 p
86 111 end

0 comments on commit a289d7e

Please sign in to comment.
Something went wrong with that request. Please try again.