Sire

Build Status Gem Version

Simple Immutable Relational Entity

Defining

require 'sire'

Order       = Sire.entity(:line_items, :total)
LineItem    = Sire.entity(:purchasable, :quantity)
LineItems   = Sire.aggregate(LineItem)
Product     = Sire.entity(:name, :price)
Service     = Sire.entity(:name, :rate)
Purchasable = Sire.relation(Product, Service)
Rate        = Sire.entity(:price, :period)

Initializing

rate = Rate[price: 13.99, period: :weekly]
service1 = Service[name: 'Consulting', rate: rate]
service2 = Service[name: 'Support', rate: { price: 56.50, period: :monthly }]
# Service[name: "Support", rate: Rate[price: 56.5, period: :monthly]]
line_item = LineItem[purchasable: { name: 'Hat', price: 5.36 }, quantity: 2]
# LineItem[purchasable: Product[name: "Hat", price: 5.36], quantity: 2]
order = Order[line_items: [line_item, {purchasable: service1, quantity: 1}], total: 12.50]
# Order[line_items: [LineItem[purchasable: Product[name: "Hat", price: 5.36], quantity: 2], LineItem[purchasable: Service[name: "Consulting", rate: Rate[price: 13.99, period: :weekly]], quantity: 1]], total: 12.5]

Usage

rate.price                                    # 13.99
rate.attributes                               # [:price, :period]
rate2 = rate.merge(price: 10.00)              # Rate[price: 10.00, period: :weekly]
rate == rate2                                 # false
rate.map { |k, v| "#{k} = #{v}" }.join(', ')  # "price = 13.99, period = weekly"

Installation

Add this line to your application's Gemfile:

gem 'sire'

Contributing

  1. Fork it ( https://github.com/[my-github-username]/sire/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request