Last active
October 28, 2015 15:15
-
-
Save rob0t7/63ac79595d143a315fa8 to your computer and use it in GitHub Desktop.
OOP Review, RSpec Review and Modules
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative 'jsonable' | |
require 'pry-byebug' | |
class Party | |
def self.throw_party(number_of_candles) | |
'We are partying' | |
true | |
end | |
end | |
class Cat | |
include JSONable | |
attr_reader :name, :age, :happy | |
def initialize(*args) | |
@name = args[0] | |
@age = age[1] | |
end | |
def speak | |
'meow' | |
end | |
def celebrate_birthday | |
@age += 1 | |
@happy = Party.throw_party(@age) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative "../lib/cat" | |
RSpec.describe Cat do | |
let(:name) { 'Bob' } | |
let(:age) { 3 } | |
subject { Cat.new(name, age) } | |
# This tests a query method | |
describe '#name' do | |
it 'returns the name of the cat' do | |
expect(subject.name).to eq name | |
end | |
end | |
# This tests a command/query function | |
describe '#speak' do | |
it 'speaks like a cat' do | |
expect(subject.speak).to eq 'meow' | |
end | |
end | |
# This tests a query method | |
describe '#age' do | |
it 'returns the age of the cat' do | |
expect(subject.age).to eq age | |
end | |
end | |
# This tests a command function | |
describe '#celebrate_birthday' do | |
let(:throw_party_result) { true } | |
before do | |
allow(Party).to receive(:throw_party) | |
.and_return(throw_party_result) | |
subject.celebrate_birthday | |
end | |
it 'adds a year to the age of the cat' do | |
expect(subject.age).to eq age + 1 | |
end | |
# this test an external object using a Mock | |
it 'throws a party' do | |
expect(Party).to have_received(:throw_party) | |
.with(age + 1) | |
end | |
it 'the cat is happy' do | |
expect(subject.happy).to be_truthy | |
end | |
# Another mock test | |
context 'when the party go wrong' do | |
let(:throw_party_result) { false } | |
it 'then my cat will be sad' do | |
expect(subject.happy).to be_falsey | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
module JSONable | |
def self.included(base) | |
base.extend ClassMethods | |
end | |
module ClassMethods | |
def create_from_json(json_string) | |
obj = new | |
object = JSON.parse(json_string) | |
object.each do |key, value| | |
next if key == 'class_name' | |
obj.instance_variable_set key, value | |
end | |
obj | |
end | |
end | |
def to_json(*a) | |
result = {} | |
result[:class_name] = self.class.name | |
self.instance_variables.each do |key| | |
result[key] = self.instance_variable_get key | |
end | |
result.to_json(*a) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module MyModule | |
# this is a constant | |
PI = 3.14 | |
# This is a module function. Good for utility functions | |
def self.foo | |
'foo' | |
end | |
# This class is namespaced | |
class Foo | |
... | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment