friends 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 018ee9e9043561fd60353d6db09a37ca42822a8d
4
- data.tar.gz: 0591a729fb07a42bcd4ea872e280d849f6eb1bea
3
+ metadata.gz: fb75dfa2c1651767156d0c51f6a48f17cdcd6014
4
+ data.tar.gz: 36e29e9c4bb81409a7ed39bc6d9e5d2f7c396b41
5
5
  SHA512:
6
- metadata.gz: fa12a4235f2aa2b5d24f6a507e872d8509caa06c356de44cf564c528e73767f181ea8f7d5ec459e48b95c221992d767f5a00844e87eeab9894bd05e82231859b
7
- data.tar.gz: a8543253d63c267ac33a7dbb9535f6465f97df093528ac181d84325bc3b14728a98bd88366adf6191c2a3db075e683d0752d76909c401fe326b7dd14f1d06e83
6
+ metadata.gz: 2c8d25accc7fe8beacd6dda24dce6b0c0c4f8d515c2c7ca0f6462cf3e90ec05136b4111b3791086cf7bc4a54b8ad04df4a6f059efb25642005d3a2c2cb40447c
7
+ data.tar.gz: 8d54d8b6a133e620442d5176e18d9c496ad4669cb8d2c140b947a1d76664e46eedf552ea50f53e2dc7adae79f029881732c4725d3053dba3c091b3bfc16ef0bc
data/.gitignore CHANGED
@@ -14,3 +14,4 @@
14
14
  mkmf.log
15
15
  *.gem
16
16
  ideas.txt
17
+ TODO.txt
@@ -51,33 +51,39 @@ module Friends
51
51
  # Modify the description to turn inputted friend names
52
52
  # (e.g. "Jacob" or "Jacob Evelyn") into full asterisk'd names
53
53
  # (e.g. "**Jacob Evelyn**")
54
+ # @param introvert [Introvert] for use in aggregate computations
54
55
  # @param friends [Array] list of friends to highlight in the description
55
56
  # @raise [FriendsError] if more than one friend matches a part of the
56
57
  # description
57
- def highlight_friends(friends:)
58
+ def highlight_friends(introvert:, friends:)
58
59
  # Map each friend to a list of all possible regexes for that friend.
59
60
  friend_regexes = {}
60
- friends.each { |f| friend_regexes[f.name] = regexes_for_name(f.name) }
61
+ friends.each { |f| friend_regexes[f] = regexes_for_name(f.name) }
61
62
 
62
- # Create hash mapping regex to friend name. Note that because two friends
63
- # may have the same regex (e.g. /John/), we need to store the names in an
63
+ # Create hash mapping regex to friend. Note that because two friends may
64
+ # have the same regex (e.g. /John/), we need to store the names in an
64
65
  # array since there may be more than one. We also iterate through the
65
66
  # regexes to add the most important regexes to the hash first, so
66
67
  # "Jacob Evelyn" takes precedence over all instances of "Jacob" (since
67
68
  # Ruby hashes are ordered).
68
69
  regex_map = Hash.new { |h, k| h[k] = [] }
69
70
  while !friend_regexes.empty?
70
- friend_regexes.each do |friend_name, regex_list|
71
- regex_map[regex_list.shift] << friend_name
72
- friend_regexes.delete(friend_name) if regex_list.empty?
71
+ friend_regexes.each do |friend, regex_list|
72
+ regex_map[regex_list.shift] << friend
73
+ friend_regexes.delete(friend) if regex_list.empty?
73
74
  end
74
75
  end
75
76
 
76
- # Go through the description and substitute in full, asterisk'd names for
77
+ # Go through the description and substitute full, asterisk'd names for
77
78
  # anything that matches a friend's name.
78
79
  new_description = description.clone
79
- regex_map.each do |regex, names|
80
- new_description.gsub!(regex, "**#{names.first}**") if names.size == 1
80
+ regex_map.each do |regex, friends|
81
+ if friends.size > 1 # If there are multiple matches, find best friend.
82
+ introvert.set_n_activities!
83
+ friends.sort_by! { |friend| -friend.n_activities }
84
+ end
85
+
86
+ new_description.gsub!(regex, "**#{friends.first.name}**")
81
87
  end
