blueberry 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 24490f20780296b37c98f69ffa36471bc7b77540
4
+ data.tar.gz: 14dfd7db95081fdf7708c79f35c6a9dbeeed03f2
5
+ SHA512:
6
+ metadata.gz: a250a12f92cc83a891243a25c627f09c1e05455d4a514582fe6ad545663d1d38ff2bfd1329b62c06d8335f11059c9134b2d65ce4a41f02aa31eaee66ed69a8c9
7
+ data.tar.gz: b8076b29169f0bcb283f172068124b8f174f51f4395d333276b93ae12ff6693ea0f7ee51cb6b92ef3ff862a23ecf0fc9b7e07622b80f22c48fb9fd16e7b1c596
data/README ADDED
@@ -0,0 +1 @@
1
+ My blueberry gem ;)
data/bin/blue ADDED
@@ -0,0 +1,47 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/playlist'
4
+ require_relative '../lib/movie3d'
5
+
6
+ playlist = Flicks::Playlist.new("Making knowledge MINE")
7
+
8
+ default_movies_file =
9
+ File.join(File.dirname(__FILE__),'movies.csv')
10
+
11
+ playlist.load_file(ARGV.shift || default_movies_file)
12
+
13
+ #Whenever you type a command that uses a file,
14
+ #the working directory(where you called the script from)
15
+ #will be the first place your shell looks to find the file.
16
+ #therefore, you need to provide absolute path for 'avengers'
17
+
18
+ #and we can call this script anywhere once gem is installed
19
+ #because gem loads bin/executable file on ruby's load path
20
+ #once it is installed
21
+
22
+ movie3d = Flicks::Movie3D.new('95%',5,10)
23
+ playlist.add_movie(movie3d)
24
+
25
+ loop do
26
+ puts "\nHow many viewings? ('quit' to exit)"
27
+ answer = gets.chomp.downcase #get string, gets next line from $stdin
28
+ #chomp removes any characters hanging onto end of string
29
+ case answer #answer is string because its from $stdin, you type in as string
30
+ when 'quit','exit'
31
+ playlist.print_stats
32
+ playlist.save_file
33
+ break
34
+ when /^\d*$/
35
+ puts "Enjoy your #{answer} viewings, achiever!"
36
+ playlist.play(answer.to_i)
37
+ else
38
+ puts "\nPlease enter a number or 'quit' to exit "
39
+ end
40
+ end
41
+
42
+
43
+
44
+
45
+
46
+
47
+
data/bin/movies.csv ADDED
@@ -0,0 +1,7 @@
1
+ charlie,5
2
+ shiny,5
3
+ dany,5
4
+ kevin,5
5
+ ray,5
6
+ yohan,5
7
+ jeremiah,5
@@ -0,0 +1,28 @@
1
+ require_relative 'movie'
2
+
3
+ module Dok2AndQuiet
4
+
5
+ def self.roll
6
+ @number = rand(1..6)
7
+ end
8
+
9
+ def self.review(movie)
10
+
11
+ case roll #or self.roll
12
+
13
+ when 1..2
14
+ movie.thumbs_down
15
+ puts "#{movie.title} got thumbs down"
16
+ when 3..4
17
+ puts "#{movie.title} got skipped"
18
+ when 5..6
19
+ movie.thumbs_up
20
+ puts "#{movie.title} got thumbs up!"
21
+ end
22
+ end
23
+ end
24
+
25
+ if __FILE__ == $0
26
+ movie = Movie.new("ironman")
27
+ puts Dok2AndQuiet::review(movie) #Dok2AndQuiet.review(movie)
28
+ end
data/lib/mixins.rb ADDED
@@ -0,0 +1,58 @@
1
+ module Rankable
2
+ def thumbs_up
3
+ puts "#{self.title} got a thumbs_up"
4
+ end
5
+ end
6
+
7
+ class Movie
8
+ include Rankable
9
+ attr_accessor :title, :rank
10
+ def initialize(title,rank)
11
+ @title = title
12
+ @rank = rank
13
+
14
+ end
15
+ end
16
+
17
+ class Song
18
+ include Rankable
19
+ attr_accessor :title, :rank
20
+
21
+ def initialize(title,rank)
22
+ self.title = title
23
+ self.rank = rank
24
+ end
25
+ end
26
+
27
+ if __FILE__ == $0
28
+ movie = Movie.new('goonies',10)
29
+ movie.thumbs_up
30
+ song = Song.new('Ruby baby',10)
31
+ song.thumbs_up
32
+ end
33
+
34
+ #mixins - simply a module that has methods
35
+ # and those methods gets mixed into other classes
36
+
37
+ #we want to share some common behaviors across
38
+ #these classes which arent in the same inheritance hierchy
39
+ #(they are not in same ancestors line, no child and parent)
40
+
41
+ #mixed in modules effectively behave like superclasses.
42
+ #it changes the method look-up hierchy
43
+ #module lives in between the class that includes the
44
+ #module and their direct superclass
45
+ #so when we call a method, it looks into the object's
46
+ #class then it looks in the module, then it looks in
47
+ #the superclass and then all the way into the top
48
+
49
+ # It generally considered better practice if
50
+ # a module depends on a method or an attribute in the
51
+ # hosting class(the class that it gets mixed into)
52
+ # as opposted to relying on the instance variable being present
53
+
54
+
55
+
56
+
57
+
58
+
data/lib/movie.rb ADDED
@@ -0,0 +1,59 @@
1
+ require_relative 'rankable'
2
+
3
+ module Flicks
4
+ class Movie
5
+ include Rankable
6
+
7
+ attr_reader :snack_carbs,:title
8
+ attr_accessor :rank
9
+
10
+ def initialize(title, rank=0)
11
+ @title = title.capitalize
12
+ @rank = rank
13
+ @snack_carbs = Hash.new(0)
14
+ end
15
+
16
+ def self.convert(line)
17
+ title,rank = line.split(',')
18
+ Movie.new(title,Integer(rank))
19
+ end
20
+
21
+ def expose_hash
22
+ @snack_carbs.each do |key,value|
23
+ snack = Snack.new(key,value.to_i)
24
+ yield(snack)
25
+ end
26
+ end
27
+
28
+ def carbs_consumed
29
+ @snack_carbs.values.reduce(0) {|sum,snack| sum + snack }
30
+ # @snacks_carbs.values.reduce(0,:+)
31
+ end
32
+
33
+ def ate_snack(snack)
34
+ @snack_carbs[snack.name] += snack.carbs
35
+ puts "Sold ~> #{snack.name.capitalize} - #{snack.carbs} carbs"
36
+ puts @snack_carbs
37
+ end
38
+
39
+ def title=(title)
40
+ @title = title.capitalize
41
+ end
42
+
43
+ def to_s
44
+ "#{@title} with a rank of #{@rank} (#{status})"
45
+ end
46
+
47
+ end
48
+ end
49
+
50
+ if __FILE__ == $0
51
+ movie1 = Flicks::Movie.new("Amazing Spiderman",8)
52
+ puts movie1
53
+ puts movie1.normalized_rank
54
+ movie1.thumbs_up
55
+ movie1.thumbs_down
56
+ puts movie1.rank
57
+ movie1.title="crappy spiderman"
58
+ puts movie1.title
59
+ end
data/lib/movie3d.rb ADDED
@@ -0,0 +1,138 @@
1
+ require_relative 'movie'
2
+
3
+ module Flicks
4
+ class Movie3D < Movie
5
+ attr_accessor :wow_factor
6
+
7
+ def initialize(title,rank,wow_factor)
8
+ super(title,rank)
9
+
10
+ @wow_factor = wow_factor
11
+ end
12
+
13
+ def thumbs_up
14
+ self.wow_factor.times { super }
15
+ end
16
+
17
+ def show_effect
18
+ puts "Wow " * self.wow_factor
19
+ end
20
+ end
21
+ end
22
+
23
+ if __FILE__ == $0
24
+ movie3d = Flicks::Movie3D.new('90%',5,10)
25
+ end
26
+
27
+ # polymorphism - 3dmovie and movie is a kind of relationship
28
+ # we can use 3dmovie on anywhere we expect to use movie this is polymorphism where
29
+ # we can use object A as like, object b, because OBJECT A is kind of
30
+ # object B - inherited
31
+
32
+
33
+ # movie.show_effect
34
+ #above method that is defined in child class doesnt work on super class movie objects because
35
+ #class Movie doesnt have this method. Ruby can only search methods UP on
36
+ #inhertiance hicharchy. only upwards, superclass and ancestors, never travel down
37
+ #on inheritance. show_effect is a new unique method defined in child class.
38
+
39
+ #quickly show you how you can use
40
+ #reflection(act of reflecting casting back)
41
+ #to look at the class Hierarchy
42
+ #(system of things ranked one above another)
43
+ #Class.superclass || Class.ancestors
44
+
45
+ # *take away*
46
+ # when you call a method on an object, ruby first tries
47
+ # to find that method in the objects class. If that method
48
+ # isnt found,ruby looks in the objects super class or module defined inside
49
+ #of superclass
50
+ # and its super class and so on all the way up to the
51
+ # top of the chain
52
+
53
+ #subclasses inherit behavior(method) and
54
+ #attributes(instance variables) from parents class.
55
+ #subclasses can also define their own unique behavior
56
+ #that wont be available in their parent class.
57
+
58
+
59
+ #subclasses can specialize(modify) behaviors(methods) by overwriting
60
+ #methods that are already defined in the parent class.
61
+ #and by using Super, they can modify existing method.
62
+
63
+ #subclasses have access to parent class's @instance variables
64
+
65
+ #we can call parent class methods on child class
66
+ #by using something called super
67
+
68
+ # class Animal
69
+ # attr_accessor :name
70
+
71
+ # def initialize(name) #receives name attribute from super and invokes initialize method
72
+ # @name = name
73
+ # end
74
+ # end
75
+
76
+ # class BadDog < Animal
77
+ # def initialize(age, name)
78
+ # super(name) #super invokes the whole initialize method in parent class and sends name attribute
79
+ # @age = age
80
+ # end
81
+ # end
82
+ # BadDog.new(2, "bear") => #<BadDog:0x007fb40b2beb68 @age=2, @name="bear">
83
+
84
+
85
+ # class Animal
86
+ # attr_accessor :name, :speed
87
+ # def initialize(name,speed)
88
+ # self.name = name
89
+ # self.speed = speed
90
+ # end
91
+
92
+ # end
93
+
94
+
95
+ # module Runnable
96
+ # attr_accessor :name,:speed
97
+
98
+ # def initialize(name,speed)
99
+ # self.name = name
100
+ # self.speed = speed
101
+ # end
102
+ # end
103
+
104
+ # class Cheetah
105
+ # include Runnable
106
+ # attr_accessor :power
107
+ # def initialize(name,speed,power)
108
+ # super(name,speed)
109
+
110
+ # @power = power
111
+ # end
112
+
113
+ # def stats
114
+ # puts self.name
115
+ # puts self.speed
116
+ # end
117
+
118
+ # end
119
+
120
+ # cheetah = Cheetah.new('cheetah',50,70)
121
+ # puts cheetah.name = "howdy"
122
+ # puts cheetah.stats
123
+
124
+ # animal = Animal.new('rocky',50)
125
+ # puts animal.inspect
126
+ # puts animal.name
127
+ # cheetah = Cheetah.new('cheetah',50,9000)
128
+ # puts cheetah.name, cheetah.speed,cheetah.power
129
+
130
+ # When called with specific arguments, eg. super(a, b), the specified arguments will be sent up the method lookup chain. Let's see a quick example:
131
+ # #super invokes same method(this time initialize method) its called on, so it calls on initialize of class Animal.
132
+
133
+ # This is similar to our previous example, with the difference being that super takes an argument, hence(this time on) the passed in argument is sent to the superclass. Consequently(as a result,outcome,therefore),
134
+ #in this example when a BadDog class is created the passed in name argument ("bear") is passed to the superclass and set to the @name instance variable.
135
+
136
+
137
+ # When you call super from within a method, it will search the inheritance hierarchy for a method(that called super)by the same name and then invoke it.
138
+
data/lib/namespaces.rb ADDED
@@ -0,0 +1,30 @@
1
+ module MovieSystem
2
+ VERSION = 1.0
3
+
4
+ def self.info
5
+ puts "Movie system version #{VERSION}"
6
+ end
7
+
8
+ class Player
9
+ end
10
+ end
11
+
12
+
13
+ module GameSystem
14
+ VERSION = 2.0
15
+
16
+ def self.info
17
+ puts "Game system version #{VERSION}"
18
+ end
19
+
20
+ class Player
21
+ end
22
+ end
23
+
24
+ puts MovieSystem::VERSION
25
+ MovieSystem.info
26
+ puts MovieSystem::Player.new
27
+
28
+ puts GameSystem::VERSION
29
+ GameSystem.info
30
+ puts GameSystem::Player.new
data/lib/playlist.rb ADDED
@@ -0,0 +1,111 @@
1
+ require_relative 'dok2_and_quiet'
2
+ require_relative 'snack_bar'
3
+ require_relative 'movie'
4
+
5
+ module Flicks
6
+ class Playlist
7
+ attr_reader :movies, :name
8
+
9
+ def initialize(name)
10
+ @name = name.capitalize
11
+ @movies = []
12
+ puts "\n***#{@name}'s Playlist has been created***"
13
+ end
14
+
15
+ def name=(title)
16
+ @name = title.capitalize
17
+ puts "Name of playlist has been modified to #{@name}"
18
+ end
19
+
20
+ def load_file(from_file)
21
+ puts "\n::::Loding File #{from_file.capitalize}::::"
22
+ File.readlines(from_file).each do |line|
23
+ add_movie(Movie.convert(line))
24
+ end
25
+ end
26
+
27
+ def save_file(to_file="final_records")
28
+ puts "\n::::::Saving To File #{to_file.capitalize}::::::"
29
+ File.open(to_file,"a+") do |file|
30
+ @movies.sort.map.with_index(1) do |movie, i|
31
+ file.puts("\t#{i}.#{movie.title},#{movie.rank}")
32
+ end
33
+ time = Time.new
34
+ file.puts time.strftime("Printed on %m/%d/%Y at %I:%M%p")
35
+ end
36
+ end
37
+
38
+ def add_movie(movie)
39
+ @movies << movie
40
+ puts "::added #{movie.title}::"
41
+ end
42
+
43
+ def play(viewings)
44
+
45
+ puts "\n\n~~Playing #{@name}'s playlist:~~"
46
+
47
+ @movies.sort {|x,y| y.rank <=> x.rank}.each do |movie|
48
+ puts "\tTrack: #{movie}"
49
+ end
50
+
51
+ snacks = SnackBar::SNACKS
52
+ puts "\nThere are #{snacks.size} snacks available in the Snack Bar."
53
+ snacks.sort {|x,y| y.carbs <=> x.carbs }.map.with_index(1) {|snack,i| puts "\t#{i}. #{snack.name.capitalize} - #{snack.carbs} carbs"}
54
+
55
+ puts "\nStarting the Movie,Enjoy your show!!"
56
+ 1.upto(viewings) do |counter|
57
+ puts "\nViewing: #{counter}"
58
+ @movies.sort.each do |movie|
59
+ Dok2AndQuiet.review(movie)
60
+ snack = SnackBar.random
61
+ movie.ate_snack(snack)
62
+ puts movie
63
+ puts "**************************************************"
64
+ end
65
+ end
66
+ end
67
+
68
+ def total_playlist_carbs
69
+ @movies.reduce(0) {|sum,movie| sum + movie.carbs_consumed}
70
+ end
71
+
72
+ def total_playlist_snacks
73
+ @movies.reduce(0) {|sum, movie| sum + movie.snack_carbs.keys.size}
74
+ end
75
+
76
+
77
+ def print_stats
78
+ puts "\n\n\nPlayList #{@name.upcase!}'s Stats: #{total_playlist_carbs} carbs - #{total_playlist_snacks} snacks"
79
+
80
+ @movies.sort {|x,y| y.carbs_consumed <=> x.carbs_consumed }.map.with_index(1) do |movie,i|
81
+ puts "#{i}. #{movie.title} - #{movie.carbs_consumed} *total* grand carbs"
82
+ movie.expose_hash {|key| puts "\t #{key.name} - #{key.carbs} carbs"}
83
+ end
84
+
85
+ hits, flops = @movies.partition {|m| m.hit? }
86
+
87
+ puts "\nHits: "
88
+ hits.sort.each do |movie|
89
+ puts "\t#{movie}"
90
+ end
91
+
92
+
93
+ puts "\nFlops: "
94
+ flops.sort.each do |movie|
95
+ puts "\t#{movie}"
96
+ end
97
+ end
98
+ end
99
+ end
100
+
101
+ if __FILE__ == $0
102
+ playlist1 = Flicks::Playlist.new("Mastering Ruby 1")
103
+ movie1 = Flicks::Movie.new("Knowledge and Skill",10)
104
+ movie2 = Flicks::Movie.new("Focus",20)
105
+ playlist1.add_movie(movie1)
106
+ playlist1.add_movie(movie2)
107
+ # puts playlist1.movies
108
+ # puts playlist1.name
109
+ playlist1.play(3)
110
+ end
111
+
data/lib/rankable.rb ADDED
@@ -0,0 +1,26 @@
1
+ module Rankable
2
+
3
+ def thumbs_up
4
+ self.rank += 1
5
+ end
6
+
7
+ def thumbs_down
8
+ self.rank -= 1
9
+ end
10
+
11
+ def hit?
12
+ self.rank >= 10
13
+ end
14
+
15
+ def status
16
+ hit? ? "Hit" : "Flop"
17
+ end
18
+
19
+ def normalized_rank
20
+ self.rank / 2
21
+ end
22
+
23
+ def <=>(other)
24
+ other.rank <=> @rank
25
+ end
26
+ end
data/lib/snack_bar.rb ADDED
@@ -0,0 +1,27 @@
1
+ Snack = Struct.new(:name,:carbs)
2
+
3
+ module SnackBar
4
+ #constant - identifier with value that cannot be altered(changed)
5
+ SNACKS = [
6
+ Snack.new(:popcorn,20),
7
+ Snack.new(:candy,15),
8
+ Snack.new(:nachos,40),#class Snack
9
+ Snack.new(:pretzel,10),
10
+ Snack.new(:pop,5) #instance object created by class Snack
11
+ ]
12
+
13
+ def self.random
14
+ SNACKS.sample
15
+ end
16
+
17
+ end
18
+
19
+ if __FILE__ == $0
20
+ snacks = SnackBar::SNACKS
21
+ puts snacks
22
+ snack = SnackBar.random
23
+ puts "Enjoy your random #{snack.name} with #{snack.carbs} carbs"
24
+ end
25
+
26
+ #one snackbar in our program, so it is fit
27
+ #to make module instead of class
@@ -0,0 +1,81 @@
1
+ require 'movie'
2
+ require_relative 'spec_helper'
3
+
4
+ module Flicks
5
+ RSpec.describe Movie do
6
+ before do
7
+ $stdout = StringIO.new
8
+ end
9
+
10
+ context "Let's unit test Movie methods" do
11
+ before do
12
+ @initial_rank = 10
13
+ @movie = Movie.new('goonies',@initial_rank)
14
+ end
15
+
16
+ it "has a capitalized title" do
17
+ @movie.title.should_not == "goonies"
18
+ @movie.title.should == "Goonies"
19
+ end
20
+
21
+ it "has an initial rank" do
22
+ @movie.rank.should == 10
23
+ end
24
+
25
+ it "has a string representation" do
26
+ @movie.to_s.should == "Goonies with a rank of 10 (Hit)"
27
+ end
28
+
29
+ it "increases rank by 1 when given a thumbs up" do
30
+ @movie.thumbs_up
31
+ @movie.rank.should == @initial_rank + 1
32
+ end
33
+
34
+ it "decreases rank by 1 when given a thumbs down" do
35
+ @movie.thumbs_down
36
+ @movie.rank.should == @initial_rank - 1
37
+ end
38
+ end
39
+
40
+ context "can have default values" do
41
+ before do
42
+ @movie = Movie.new('goonies')
43
+ end
44
+
45
+ it "has a default rank of 0 if not initialized" do
46
+ @movie.rank.should == 0
47
+ end
48
+ end
49
+
50
+ context "with a rank of at least 10." do
51
+ before do
52
+ @movie = Movie.new('lawyer',10)
53
+ end
54
+
55
+ it "is a Hit" do
56
+ expect(@movie).to be_hit
57
+ end
58
+
59
+ it "has a HIT status" do
60
+ expect(@movie.status).to eq("Hit")
61
+ end
62
+
63
+
64
+ end
65
+
66
+ context 'with a rank less than 10.' do
67
+ before do
68
+ @movie = Movie.new('fantastic 4',5)
69
+ end
70
+
71
+ it "is not a hit" do
72
+ expect(@movie).to_not be_hit
73
+ end
74
+
75
+ it 'has a Flop status' do
76
+ expect(@movie.status).to eq('Flop')
77
+ end
78
+ end
79
+ end
80
+ end
81
+
@@ -0,0 +1,37 @@
1
+ require 'playlist'
2
+
3
+ module Flicks
4
+ RSpec.describe Playlist do
5
+ before do
6
+ $stdout = StringIO.new
7
+ @playlist = Playlist.new('show me the money')
8
+ end
9
+
10
+ context '*being played with one movie.' do
11
+ before do
12
+ @initial_rank = 10
13
+ @movie = Movie.new('L4L',@initial_rank)
14
+ @playlist.add_movie(@movie)
15
+ end
16
+
17
+ it 'gives the movie a thumbs up when high number is rolled' do
18
+ allow(Dok2AndQuiet).to receive(:roll).and_return(5)
19
+ @playlist.play(1)
20
+
21
+ expect(@movie.rank).to eq(@initial_rank + 1)
22
+ end
23
+
24
+ it 'skips the movie when medium number is rolled' do
25
+ allow(Dok2AndQuiet).to receive(:roll).and_return(3)
26
+ @playlist.play(1)
27
+ expect(@movie.rank).to eq(@initial_rank)
28
+ end
29
+
30
+ it 'gives the movie a thumbs down if lower number is rolled' do
31
+ allow(Dok2AndQuiet).to receive(:roll).and_return(1)
32
+ @playlist.play(1)
33
+ expect(@movie.rank).to eq(@initial_rank - 1)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,8 @@
1
+ RSpec.configure do |config|
2
+ config.expect_with :rspec do |c|
3
+ c.syntax = [:should, :expect] # default, enables both `should` and `expect`
4
+ end
5
+ config.mock_with :rspec do |c|
6
+ c.syntax = [:should, :expect] # default, enables both `should` and `expect`
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: blueberry
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Dany Han
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-17 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ executables:
16
+ - blue
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - README
21
+ - bin/blue
22
+ - bin/movies.csv
23
+ - lib/dok2_and_quiet.rb
24
+ - lib/mixins.rb
25
+ - lib/movie.rb
26
+ - lib/movie3d.rb
27
+ - lib/namespaces.rb
28
+ - lib/playlist.rb
29
+ - lib/rankable.rb
30
+ - lib/snack_bar.rb
31
+ - spec/movie_spec.rb
32
+ - spec/playlist_spec.rb
33
+ - spec/spec_helper.rb
34
+ homepage:
35
+ licenses: []
36
+ metadata: {}
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project:
53
+ rubygems_version: 2.2.2
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: My blueberry gem ;)
57
+ test_files: []