Skip to Content
Lean Architecture for Agile Software Development
book

Lean Architecture for Agile Software Development

by James Coplien, Gertrud Bjørnvig
August 2010
Intermediate to advanced
376 pages
10h 6m
English
Wiley
Content preview from Lean Architecture for Agile Software Development

Appendix D. Account Example in Ruby

This rendition comes from Steen Lehmann.

require 'osx/cocoa'

#!/usr/bin/env ruby
# Lean Architecture example in Ruby -
# with ContextAccessor

# Module that can be mixed in to any class
# that needs access to the current context. It is
# implemented as a thread-local variable.

module ContextAccessor
  def context
    Thread.current[:context]
  end

  def context=(ctx)
    Thread.current[:context] = ctx
  end

  def execute_in_context
    old_context = self.context
    self.context = self
    yield
    self.context = old_context
  end
end
#
# This is the base class (common code) for all
# Account domain classes.
#

class Account
  attr_reader :account_id, :balance

  def initialize(account_id)
    @account_id = account_id
    @balance = 0
  end

  def decreaseBalance(amount)
    raise "Bad argument to withdraw" if amount < 0
    raise "Insufficient funds" if amount > balance
    @balance -= amount
  end

  def increaseBalance(amount)
    @balance += amount
  end

  def update_log(msg, date, amount)
    puts "Account: #{inspect}, #{msg}, \ #{date.to_s},
       #{amount}"
  end

  def self.find(account_id)
    @@store ||= Hash.new
    return @@store[account_id] if @@store.has_key?
          account_id

    if :savings == account_id
      account = SavingsAccount.new(account_id)
      account.increaseBalance(100000)
    elsif :checking == account_id
      account = CheckingAccount.new(account_id)
    else
      account = Account.new(account_id)
    end
    @@store[account_id] = account
    account
  end
end
# This module is the methodless role type. Since # we don't really use types to declare identifiers, # it's kind ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Agile Software Architecture

Agile Software Architecture

Muhammad Ali Babar, Alan W. Brown, Ivan Mistrik
Lean Software Development: An Agile Toolkit

Lean Software Development: An Agile Toolkit

Mary Poppendieck, Tom Poppendieck

Publisher Resources

ISBN: 9780470684207Purchase book