Sha256: 3b32700bbaa96e5b0923f73c8ec0159a529198eb30fcc1a7603dc937aaac853e

Contents?: true

Size: 881 Bytes

Versions: 3

Compression:

Stored size: 881 Bytes

Contents

# If you want to use wrappers, here's how you could

require 'surrounded/context'
require 'delegate'
class WrapperContext
  extend Surrounded::Context

  apply_roles_on(:trigger)
  setup(:admin, :task)

  class Admin < SimpleDelegator
    def some_admin_method
      puts 'hello from the admin wrapper!'
    end
  end

  trigger :do_something do
    admin.some_admin_method
  end

  private

  def add_role_methods(obj, wrapper)
    assign_role(role_name(wrapper), wrapper.new(obj))
  end

  def remove_role_methods(obj, wrapper)
    # in this case, the obj is already wrapped
    core_object = self.send(role_name(wrapper)).__getobj__
    assign_role(role_name(wrapper), core_object)
  end

  def role_name(klass)
    klass.name.split("::").last.gsub(/([A-Z])/){ "_#{$1.downcase}" }.sub(/^_/,'')
  end
end

context = WrapperContext.new(Object.new, Object.new)
context.do_something

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
surrounded-0.3.0 examples/wrappers.rb
surrounded-0.2.1 examples/wrappers.rb
surrounded-0.2.0 examples/wrappers.rb