Pad
A gem that enables a Ports and Adapter architectural style with Domain Driven Design.
Installation
Add this line to your application's Gemfile:
gem 'pad'
And then execute:
$ bundle
Or install it yourself as:
$ gem install pad
Usage
Models
You can create your own models and use Pad attributes by including Pad.model
in your classes.
This will add an attribute
method you can use to add attributes to your class.
require 'pad'
class Vehicle
include Pad.model
attribute :year
attribute :manufacturer
attribute :make
end
Attribute values can be passed into the constructor as a hash using a key of the attribute name.
sienna = Vehicle.new year: 2006, manufacturer: 'Toyota', make: 'Sienna' # => #<Vehicle:0x007fcdacd2ff38 @year=2006, @manufacturer="Toyota", @make="Sienna">
And readers and writers are available for each attribute:
sienna.year # => 2006
sienna.year = 2007
sienna # => #<Vehicle:0x007fcdacd2ff38 @year=2007, @manufacturer="Toyota", @make="Sienna">
You can set the type of an attribute
attribute :name, String
You can set the visibility of attributes:
attribute :private, String, reader: :private, writer: :private
attribute :protected, String, reader: :protected, writer: :protected
attribute :public, String, reader: :public, writer: :public
Entities
You can create entities which extend models with a built-in 'id' attribute which uniquely identifies the entity.
require 'pad'
class Person
include Pad.entity
attribute :name
attribute :age
end
dave = Person.new id: 21, name: "Dave", age: 32 # => #<Person:0x007feaf4a3c668 @id=21, @name="Dave", @age=32>
dave.id # => 21
dave.name # => "Dave"
dave.age # => 32
another_dave = Person.new id: 21
dave == another_dave # => true
Value Objects
You can create value objects which have use read-only attribute values to denote equality.
Attribute values are passed into the constructor as a hash. Attributes to be considered
when checking equality are surrounded by a values
block.
require 'pad'
class Price
include Pad.value_object
values do
attribute :cost
attribute :currency
end
attribute :id
end
pen1 = Price.new id: 1, cost: 100, currency: 'CAD' # => ##<Price cost=100 currency="CAD">
pen2 = Price.new id: 2, cost: 100, currency: 'CAD' # => ##<Price cost=100 currency="CAD">
pen3 = Price.new id: 3, cost: 999, currency: 'CAD' # => ##<Price cost=100 currency="CAD">
pen1 == pen2 # => true
pen1 == pen3 # => false
Contributing
- Fork it ( https://github.com/dwhelan/pad/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request