Sha256: fd03bfff0693c1279499ea441d8de41108b23946e84f23f50ea5dfd5f33a7cb7

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

# Here's an example of how you might use this in rails.

# First, be guarded against changes in third-party libraries
module Awareness
  include Surrounded
  include Casting::Client
  delegate_missing_methods
end

class User
  include Awareness
end

class ApplicationController
  include Awareness
end

class SomeUseCase
  extend Surrounded::Context

  setup(:user, :other_user, :listener)

  trigger :do_something do
    user.something
  end

  module User
    def something
      puts "Hello, #{other_user}"
      listener.redirect_to('/')
    end
  end
end

class SomethingController < ApplicationController
  use_case = :SomeUseCase
  def create
    use_case = :SomeOverrideUseCase
    trigger :do_something, current_user, User.last
  end

  def trigger(meth, *args)
    use_case.new(*(args << self)).send(meth)
  end

  def self.use_case=(name)
    @_default_use_case_name = name
  end

  def self.use_case
    @_default_use_case_name.to_s.constantize
  end

  def use_case=(name)
    @_use_case_name = name
  end

  def use_case
    if defined?(@_use_case_name)
      @_use_case_name.to_s.constantize
    else
      self.class.use_case
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
surrounded-0.1.0 examples/rails.rb