towry
7/17/2017 - 2:06 PM

describe the DCI(Data, context, interaction) architecture in Ruby.

describe the DCI(Data, context, interaction) architecture in Ruby.

# The data, doesn't change offen.

class User
  def initialize
    @name = "towry"
  end
end
# interaction, the role.

module Customer
  def buy(book)
    puts @name
    puts "bought a book"
    # or do other thing about the
  end
end
require_relative "customer"
require_relative "user"

# context bind role to object.
# by using the extend method in ruby.
# now the data object has some behaviors that get from the role class(Customer).
class Context
  def initialize(user, book)
    @customer, @book = user, book
    @customer.extend(Customer)
  end

  def call
    @customer.buy(@book)
  end
end

user = User.new
ctx = Context.new(user, "a book")
ctx.call