Class: Drawers::ResourceParts

Inherits:
Object
  • Object
show all
Defined in:
lib/drawers/resource_parts.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ ResourceParts

Returns a new instance of ResourceParts.



28
29
30
# File 'lib/drawers/resource_parts.rb', line 28

def initialize(name)
  @qualified_name = name
end

Instance Attribute Details

#class_pathObject (readonly)

Returns the value of attribute class_path.



4
5
6
# File 'lib/drawers/resource_parts.rb', line 4

def class_path
  @class_path
end

#named_resource_typeObject (readonly)

Returns the value of attribute named_resource_type.



4
5
6
# File 'lib/drawers/resource_parts.rb', line 4

def named_resource_type
  @named_resource_type
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



4
5
6
# File 'lib/drawers/resource_parts.rb', line 4

def namespace
  @namespace
end

#resource_nameObject (readonly)

Returns the value of attribute resource_name.



4
5
6
# File 'lib/drawers/resource_parts.rb', line 4

def resource_name
  @resource_name
end

#resource_typeObject (readonly)

Returns the value of attribute resource_type.



4
5
6
# File 'lib/drawers/resource_parts.rb', line 4

def resource_type
  @resource_type
end

Class Method Details

.call(name) ⇒ Object



21
22
23
24
25
# File 'lib/drawers/resource_parts.rb', line 21

def call(name)
  resource = new(name)
  resource.call
  resource
end

.from_name(name) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/drawers/resource_parts.rb', line 9

def from_name(name)
  resource = call(name)

  [
    resource.namespace,
    resource.resource_name,
    resource.resource_type,
    resource.named_resource_type,
    resource.class_path
  ]
end

Instance Method Details

#callObject



32
33
34
35
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
# File 'lib/drawers/resource_parts.rb', line 32

def call
  # if this is not part of a resource, don't even bother
  return unless index_of_resource_type

  # Api, V2, Post, Operations, Update
  # => Operations
  @resource_type = qualified_parts[index_of_resource_type]

  # Api, V2, Post, Operations, Update
  # => Posts
  #
  # Posts, Controller
  # => Posts
  original_resource_name = qualified_parts[index_of_resource_type - 1]
  @resource_name = original_resource_name.pluralize

  # Posts_Controller
  # Post_Operations
  @named_resource_type = "#{original_resource_name}_#{@resource_type}"

  # Api, V2, Post, Operations, Update
  # => Api, V2
  namespace_index = index_of_resource_type - 1
  @namespace = namespace_index < 1 ? '' : qualified_parts.take(namespace_index)

  # Api, V2, Post, Operations, Update
  # => Update
  class_index = index_of_resource_type + 1
  @class_path = class_index < 1 ? '' : qualified_parts.drop(class_index)
end