Class: CommandLineInterface

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

Instance Method Summary collapse

Instance Method Details

#apply_method(method, zodiac) ⇒ Object

used to reduce repeating the code, called twice in zodiac_info method



126
127
128
129
130
131
# File 'lib/cli.rb', line 126

def apply_method(method, zodiac)
    puts "\n#{method_to_string(method)}".magenta.bold + " for ".blue.bold + "#{zodiac.name}".magenta.bold + " is/are:".blue.bold
    method_output = zodiac.send(method)
    puts "\n\n#{output_array_to_string(method_output)}\n\n ".magenta
    what_now(@zodiac)
end

#beginObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/cli.rb', line 4

def begin
    Zodiac.create_new_zodiacs
    puts "Hello! Please type in your birthday (MM/DD):".blue.bold 
    user_input = gets.strip
    zodiac_sign = ''
    if !check_birthday(user_input)
        "Please try again: ".blue.bold
        self.begin
    else
        @zodiac = user_input_to_zodiac(user_input) #set as instance variable so it can be passed to other instance methods
        puts "Your zodiac sign is ".blue.bold + "#{@zodiac.name}".magenta.bold
    end
    zodiac_info(@zodiac)
end

#check_birthday(input) ⇒ Object

an input of MM-DD throws an error on Date object, replaces a hyphen with a backslash, checks the input is valid. Asks the user if their birthday is in the form “February 9”.



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cli.rb', line 20

def check_birthday(input)
    if input.include?('-')
        input = input.gsub('-', '/')
    elsif 
        input.length > 5 || input.length < 3
        return false
    end
    date = Date.parse(input)
    month = Date::MONTHNAMES[date.month] #uses built in class constant to select the name of the month based on the number input
    puts "You entered: ".blue.bold + "#{month} #{date.day}".magenta.bold + ", is that correct? Y/N".blue.bold
    user_input = gets.strip
    user_input =~ (/[Yy](es)?/) ? true : false
end


116
117
118
119
120
121
122
123
# File 'lib/cli.rb', line 116

def menu 
    <<~MENU 
    What would you like to do now? \n
    1. See another zodiac sign
    2. Look at other characteristics
    3. Exit
    MENU
end

#method_to_string(method) ⇒ Object

this takes the method and returns it to tell the user what they are looking at on on line 74 and in the apply_method method.



158
159
160
161
162
163
# File 'lib/cli.rb', line 158

def method_to_string(method)
    method = method.to_s
    array = method.split('_')
    string = array.join(" ")
    string.capitalize!
end

#options1Object

heredoc to normalize the spacing



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/cli.rb', line 91

def options1 
<<~OPTIONS

        What would you like to know about? :

        1. Traits
        2. Physical Traits
        3. Ruling Planet
        4. Compatible signs
        5. Dates
        6. Favorite things
        7. More
    OPTIONS
end

#options2Object



106
107
108
109
110
111
112
113
114
# File 'lib/cli.rb', line 106

def options2 
    <<~OPTIONS2
        8. Symbol
        9. Element
        10. Famous people with this sign
        11. Secret wish
        12. Hates 
    OPTIONS2
end

#output_array_to_string(method_output) ⇒ Object

this takes the return value from the zodiac instance method and if it is an array, changes to capitalize firt word and separate wtih a new line



166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/cli.rb', line 166

def output_array_to_string(method_output)
    array = []
    string = ""
    if method_output.class == Array 
        method_output.each do |word|
            array << word.strip.capitalize 
        end
        string = array.join("\n")
    else 
        string = method_output
    end
    string
end

#user_input_to_zodiac(input) ⇒ Object

takes the user inputted DOB, serches through the dates in all Zodiac instances using the Date object, returns the zodiac instance. Exception made for Capricorn because beginning of year /end of year



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cli.rb', line 35

def user_input_to_zodiac(input)
    zodiac_sign = ''
    dates = []
    Zodiac.all_dates.each do |date_array|
        if Date.parse(input).between?(Date.parse(date_array[0]), Date.parse(date_array[1]))
            dates = date_array
        end
    end
    Zodiac.all.each do |zodiac|
        if zodiac.sun_dates == dates
            @zodiac = zodiac
        elsif Date.parse(input).between?(Date.parse("12/22"), Date.parse("12/31")) || Date.parse(input).between?(Date.parse("01/01"), Date.parse("01/19"))
            @zodiac = Zodiac.all.find{|zodiac| zodiac.name == "Capricorn"}
        end
    end
@zodiac
end

#user_select_7(input, zodiac, array_of_methods) ⇒ Object

method if user selects “More” from options1, so it can loop back around if 7 is selected multiple times and index 7 is never actually selected



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/cli.rb', line 70

def user_select_7(input, zodiac, array_of_methods)
    puts options2.blue.bold
    user_input = gets.strip.to_i
    method = array_of_methods[user_input]
    if user_input.between?(1, 6)
        apply_method(method, zodiac)
    elsif !user_input.between?(8, 12) 
        puts "\nPlease select a valid number:".blue.bold
        zodiac_info(@zodiac)
    elsif user_input == 10  #exception for famous_people method because the capitalization should be kept as it was originally.
        puts "\n #{method_to_string(method)}".magenta.bold + " who also have ".blue.bold + "#{zodiac.name}".magenta.bold  + " sign :".blue.bold
        puts "\n\n #{(zodiac.send(method)).join("\n")} \n\n".magenta
        what_now(@zodiac)
    elsif user_input == 7
        user_select_7(input, array_of_methods)
    else 
        apply_method(method, zodiac)
    end
end

#what_now(zodiac) ⇒ Object

what now gives the “what would you like to do now” menu, gives list of zodiac signs, takes user input and returns what the user requested to see



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/cli.rb', line 134

def what_now(zodiac)
    puts menu.blue.bold
    user_input = gets.strip 
    if user_input == "2"
        zodiac_info(@zodiac)
    elsif user_input == "1"
        puts "Which sign would you like to see?".blue.bold
        Zodiac.all_names.each_with_index {|name, index| puts "#{index + 1}. #{name}".blue.bold}
        user_input_sign = gets.strip 
        user_input_index = user_input_sign.to_i - 1 
        zodiac_sign = Zodiac.all_names[user_input_index]
        @zodiac = Zodiac.all.find{|z| z.name == zodiac_sign}
        system "clear"
        puts "You selected ".blue.bold + "#{@zodiac.name}".magenta.bold + "!".blue.bold
        zodiac_info(@zodiac) 
    elsif user_input == "3"
        exit!
    elsif user_input != "1" || user_input != "2" || user_input != "3"
        puts "Please make a valid selection.".blue.bold
        what_now(@zodiac)
    end
end

#zodiac_info(zodiac) ⇒ Object

gives options of what characteristics of zodiac you would like to see, and displays it, asks for user selection. Selected from array_of methods and the selected method is applied to the zodiac instance.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/cli.rb', line 54

def zodiac_info(zodiac)
    array_of_methods = Zodiac.get_attributes.map{|attribute| attribute.to_s}
    puts options1.blue.bold
    user_input = gets.strip.to_i
    method = array_of_methods[user_input]
    if !user_input.between?(1, 7)
        puts "\n Please select a valid number:".blue.bold
        zodiac_info(@zodiac)
    elsif user_input == 7
        user_select_7(user_input, @zodiac, array_of_methods)
    else 
    apply_method(method, zodiac)
    end
end