lib/aequitas/contextual_rule_set.rb in aequitas-0.0.1 vs lib/aequitas/contextual_rule_set.rb in aequitas-0.0.2

- old
+ new

@@ -1,26 +1,26 @@ # -*- encoding: utf-8 -*- require 'forwardable' -require 'aequitas/support/equalizable' +require 'aequitas/support/value_object' require 'aequitas/exceptions' require 'aequitas/context' require 'aequitas/rule_set' module Aequitas class ContextualRuleSet - extend Equalizable + extend ValueObject extend Forwardable include Enumerable equalize_on :rule_sets # MessageTransformer to use for transforming Violations on Resources # instantiated from the model to which this ContextualRuleSet is bound - # + # # @api public - # attr_accessor :transformer + attr_accessor :transformer # @api private attr_reader :rule_sets def_delegators :rule_sets, :each, :empty? @@ -42,11 +42,11 @@ self end # Delegate #validate to RuleSet - # + # # @api public def validate(resource, context_name) context(context_name).validate(resource) end @@ -54,44 +54,44 @@ # # @param [String] name # Context name for which to return a RuleSet # @return [RuleSet] # RuleSet for the given context - # + # # @api public def context(context_name) rule_sets.fetch(context_name) end # Retrieve Rules applicable to a given attribute name - # + # # @param [Symbol] attribute_name # name of the attribute for which to retrieve applicable Rules - # + # # @return [Array] # list of Rules applicable to +attribute_name+ def [](attribute_name) context(:default).fetch(attribute_name, []) end # Create a new rule of the given class for each name in +attribute_names+ # and add the rules to the RuleSet(s) indicated - # + # # @param [Aequitas::Rule] rule_class # Rule class, example: Aequitas::Rule::Presence # # @param [Array<Symbol>] attribute_names # Attribute names given to validation macro, example: # [:first_name, :last_name] in validates_presence_of :first_name, :last_name - # + # # @param [Hash] options # Options supplied to validation macro, example: # {:context=>:default, :maximum=>50, :allow_nil=>true, :message=>nil} - # + # # @option [Symbol] :context, :group, :when, :on # the context in which the new rule should be run - # + # # @return [self] def add(rule_class, attribute_names, options = {}, &block) context_names = extract_context_names(options) attribute_names.each do |attribute_name| @@ -102,30 +102,30 @@ self end # Assimilate all rules contained in +other+ into the receiver - # + # # @param [ContextualRuleSet] other # the ContextualRuleSet whose rules are to be assimilated - # + # # @return [self] def concat(other) other.rule_sets.each do |context_name, rule_set| add_rules_to_context(context_name, rules) end self end # Define a context and append rules to it - # + # # @param [Symbol] context_name # name of the context to define and append rules to # @param [RuleSet, Array] rules # Rules to append to +context_name+ - # + # # @return [self] def add_rules_to_context(context_name, rules) define_context(context_name) context(context_name).concat(rules) @@ -142,13 +142,13 @@ # the current validation context from the stack (if valid for this model), # nil if no context name is on the stack and no contexts are defined for # this model, or :default if the context on the stack is invalid for # this model or no context is on the stack and this model has at least # one validation context - # + # # @api private - # + # # TODO: this logic behind this method is too complicated. # simplify the semantics of #current_context, #validate def current_context context = Aequitas::Context.current valid_context?(context) ? context : :default @@ -183,11 +183,11 @@ # # @raise [InvalidContextError] # raised if the context is not valid for this contextual rule set # # @api private - # + # # TODO: is this method actually needed? def assert_valid_context(context_name) unless valid_context?(context_name) actual = context_name.inspect expected = rule_sets.keys.inspect @@ -196,25 +196,19 @@ end private # Allow :context to be aliased to :group, :when & :on - # + # # @param [Hash] options # the options from which +context_names+ is to be extracted - # + # # @return [Array(Symbol)] # the context name(s) from +options+ - # + # # @api private def extract_context_names(options) - context_name = [ - options.delete(:context), - options.delete(:group), - options.delete(:when), - options.delete(:on) - ].compact.first - + context_name = options.values_at(:context, :group, :when, :on).compact.first Array(context_name || :default) end end # class ContextualRuleSet end # module Aequitas