lib/eco/api/session/batch/base_policy.rb in eco-helpers-2.6.0 vs lib/eco/api/session/batch/base_policy.rb in eco-helpers-2.6.1

- old
+ new

@@ -18,13 +18,14 @@ # @attr_reader attr [Symbol] the Symbol `name` of the current policy # @attr_reader max [Integer] `max` **allowed** number of occurrences of the property # @attr_reader min [Integer] `min` **required** number of occurrences of the property class BasePolicy extend Eco::API::Common::ClassHierarchy + # adds evaluate method to enable block calls as DSL + include Eco::Language::Methods::DslAble class << self - # If the class for `key` exists, it returns it. Otherwise it generates it. # @note for this to work, `key` should be one of the submodels of the current class' `model` # @return [Eco::API::Session::Batch::BasePolicy] or subclass thereof def policy_class(key) key = key.to_sym.freeze @@ -50,20 +51,18 @@ klass = self.class.policy_class(attr) policy = self[attr] = klass.new(attr, _parent: self) end if block - block.call(policy) + policy.evaluate(policy, &block) self else policy end end - end end - end include Enumerable attr_reader :attr @@ -73,10 +72,20 @@ @_parent = _parent @attr = attr.to_sym @policies = {} end + def max(value = :unused) + return @max if value == :unused + self.max = value + end + + def min(value = :unused) + return @min if value == :unused + self.min = value + end + def attr(as_namespace: false) return @attr if !as_namespace || root? "#{@_parent.attr(as_namespace: true)}:#{@attr}" end @@ -220,10 +229,9 @@ def model_to_hash(model) return model if model.is_a?(Hash) model.to_h if model.respond_to?(:to_h) end - end end end end end