Class: Jets::Router::Scope

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/jets/router/scope.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#get_controller_action, #handle_on!, #join, #underscore

Constructor Details

#initialize(options = {}, parent = nil, level = 1) ⇒ Scope

Returns a new instance of Scope.



7
8
9
10
11
# File 'lib/jets/router/scope.rb', line 7

def initialize(options = {}, parent = nil, level = 1)
  @options = options
  @parent = parent
  @level = level
end

Instance Attribute Details

#levelObject (readonly)

Returns the value of attribute level.



6
7
8
# File 'lib/jets/router/scope.rb', line 6

def level
  @level
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/jets/router/scope.rb', line 6

def options
  @options
end

#parentObject (readonly)

Returns the value of attribute parent.



6
7
8
# File 'lib/jets/router/scope.rb', line 6

def parent
  @parent
end

Instance Method Details

#fromObject



90
91
92
# File 'lib/jets/router/scope.rb', line 90

def from
  @options[:from]
end

#full_asObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/jets/router/scope.rb', line 54

def full_as
  items = []
  current = self
  while current
    items.unshift(current.options[:as]) # <= option_name
    current = current.parent
  end

  items.compact!
  return if items.empty?

  items = singularize_leading(items)
  items.join('_')
end

#full_moduleObject



21
22
23
24
25
26
27
28
29
# File 'lib/jets/router/scope.rb', line 21

def full_module
  items = walk_parents do |current, i, result|
    mod = current.options[:module]
    next unless mod
    result.unshift(mod)
  end

  items.empty? ? nil : items.join('/')
end

#full_prefixObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/jets/router/scope.rb', line 31

def full_prefix
  items = walk_parents do |current, i, result|
    prefix = current.options[:prefix]
    next unless prefix

    case current.from
    when :resources
      path_param = if current.options[:param]
        ":#{current.options[:param]}"
      else
        resource_name = prefix.to_s.split('/').last
        resource_name = ":#{resource_name.singularize}_id"
      end
      result.unshift(path_param)
      result.unshift(prefix)
    else # resource, namespace or general scope
      result.unshift(prefix)
    end
  end

  items.empty? ? nil : items.join('/')
end

#new(options = {}) ⇒ Object



17
18
19
# File 'lib/jets/router/scope.rb', line 17

def new(options={})
  self.class.new(options, self, level + 1)
end

#root?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/jets/router/scope.rb', line 13

def root?
  @parent.nil?
end

#singularize_leading(items) ⇒ Object

singularize all except last item



80
81
82
83
84
85
86
87
88
# File 'lib/jets/router/scope.rb', line 80

def singularize_leading(items)
  result = []
  items.each_with_index do |item, index|
    item = item.to_s
    r = index == items.size - 1 ? item : item.singularize
    result << r
  end
  result
end

#walk_parentsObject



69
70
71
72
73
74
75
76
77
# File 'lib/jets/router/scope.rb', line 69

def walk_parents
  current, i, result = self, 0, []
  while current
    yield(current, i, result)
    current = current.parent
    i += 1
  end
  result
end