82
88
 
83
89
  @description = new_description
@@ -30,6 +30,13 @@ module Friends
30
30
  "#{SERIALIZATION_PREFIX}#{name}"
31
31
  end
32
32
 
33
+ # The number of activities this friend is in. This is for internal use only
34
+ # and is set by the Introvert as needed.
35
+ attr_writer :n_activities
36
+ def n_activities
37
+ @n_activities || 0
38
+ end
39
+
33
40
  private
34
41
 
35
42
  # Default sorting for an array of friends is alphabetical.
@@ -79,7 +79,7 @@ module Friends
79
79
  raise FriendsError, e
80
80
  end
81
81
 
82
- activity.highlight_friends(friends: friends)
82
+ activity.highlight_friends(introvert: self, friends: friends)
83
83
  activities << activity
84
84
  clean # Write a cleaned file.
85
85
 
@@ -102,24 +102,15 @@ module Friends
102
102
  raise FriendsError, "Favorites limit must be positive or unlimited"
103
103
  end
104
104
 
105
- # Construct a hash of friend name to frequency of appearance.
106
- freq_table = Hash.new { |h, k| h[k] = 0 }
107
- activities.each do |activity|
108
- activity.friend_names.each do |friend_name|
109
- freq_table[friend_name] += 1
110
- end
111
- end
112
-
113
- # Remove names that are not in the friends list.
114
- freq_table.select! { |name, _| friend_with_exact_name(name) }
105
+ set_n_activities! # Set n_activities for all friends.
115
106
 
116
107
  # Sort the results, with the most favorite friend first.
117
- results = freq_table.sort_by { |_, count| -count }
108
+ results = friends.sort_by { |friend| -friend.n_activities }
118
109
 
119
110
  # If we need to, trim the list.
120
111
  results = results.take(limit) unless limit.nil?
121
112
 
122
- results.map(&:first)
113
+ results.map(&:name)
123
114
  end
124
115
 
125
116
  # List all activity details.
@@ -153,6 +144,23 @@ module Friends
153
144
  acts.map(&:display_text)
154
145
  end
155
146
 
147
+ # Sets the n_activities field on each friend.
148
+ def set_n_activities!
149
+ # Construct a hash of friend name to frequency of appearance.
150
+ freq_table = Hash.new { |h, k| h[k] = 0 }
151
+ activities.each do |activity|
152
+ activity.friend_names.each do |friend_name|
153
+ freq_table[friend_name] += 1
154
+ end
155
+ end
156
+
157
+ # Remove names that are not in the friends list.
158
+ freq_table.each do |name, count|
159
+ friend = friend_with_exact_name(name)
160
+ friend.n_activities = count if friend # Do nothing if name not valid.
161
+ end
162
+ end
163
+
156
164
  private
157
165
 
158
166
  # Gets the list of friends as read from the file.
@@ -1,3 +1,3 @@
1
1
  module Friends
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -74,7 +74,10 @@ describe Friends::Activity do
74
74
  let(:friend2) { Friends::Friend.new(name: "John Cage") }
75
75
  let(:friends) { [friend1, friend2] }
76
76
  let(:description) { "Lunch with #{friend1.name} and #{friend2.name}." }
77
- subject { activity.highlight_friends(friends: friends) }
77
+ let(:introvert) { Minitest::Mock.new }
78
+ subject do
79
+ activity.highlight_friends(introvert: introvert, friends: friends)
80
+ end
78
81
 
79
82
  it "finds all friends" do
80
83
  subject
@@ -87,7 +90,7 @@ describe Friends::Activity do
87
90
  date_s: Date.today.to_s,
88
91
  description: "Lunch with Elizabeth and John."
89
92
  )
90
- activity.highlight_friends(friends: friends)
93
+ activity.highlight_friends(introvert: introvert, friends: friends)
91
94
  activity.description.
92
95
  must_equal "Lunch with **#{friend1.name}** and **#{friend2.name}**."
93
96
  end
