Module: Rack::Mount::GeneratableRegexp::InstanceMethods

Included in:
Rack::Mount::GeneratableRegexp
Defined in:
lib/rack/mount/generatable_regexp.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(obj) ⇒ Object



31
32
33
# File 'lib/rack/mount/generatable_regexp.rb', line 31

def self.extended(obj)
  obj.segments
end

Instance Method Details

#capturesObject



72
73
74
# File 'lib/rack/mount/generatable_regexp.rb', line 72

def captures
  segments.flatten.find_all { |s| s.is_a?(DynamicSegment) }
end

#defaultsObject



42
43
44
# File 'lib/rack/mount/generatable_regexp.rb', line 42

def defaults
  @defaults ||= {}
end

#defaults=(defaults) ⇒ Object



35
36
37
38
39
40
# File 'lib/rack/mount/generatable_regexp.rb', line 35

def defaults=(defaults)
  @required_captures = nil
  @required_params = nil
  @required_defaults = nil
  @defaults = defaults
end

#freezeObject



96
97
98
99
100
101
102
103
# File 'lib/rack/mount/generatable_regexp.rb', line 96

def freeze
  segments
  captures
  required_captures
  required_params
  required_defaults
  super
end

#generatable?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/rack/mount/generatable_regexp.rb', line 46

def generatable?
  segments.any?
end

#generate(params = {}, recall = {}, options = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/rack/mount/generatable_regexp.rb', line 50

def generate(params = {}, recall = {}, options = {})
  return nil unless generatable?

  merged = recall.merge(params)
  return nil unless required_params.all? { |p| merged.include?(p) }
  return nil unless required_defaults.all? { |k, v| merged[k] == v }

  generate_from_segments(segments, params, merged, options)
end

#required_capturesObject



76
77
78
79
80
# File 'lib/rack/mount/generatable_regexp.rb', line 76

def required_captures
  @required_captures ||= segments.find_all { |s|
    s.is_a?(DynamicSegment) && !@defaults.include?(s.name)
  }.freeze
end

#required_defaultsObject



86
87
88
89
90
91
92
93
94
# File 'lib/rack/mount/generatable_regexp.rb', line 86

def required_defaults
  @required_defaults ||= begin
    required_defaults = @defaults.dup
    captures.inject({}) { |h, s| h.merge!(s.to_hash) }.keys.each { |name|
      required_defaults.delete(name)
    }
    required_defaults
  end
end

#required_paramsObject



82
83
84
# File 'lib/rack/mount/generatable_regexp.rb', line 82

def required_params
  @required_params ||= required_captures.map { |s| s.name }.freeze
end

#segmentsObject



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rack/mount/generatable_regexp.rb', line 60

def segments
  @segments ||= begin
    defaults
    segments = []
    catch(:halt) do
      expression = Utils.parse_regexp(self)
      segments = parse_segments(expression)
    end
    segments
  end
end