lib/surrounded/context.rb in surrounded-0.8.0 vs lib/surrounded/context.rb in surrounded-0.8.1
- old
+ new
@@ -56,33 +56,51 @@
class << self
attr_writer :default_role_type
end
- # Additional Features
-
# Provide the ability to create access control methods for your triggers.
def protect_triggers; self.extend(::Surrounded::AccessControl); end
# Automatically create class methods for each trigger method.
def shortcut_triggers; self.extend(::Surrounded::Shortcuts); end
- def private_const_set(name, const)
- const = const_set(name, const)
- private_constant name.to_sym
- const
- end
-
def default_role_type
@default_role_type ||= Surrounded::Context.default_role_type
end
# Set the default type of implementation for role method for an individual context.
def default_role_type=(type)
@default_role_type = type
end
+ # Set the time to apply roles to objects. Either :trigger or :initialize.
+ # Defaults to :trigger
+ def apply_roles_on(which)
+ @__apply_role_policy = which
+ end
+
+ def __apply_role_policy
+ @__apply_role_policy ||= :trigger
+ end
+
+ # Shorthand for creating an instance level initialize method which
+ # handles the mapping of the given arguments to their named role.
+ def initialize(*setup_args)
+ private_attr_reader(*setup_args)
+
+ class_eval "
+ def initialize(#{setup_args.join(',')})
+ preinitialize
+ arguments = method(__method__).parameters.map{|arg| eval(arg[1].to_s) }
+ @role_map = RoleMap.new
+ map_roles(#{setup_args}.zip(arguments))
+ postinitialize
+ end
+ ", __FILE__, __LINE__
+ end
+
# Create a named behavior for a role using the standard library SimpleDelegator.
def wrap(name, &block)
require 'delegate'
wrapper_name = name.to_s.gsub(/(?:^|_)([a-z])/){ $1.upcase }
klass = private_const_set(wrapper_name, Class.new(SimpleDelegator, &block))
@@ -119,39 +137,10 @@
rescue NameError => e
raise e.extend(InvalidRoleType)
end
alias_method :role_methods, :role
- def apply_roles_on(which)
- @__apply_role_policy = which
- end
-
- def __apply_role_policy
- @__apply_role_policy ||= :trigger
- end
-
- # Shorthand for creating an instance level initialize method which
- # handles the mapping of the given arguments to their named role.
- def initialize(*setup_args)
- private_attr_reader(*setup_args)
-
- class_eval "
- def initialize(#{setup_args.join(',')})
- preinitialize
- arguments = method(__method__).parameters.map{|arg| eval(arg[1].to_s) }
- @role_map = RoleMap.new
- map_roles(#{setup_args}.zip(arguments))
- postinitialize
- end
- ", __FILE__, __LINE__
- end
-
- def private_attr_reader(*method_names)
- attr_reader(*method_names)
- private(*method_names)
- end
-
# Creates a context instance method which will apply behaviors to role players
# before execution and remove the behaviors after execution.
#
# Alternatively you may define your own methods then declare them as triggers
# afterward.
@@ -184,16 +173,10 @@
end
def store_trigger(*names)
@triggers.merge(names)
end
-
- def role_const(name)
- if const_defined?(name)
- const_get(name)
- end
- end
def define_trigger_method(name, &block)
unless triggers.include?(name) || name.nil?
alias_method :"__trigger_#{name}", :"#{name}"
private :"__trigger_#{name}"
@@ -214,9 +197,33 @@
ensure
remove_roles if __apply_role_policy == :trigger
end
end
}, __FILE__, __LINE__
+ end
+
+ # === Utility shortcuts
+
+ # Set a named constant and make it private
+ def private_const_set(name, const)
+ unless self.const_defined?(name, false)
+ const = const_set(name, const)
+ private_constant name.to_sym
+ end
+ const
+ end
+
+ # Create attr_reader for the named methods and make them private
+ def private_attr_reader(*method_names)
+ attr_reader(*method_names)
+ private(*method_names)
+ end
+
+ # Conditional const_get for a named role behavior
+ def role_const(name)
+ if const_defined?(name)
+ const_get(name)
+ end
end
module InstanceMethods
# Check whether a given name is a role inside the context.
# The provided block is used to evaluate whether or not the caller
\ No newline at end of file