Sha256: 980896096b7011cc0639cd5a6003131d68983485d2ef14c145be51aaff4efc9b

Contents?: true

Size: 1.9 KB

Versions: 1

Compression:

Stored size: 1.9 KB

Contents

module ActiveRoad
  class RequestConditionnalCostLinker
    attr_accessor :unauthorized_constraints, :authorized_constraints, :constraints

    def initialize(constraints = [], external_constraints = {})
      @constraints = constraints
    end 

    def unauthorized_constraints
      @unauthorized_constraints ||= [].tap do |unauthorized_constraints|
        constraints.each do |constraint|
          if constraint.start_with?("~")
            unauthorized_constraints << constraint.gsub("~", "")
          end  
        end
      end
    end

    def authorized_constraints 
      @authorized_constraints ||= [].tap do |authorized_constraints|
        constraints.each do |constraint|
          if !constraint.start_with?("~")
            authorized_constraints << constraint
          end  
        end
      end
    end

    def authorized_constraints_intersection_with?(tags)
      (authorized_constraints & tags == false || authorized_constraints & tags == []) ? false : true
    end

    def unauthorized_constraints_intersection_with?(tags)
      (unauthorized_constraints & tags == false || unauthorized_constraints & tags == []) ? false : true 
    end

    def linked?(conditionnal_costs_tags)
      authorized_constraints_intersection_with?(conditionnal_costs_tags) || unauthorized_constraints_intersection_with?(conditionnal_costs_tags)
    end

    def conditionnal_costs_linked(conditionnal_costs)
      conditionnal_costs.where(:tags => authorized_constraints)
    end

    def conditionnal_costs_sum(conditionnal_costs)
      conditionnal_costs_tags = conditionnal_costs.collect(&:tags)  
      if linked?(conditionnal_costs_tags)
        if unauthorized_constraints && unauthorized_constraints_intersection_with?(conditionnal_costs_tags)
          return Float::MAX
        else
          return conditionnal_costs_linked(conditionnal_costs).collect(&:cost).sum 
        end
      else
        0
      end
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_road-0.0.3 app/models/active_road/request_conditionnal_cost_linker.rb