Skip to content

Instantly share code, notes, and snippets.

View mariorcardoso's full-sized avatar

Mario Cardoso mariorcardoso

View GitHub Profile
> product = Product.create(cost: Money.new(500, "EUR"))
> product.cost
=> #<Money fractional:500 currency:EUR>
> product.cost.cents
=> 500
> product.currency
=> "EUR"
class Product < ActiveRecord::Base
def cost
Money.new(cents, currency)
end
def cost=(cost)
self.cents = cost.cents
self.currency = cost.currency.to_s
end
end
class Temperature
include Comparable
attr_reader :degrees
COLD = 20
HOT = 25
def initialize(degrees)
@degrees = degrees
end
class Grade
include Comparable
attr_reader :percentage
def initialize(percentage)
@percentage = percentage
end
def better_than?(other)
self > other
> gary = Person.create(name: "Gary")
> gary.address_city = "Brooklyn"
> gary.address_state = "NY"
> gary.address
=> #<Address:0x007fcbfcce0188 @city="Brooklyn", @state="NY">
> gary.address = Address.new("Brooklyn", "NY")
> gary.address
=> #<Address:0x007fcbfa3b2e78 @city="Brooklyn", @state="NY">
class Address
attr_reader :city, :state
def initialize(city, state)
@city, @state = city, state
end
def ==(other_address)
city == other_address.city && state == other_address.state
end
class Person < ActiveRecord::Base
def address
Address.new(address_city, address_state)
end
def address=(address)
self.address_city = address.city
self.address_state = address.state
end
end
class Event < ActiveRecord::Base
def date_range
DateRange.new(start_date, end_date)
end
def date_range=(date_range)
self.start_date = date_range.start_date
self.end_date = date_range.end_date
end
end
@mariorcardoso
mariorcardoso / date_range.rb
Last active June 25, 2017 11:44
Code samples about value objects
class DateRange
attr_reader :start_date, :end_date
def initialize(start_date, end_date)
@start_date, @end_date = start_date, end_date
end
def include_date?(date)
date >= start_date && date <= end_date
end
class Rectangle {
constructor(height, width, color) {
this.height = height
this.width = width
this.color = color
}
draw(layer, stage) {
const rectangle = new Konva.Rect({
x: Math.random() * stage.getWidth(),