Class: Route500Check::RouteParamBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/route_500_check/route_param_builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ RouteParamBuilder

======================

DSL 用



8
9
10
# File 'lib/route_500_check/route_param_builder.rb', line 8

def initialize(params)
  @params = params
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/route_500_check/route_param_builder.rb', line 20

def method_missing(name, *args, &block)
  @params[name.to_sym] =
    if args.length <= 1
      args.first
    else
      args
    end
end

Class Method Details

.cartesian_product(values_map) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/route_500_check/route_param_builder.rb', line 98

def self.cartesian_product(values_map)
  keys   = values_map.keys
  values = values_map.values

  values
    .shift
    .product(*values)
    .map { |combo| keys.zip(combo).to_h }
end

.expand(route_defs, default_limit) ⇒ Object

======================

実行用(Runner が呼ぶ)



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
89
90
91
92
93
94
95
96
# File 'lib/route_500_check/route_param_builder.rb', line 36

def self.expand(route_defs, default_limit)
  route_defs.flat_map do |route|
    template = route.template
    params   = route.params || {}

    placeholders =
      template.scan(/:(\w+)/).flatten.map(&:to_sym)

    # placeholder が無い場合
    if placeholders.empty?
      paths = [template]
    else
      values_map =
        placeholders.map do |key|
          raw = params[key]
          values =
            case raw
            when Range
              raw.to_a
            when Array
              raw.flat_map { |v| v.is_a?(Range) ? v.to_a : v }
            else
              [raw]
            end

          [key, values]
        end.to_h

      combinations = cartesian_product(values_map)

      # sample
      if params[:__sample__]
        combinations = combinations.sample(params[:__sample__])
      end

      # limit(route → global の順で適用)
      route_limit  = params[:__limit__]
      global_limit = default_limit

      effective_limit =
        if route_limit && global_limit
          [route_limit, global_limit].min
        else
          route_limit || global_limit
        end

      if effective_limit
        combinations = combinations.take(effective_limit)
      end

      paths =
        combinations.map do |combo|
          path = template.dup
          combo.each { |k, v| path.sub!(":#{k}", v.to_s) }
          path
        end
    end

    paths
  end
end

Instance Method Details

#limit(n) ⇒ Object



16
17
18
# File 'lib/route_500_check/route_param_builder.rb', line 16

def limit(n)
  @params[:__limit__] = n
end

#respond_to_missing?(_name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/route_500_check/route_param_builder.rb', line 29

def respond_to_missing?(_name, _include_private = false)
  true
end

#sample(n) ⇒ Object



12
13
14
# File 'lib/route_500_check/route_param_builder.rb', line 12

def sample(n)
  @params[:__sample__] = n
end