@@ -97,27 +100,17 @@ describe Friends::Activity do
97
100
  date_s: Date.today.to_s,
98
101
  description: "Lunch with elizabeth cady stanton."
99
102
  )
100
- activity.highlight_friends(friends: friends)
103
+ activity.highlight_friends(introvert: introvert, friends: friends)
101
104
  activity.description.
102
105
  must_equal "Lunch with **Elizabeth Cady Stanton**."
103
106
  end
104
107
 
105
- it "ignores when there are multiple matches" do
106
- friend2.name = "Elizabeth II"
107
- activity = Friends::Activity.new(
108
- date_s: Date.today.to_s,
109
- description: "Dinner with Elizabeth."
110
- )
111
- activity.highlight_friends(friends: friends)
112
- activity.description.must_equal "Dinner with Elizabeth." # No match found.
113
- end
114
-
115
108
  it "ignores when at beginning of word" do
116
109
  activity = Friends::Activity.new(
117
110
  date_s: Date.today.to_s,
118
111
  description: "Field trip to the Johnson Co."
119
112
  )
120
- activity.highlight_friends(friends: friends)
113
+ activity.highlight_friends(introvert: introvert, friends: friends)
121
114
 
122
115
  # No match found.
123
116
  activity.description.must_equal "Field trip to the Johnson Co."
@@ -128,7 +121,7 @@ describe Friends::Activity do
128
121
  date_s: Date.today.to_s,
129
122
  description: "Field trip to the JimJohnJames Co."
130
123
  )
131
- activity.highlight_friends(friends: friends)
124
+ activity.highlight_friends(introvert: introvert, friends: friends)
132
125
 
133
126
  # No match found.
134
127
  activity.description.must_equal "Field trip to the JimJohnJames Co."
@@ -139,7 +132,7 @@ describe Friends::Activity do
139
132
  date_s: Date.today.to_s,
140
133
  description: "Field trip to the JimJohn Co."
141
134
  )
142
- activity.highlight_friends(friends: friends)
135
+ activity.highlight_friends(introvert: introvert, friends: friends)
143
136
 
144
137
  # No match found.
145
138
  activity.description.must_equal "Field trip to the JimJohn Co."
@@ -150,7 +143,7 @@ describe Friends::Activity do
150
143
  date_s: Date.today.to_s,
151
144
  description: "Dinner with **Elizabeth Cady Stanton."
152
145
  )
153
- activity.highlight_friends(friends: friends)
146
+ activity.highlight_friends(introvert: introvert, friends: friends)
154
147
 
155
148
  # No match found.
156
149
  activity.description.must_equal "Dinner with **Elizabeth Cady Stanton."
@@ -164,11 +157,31 @@ describe Friends::Activity do
164
157
  # match, because the Elizabeth isn't surrounded by asterisks.
165
158
  description: "Dinner with Elizabeth**."
166
159
  )
167
- activity.highlight_friends(friends: friends)
160
+ activity.highlight_friends(introvert: introvert, friends: friends)
168
161
 
169
162
  # No match found.
170
163
  activity.description.must_equal "Dinner with Elizabeth**."
171
164
  end
165
+
166
+ it "chooses the better friend when there are multiple matches" do
167
+ friend2.name = "Elizabeth II"
168
+ activity = Friends::Activity.new(
169
+ date_s: Date.today.to_s,
170
+ description: "Dinner with Elizabeth."
171
+ )
172
+
173
+ # Pretend the introvert sets the friends' n_activities values.
174
+ introvert.expect(:set_n_activities!, nil)
175
+ friend1.n_activities = 5
176
+ friend2.n_activities = 7
177
+
178
+ activity.highlight_friends(introvert: introvert, friends: friends)
179
+
180
+ # Pick the friend with more activities.
181
+ activity.description.must_equal "Dinner with **Elizabeth II**."
182
+
183
+ # introvert.verify
184
+ end
172
185
  end
173
186
 
174
187
  describe "#friend_names" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: friends
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jacob Evelyn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-28 00:00:00.000000000 Z
11
+ date: 2015-06-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gli