Browse files

Merge branch 'master' of github.com:funkylarma/RubyDevelopment

  • Loading branch information...
2 parents 2077825 + ef481f8 commit a07ef1256a91d49a98b8d4515cf7dee3bab01a8d @funkylarma committed May 9, 2012
View
1 .rvmrc
@@ -0,0 +1 @@
+rvm use --create 1.9.3@tutorial
View
5 Beginning Ruby/Chapter7/string_extensions.rb
@@ -0,0 +1,5 @@
+class String
+ def vowels
+ self.scan(/[aeiou]/i)
+ end
+end
View
3 Beginning Ruby/Chapter7/vowel_test.rb
@@ -0,0 +1,3 @@
+require './string_extensions'
+
+puts "This is a test".vowels.join('-')
View
14 Lynda.com/Ruby Essential Training/food_finder/init.rb
@@ -0,0 +1,14 @@
+#### Food Finder ####
+#
+# Launch this Ruby file from the command line
+# to get started
+#
+
+APP_ROOT = File.dirname(__FILE__)
+
+$:.unshift(File.join(APP_ROOT, 'lib'))
+
+require 'guide'
+
+guide = Guide.new('resturants.txt')
+guide.launch!
View
154 Lynda.com/Ruby Essential Training/food_finder/lib/guide.rb
@@ -0,0 +1,154 @@
+# Guide class
+
+require 'restaurant'
+require 'support/string_extend'
+
+class Guide
+
+ class Config
+ @@actions = ['list', 'find', 'add', 'quit']
+ def self.actions
+ @@actions
+ end
+ end
+
+ def initialize(path=nil)
+ # Locate the resturant text file
+ Restaurant.filepath = path
+ # or create a new file
+ if Restaurant.file_usable?
+ puts "Found resturant file."
+ # exit if create fails
+ elsif Restaurant.create_file
+ puts "Created restaurant file."
+ else
+ puts "Exiting.\n\n"
+ exit!
+ end
+ end
+
+ def launch!
+ # Load the intro
+ introduction
+ # Start the action loop
+ result = nil
+ until result == :quit
+ action, args = get_action
+ # Do that action
+ result = do_action(action, args)
+ end
+ # Load the conclusion
+ conclusion
+ end
+
+ def get_action
+ action = nil
+ until Guide::Config.actions.include?(action)
+ puts "Actions: " + Guide::Config.actions.join(", ") if action
+ print "> "
+ user_response = gets.chomp
+ args = user_response.downcase.strip.split(' ')
+ action = args.shift
+ end
+ return action, args
+ end
+
+ def do_action(action, args=[])
+ case action
+ when 'list'
+ list(args)
+ when 'find'
+ keyword = args.shift
+ find(keyword)
+ when 'add'
+ add
+ when 'quit'
+ return :quit
+ else
+ puts "\nI don't understand that command.\n"
+ end
+ end
+
+ def list(args=[])
+ sort_order = args.shift
+ sort_order = args.shift if sort_order == 'by'
+ sort_order = "name" unless['name', 'cuisine', 'price'].include?(sort_order)
+
+ output_action_header("Listing restaurants")
+
+ restaurants = Restaurant.saved_restaurants
+ restaurants.sort! do |r1, r2|
+ case sort_order
+ when 'name'
+ r1.name.downcase <=> r2.name.downcase
+ when 'cuisine'
+ r2.cuisine.downcase <=> r2.cuisine.downcase
+ when 'price'
+ r1.price.to_i <=> r2.price.to_i
+ end
+ end
+
+ output_restaurant_table(restaurants)
+ puts "Sort using: 'list cuisine' or 'list by name'\n\n"
+ end
+
+ def add
+ output_action_header("Add a restaurant")
+
+ restaurant = Restaurant.build_using_questions
+
+ if restaurant.save
+ puts "\nResturant added\n\n"
+ else
+ puts "\nSave Error: Restaurant not added\n\n"
+ end
+ end
+
+ def find(keyword="")
+ output_action_header("Find a restarurant")
+ if keyword
+ restaurants = Restaurant.saved_restaurants
+ found = restaurants.select do |rest|
+ rest.name.downcase.include?(keyword.downcase) ||
+ rest.cuisine.downcase.include?(keyword.downcase) ||
+ rest.price.to_i <= keyword.to_i
+ end
+ output_restaurant_table(found)
+ else
+ puts "Find using a key phrase to search the restaurant list."
+ puts "Examples: 'find tamale', 'find Mexican', 'find mex'\n\n"
+ end
+ end
+
+
+ def introduction
+ puts "\n\n<<< Welcome to the Food Finder >>> \n\n"
+ puts "This is an interactive guide to help you find the food you crave.\n\n"
+ end
+
+ def conclusion
+ puts "\n<<< Goodbye and Bon Appetit! >>> \n\n\n"
+ end
+
+ private
+
+ def output_action_header(text)
+ puts "\n#{text.upcase.center(60)}\n\n"
+ end
+
+ def output_restaurant_table(restaurants=[])
+ print " " + "Name".ljust(30)
+ print " " + "Cuisine".ljust(20)
+ print " " + "Price".rjust(6) + "\n"
+ puts "-" * 60
+ restaurants.each do |rest|
+ line = " " << rest.name.titleize.ljust(30)
+ line << " " + rest.cuisine.titleize.ljust(20)
+ line << " " + rest.formatted_price.rjust(6)
+ puts line
+ end
+ puts "No listings found" if restaurants.empty?
+ puts "-" * 60
+ end
+
+end
View
90 Lynda.com/Ruby Essential Training/food_finder/lib/restaurant.rb
@@ -0,0 +1,90 @@
+require 'support/number_helper'
+
+# Resturant class
+
+class Restaurant
+
+ include NumberHelper
+
+ @@filepath = nil
+ attr_accessor :name, :cuisine, :price
+
+ def self.filepath=(path=nil)
+ @@filepath = File.join(APP_ROOT, path)
+ end
+
+ def self.file_exists?
+ # Class should know if the resturant file exists
+ if @@filepath && File.exists?(@@filepath)
+ return true
+ else
+ return false
+ end
+ end
+
+ def self.file_usable?
+ return false unless @@filepath
+ return false unless File.exists?(@@filepath)
+ return false unless File.readable?(@@filepath)
+ return false unless File.writable?(@@filepath)
+ return true
+ end
+
+ def self.create_file
+ # Create the restaurant file
+ File.open(@@filepath, 'w') unless file_exists?
+ return file_usable?
+ end
+
+ def self.saved_restaurants
+ # Read the resturant file
+ restaurants = []
+ if file_usable?
+ file = File.new(@@filepath, 'r')
+ file.each_line do |line|
+ restaurants << Restaurant.new.import_line(line.chomp)
+ end
+ file.close
+ end
+ return restaurants
+ end
+
+ def self.build_using_questions
+ args = {}
+ print "Restaurant name: "
+ args[:name] = gets.chomp.strip
+
+ print "Cuisine type: "
+ args[:cuisine] = gets.chomp.strip
+
+ print "Average price: "
+ args[:price] = gets.chomp.strip
+
+ return self.new(args)
+ end
+
+ def initialize(args={})
+ @name = args[:name] || ""
+ @cuisine = args[:cuisine] || ""
+ @price = args[:price] || ""
+ end
+
+ def import_line(line)
+ line_array = line.split("\t")
+ @name, @cuisine, @price = line_array
+ return self
+ end
+
+ def save
+ return false unless Restaurant.file_usable?
+ File.open(@@filepath, 'a') do |file|
+ file.puts "#{[@name, @cuisine, @price].join("\t")}\n"
+ end
+ return true
+ end
+
+ def formatted_price
+ number_to_currency(@price)
+ end
+
+end
View
37 Lynda.com/Ruby Essential Training/food_finder/lib/support/number_helper.rb
@@ -0,0 +1,37 @@
+# encoding: utf-8
+# This module illustrates how additional functionality can be
+# included (or "mixed-in") to a class and then reused.
+# Borrows heavily from Ruby on Rails number_to_currency method.
+
+module NumberHelper
+
+ def number_to_currency(number, options={})
+ unit = options[:unit] || '£'
+ precision = options[:precision] || 2
+ delimiter = options[:delimiter] || ','
+ seperator = options[:seperator] || '.'
+
+ seperator = '' if precision == 0
+ integer, decimal = number.to_s.split('.')
+
+ i = integer.length
+ until i <= 3
+ i -= 3
+ integer = integer.insert(i, delimiter)
+ end
+
+ if precision == 0
+ precise_decimal = ''
+ else
+ # make sure decimal is not nil
+ decimal ||= "0"
+ # make sure the decimal is not too large
+ decimal = decimal[0, precision-1]
+ # make sure te decimal is not too short
+ precise_decimal = decimal.ljust(precision, "0")
+ end
+
+ return unit + integer + seperator + precise_decimal
+ end
+
+end
View
10 Lynda.com/Ruby Essential Training/food_finder/lib/support/string_extend.rb
@@ -0,0 +1,10 @@
+# This helper is opening yp core Ruby String class
+# in order to add a new method to all strings
+
+class String
+
+ def titleize
+ self.split(' ').collect { |word| word.capitalize}.join(" ")
+ end
+
+end
View
3 Lynda.com/Ruby Essential Training/food_finder/resturants.txt
@@ -0,0 +1,3 @@
+Test Junk 5
+Test2 Junk 10
+pita pocket fast 20
View
3 README.markdown
@@ -5,4 +5,5 @@ This is a collection of exercises and sample code from various Ruby related stud
* [Begining Ruby - From Novice to Professional](http://www.apress.com/9781590597668)
###Sites
-* [Team Tree House - Ruby Foundations](http://teamtreehouse.com/library/ruby-foundations/)
+* [Team Tree House - Ruby Foundations](http://teamtreehouse.com/library/ruby-foundations/)
+* [Lynda.com - Ruby Essential Training](http://www.lynda.com/Ruby-tutorials/essential-training/47905-2.html)

0 comments on commit a07ef12

Please sign in to comment.