Class: Rage::Router::Constrainer
- Inherits:
-
Object
- Object
- Rage::Router::Constrainer
- Defined in:
- lib/rage/router/constrainer.rb
Instance Attribute Summary collapse
-
#strategies ⇒ Object
readonly
Returns the value of attribute strategies.
Instance Method Summary collapse
-
#__build_derive_constraints ⇒ Object
Optimization: build a fast function for deriving the constraints for all the strategies at once.
- #derive_constraints(env) ⇒ Object
- #has_constraint_strategy(strategy_name) ⇒ Object
-
#initialize(custom_strategies) ⇒ Constrainer
constructor
A new instance of Constrainer.
- #new_store_for_constraint(constraint) ⇒ Object
-
#note_usage(constraints) ⇒ Object
When new constraints start getting used, we need to rebuild the deriver to derive them.
- #strategy_used?(strategy_name) ⇒ Boolean
- #validate_constraints(constraints) ⇒ Object
Constructor Details
#initialize(custom_strategies) ⇒ Constrainer
Returns a new instance of Constrainer.
6 7 8 9 10 11 12 |
# File 'lib/rage/router/constrainer.rb', line 6 def initialize(custom_strategies) @strategies = { host: Rage::Router::Strategies::Host.new } @strategies_in_use = Set.new end |
Instance Attribute Details
#strategies ⇒ Object (readonly)
Returns the value of attribute strategies.
4 5 6 |
# File 'lib/rage/router/constrainer.rb', line 4 def strategies @strategies end |
Instance Method Details
#__build_derive_constraints ⇒ Object
Optimization: build a fast function for deriving the constraints for all the strategies at once. We inline the definitions of the version constraint and the host constraint for performance. If no constraining strategies are in use (no routes constrain on host, or version, or any custom strategies) then we don’t need to derive constraints for each route match, so don’t do anything special, and just return undefined This allows us to not allocate an object to hold constraint values if no constraints are defined.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/rage/router/constrainer.rb', line 62 def __build_derive_constraints return if @strategies_in_use.empty? lines = ["{"] @strategies_in_use.each do |key| strategy = @strategies[key] # Optimization: inline the derivation for the common built in constraints if !strategy.custom? if key == :host lines << " host: env['HTTP_HOST'.freeze]," else raise ArgumentError, "unknown non-custom strategy for compiling constraint derivation function" end else lines << " #{strategy.name}: @strategies[#{key}].derive_constraint(env)," end end lines << "}" instance_eval <<-RUBY def derive_constraints(env) #{lines.join("\n")} end RUBY end |
#derive_constraints(env) ⇒ Object
27 28 |
# File 'lib/rage/router/constrainer.rb', line 27 def derive_constraints(env) end |
#has_constraint_strategy(strategy_name) ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/rage/router/constrainer.rb', line 18 def has_constraint_strategy(strategy_name) custom_constraint_strategy = @strategies[strategy_name] if custom_constraint_strategy return custom_constraint_strategy.custom? || strategy_used?(strategy_name) end false end |
#new_store_for_constraint(constraint) ⇒ Object
45 46 47 48 |
# File 'lib/rage/router/constrainer.rb', line 45 def new_store_for_constraint(constraint) raise ArgumentError, "No strategy registered for constraint key '#{constraint}'" unless @strategies[constraint] @strategies[constraint].storage end |
#note_usage(constraints) ⇒ Object
When new constraints start getting used, we need to rebuild the deriver to derive them. Do so if we see novel constraints used.
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rage/router/constrainer.rb', line 31 def note_usage(constraints) if constraints before_size = @strategies_in_use.size constraints.each_key do |key| @strategies_in_use.add(key) end if before_size != @strategies_in_use.size __build_derive_constraints end end end |
#strategy_used?(strategy_name) ⇒ Boolean
14 15 16 |
# File 'lib/rage/router/constrainer.rb', line 14 def strategy_used?(strategy_name) @strategies_in_use.include?(strategy_name) end |
#validate_constraints(constraints) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/rage/router/constrainer.rb', line 50 def validate_constraints(constraints) constraints.each do |key, value| strategy = @strategies[key] raise ArgumentError, "No strategy registered for constraint key '#{key}'" unless strategy strategy.validate(value) end end |