Skip to content

Commit

Permalink
Merge pull request #243 from shen-sat/152/add_set_home_command_to_set…
Browse files Browse the repository at this point in the history
…_your_own_location

152/add set home command to set your own location

Fixes #152
  • Loading branch information
JacobEvelyn authored Dec 11, 2019
2 parents 3e63a6c + 09b80ff commit 631095f
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 5 deletions.
24 changes: 21 additions & 3 deletions lib/friends/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def initialize(str: "")

attr_reader :date
attr_accessor :description
attr_writer :implicit_location

# @return [String] the command-line display text for the activity
def to_s
Expand Down Expand Up @@ -132,9 +133,13 @@ def update_location_name(old_name:, new_name:)
end

# @param location [Location] the location to test
# @return [Boolean] true iff this activity includes the given location
# @return [Boolean] true if activity has location in description or it equals implicit location
def includes_location?(location)
@description.scan(/(?<=_)[^_]+(?=_)/).include? location.name
location_in_description?(location) || location_is_implicit?(location)
end

def moved_to_location
@description[/(?<=[mM]oved to _)\w[^_]*(?=_)/]
end

# @param friend [Friend] the friend to test
Expand Down Expand Up @@ -162,10 +167,15 @@ def friend_names

# Find the names of all locations in this description.
# @return [Array] list of all location names in the description
def location_names
def description_location_names
@description.scan(/(?<=_)\w[^_]*(?=_)/).uniq
end

# @return [Array] list of all location names in either description or implicit_location
def location_names
@implicit_location ? [@implicit_location] : description_location_names
end

private

# Modify the description to turn inputted location names (e.g. "Atlantis")
Expand Down Expand Up @@ -330,5 +340,13 @@ def description_matches(regex:, replace:, indicator:)
def <=>(other)
other.date <=> date
end

def location_in_description?(location)
description_location_names.include? location.name
end

def location_is_implicit?(location)
@implicit_location == location.name
end
end
end
11 changes: 10 additions & 1 deletion lib/friends/introvert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def clean(clean_command:)
end
end

event.location_names.each do |name|
event.description_location_names.each do |name|
unless location_names.include? name
add_location(name: name)
location_names << name
Expand Down Expand Up @@ -658,6 +658,14 @@ def set_n_activities!(type)
end
end

def set_implicit_locations!
implicit_location = nil
@activities.reverse_each do |activity|
implicit_location = activity.moved_to_location if activity.moved_to_location
activity.implicit_location = implicit_location if activity.description_location_names.empty?
end
end

# Process the friends.md file and store its contents in internal data
# structures.
def read_file
Expand All @@ -677,6 +685,7 @@ def read_file
# Parse the line and update the parsing state.
state = parse_line!(line, line_num: line_num, state: state)
end
set_implicit_locations!

set_n_activities!(:friend)
set_n_activities!(:location)
Expand Down
4 changes: 4 additions & 0 deletions test/commands/clean_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<<-CONTENT
### Activities:
- 2017-01-01: Celebrated the new year in _Paris_ with **Marie Curie** and her husband **Pierre Curie**. **Marie Curie** loves _Paris_!
- 2016-12-31: Moved to _London_.
### Notes:
- 2017-01-01: I just learned that **Jacques Cousteau** is thinking about moving from _Gironde_ to _The Lost City of Atlantis_ (_Gironde_ did seem a bit too terrestrial for him).
Expand All @@ -90,6 +91,7 @@
file_equals <<-CONTENT
### Activities:
- 2017-01-01: Celebrated the new year in _Paris_ with **Marie Curie** and her husband **Pierre Curie**. **Marie Curie** loves _Paris_!
- 2016-12-31: Moved to _London_.
### Notes:
- 2017-01-01: I just learned that **Jacques Cousteau** is thinking about moving from _Gironde_ to _The Lost City of Atlantis_ (_Gironde_ did seem a bit too terrestrial for him).
Expand All @@ -102,6 +104,7 @@
### Locations:
- Gironde
- London
- NYC
- Paris
- The Lost City of Atlantis
Expand All @@ -113,6 +116,7 @@
Friend added: \"Marie Curie\"
Friend added: \"Pierre Curie\"
Location added: \"Paris\"
Location added: \"London\"
Friend added: \"Jacques Cousteau\"
Location added: \"Gironde\"
Location added: \"The Lost City of Atlantis\"
Expand Down
32 changes: 31 additions & 1 deletion test/commands/list/activities_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,38 @@
stdout_only "2014-12-31: Celebrated the new year in Paris with Marie Curie. @partying"
end
end
end

describe "when implicit location is set" do
let(:location_name) { "atlantis" }
let(:content) do
<<-FILE
### Activities:
- 2015-01-30: Went to a museum with **George Washington Carver**.
- 2015-01-29: moved to _Paris_.
- 2015-01-01: Got lunch with **Grace Hopper** and **George Washington Carver**. @food
- 2014-12-31: Celebrated the new year in _Paris_ with **Marie Curie**. @partying @food
- 2014-12-30: Moved to _Atlantis_.
- 2014-12-29: Talked to **George Washington Carver** on the phone for an hour.
### Friends:
- George Washington Carver
- Marie Curie [Atlantis] @science
- Grace Hopper (a.k.a. The Admiral a.k.a. Amazing Grace) [Paris] @navy @science
### Locations:
- Atlantis
- Paris
FILE
end

it "matches location case-insensitively" do
stdout_only <<-OUTPUT
2015-01-01: Got lunch with Grace Hopper and George Washington Carver. @food
2014-12-30: Moved to Atlantis.
OUTPUT
end
end
end
describe "--with" do
subject { run_cmd("list activities --with #{friend_name}") }

Expand Down
31 changes: 31 additions & 0 deletions test/commands/list/favorite/locations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,36 @@
value(lines.last).must_include "3. Location"
end
end

describe "when implied locations are set" do
let(:content) do
<<-FILE
### Activities:
- 2015-01-30: Went to a museum with **George Washington Carver**.
- 2015-01-29: moved to _Paris_.
- 2015-01-01: Got lunch with **Grace Hopper** and **George Washington Carver**. @food
- 2014-12-31: Celebrated the new year in _Paris_ with **Marie Curie**. @partying @food
- 2014-12-30: Moved to _Atlantis_.
- 2014-12-29: Talked to **George Washington Carver** on the phone for an hour.
### Friends:
- George Washington Carver
- Marie Curie [Atlantis] @science
- Grace Hopper (a.k.a. The Admiral a.k.a. Amazing Grace) [Paris] @navy @science
### Locations:
- Atlantis
- Paris
FILE
end

it "lists locations in order of decreasing activity" do
stdout_only <<-OUTPUT
Your favorite locations:
1. Paris (3 activities)
2. Atlantis (2)
OUTPUT
end
end
end
end

0 comments on commit 631095f

Please sign in to comment.