Sha256: 9e6010c07fc63db115dfafe64a1becea7affcbb3cffb49ba4a3512b4ae71bb9d

Contents?: true

Size: 1.32 KB

Versions: 9

Compression:

Stored size: 1.32 KB

Contents

# A Role gives an object (e.g. an User) specific behaviour (or methods).
#
# == Example
#
#   module TeamMember
#     extend Neo::DCI::Role
#
#     def self.assignable_to?(user, team)
#       team.member?(user)
#     end
#
#     def leave_team!(team)
#       # ...
#     end
#   end
#
#   class User
#     include Neo::DCI::Data
#   end
#
#   team_member = team.members.first
#   team_member.role_as TeamMember
#   team_member.leave_team!(team)
#
#   non_member = User.find(1)
#   non_member.role_as TeamMember # => raises Role::NotAssignable
#
module Neo
  module DCI
    module Role
      # Decides if the role is assignable to +object+.
      #
      # The decision can determined by +object+ and +params+.
      #
      # Returns +true+ by default so every +object+ can have that role.
      def assignable_to?(object, *params)
        true
      end

      # Hook after a role has been assigned.
      #
      # Useful for extend the role with other roles.
      def role_assigned(object, *params)
      end

      class NotAssignable < StandardError
        attr_reader :role, :object, :params

        def initialize(role, object, params)
          super "Role #{role} not assignable to #{object} with params #{params.inspect}"
          @role   = role
          @object = object
          @params = params
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
neo-dci-0.6.0 lib/neo/dci/role.rb
neo-dci-0.5.1 lib/neo/dci/role.rb
neo-dci-0.5.0 lib/neo/dci/role.rb
neo-dci-0.4.1 lib/neo/dci/role.rb
neo-dci-0.4.0 lib/neo/dci/role.rb
neo-dci-0.3.0 lib/neo/dci/role.rb
neo-dci-0.2.0 lib/neo/dci/role.rb
neo-dci-0.1.0 lib/neo/dci/role.rb
neo-dci-0.0.1 lib/neo/dci/role.rb