Class: Wallaby::View::CustomPrefixes

Inherits:
Object
  • Object
show all
Defined in:
lib/wallaby/view/custom_prefixes.rb

Overview

Custom prefix builder to add more lookup prefix paths to given #prefixes.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefixes:, action_name:, themes:, options:) ⇒ CustomPrefixes

Create the instance

Parameters:

  • prefixes (Array<String>)
  • action_name (String)
  • themes (String)
  • options (Hash)


86
87
88
89
90
91
# File 'lib/wallaby/view/custom_prefixes.rb', line 86

def initialize(prefixes:, action_name:, themes:, options:)
  @prefixes = prefixes
  @action_name = action_name
  @themes = themes
  @options = (options || {}).with_indifferent_access
end

Instance Attribute Details

#action_nameString (readonly)

Action name to be added

Returns:

  • (String)


16
17
18
# File 'lib/wallaby/view/custom_prefixes.rb', line 16

def action_name
  @action_name
end

#optionsHash (readonly)

Options for extending the given prefixes

Returns:

  • (Hash)


27
28
29
# File 'lib/wallaby/view/custom_prefixes.rb', line 27

def options
  @options
end

#prefixesArray<String> (readonly)

Base prefixes to extend

Returns:

  • (Array<String>)

See Also:



11
12
13
# File 'lib/wallaby/view/custom_prefixes.rb', line 11

def prefixes
  @prefixes
end

#themesArray<Hash> (readonly)

Themes to be inserted

Returns:

  • (Array<Hash>)

See Also:

  • Themeable#.themes


22
23
24
# File 'lib/wallaby/view/custom_prefixes.rb', line 22

def themes
  @themes
end

Class Method Details

.execute(prefixes:, action_name:, themes: nil, options: nil, &block) ⇒ Array<String>

Extend given prefixes with action name and theme name

Examples:

To extend given prefixes:

Wallaby::View::CustomPrefixes.execute(
  prefixes: ['users', 'application'], action_name: 'index'
)

# => [
#   'users/index',
#   'users',
#   'application/index',
#   'application'
# ]

To extend given prefixes with themes:

Wallaby::View::CustomPrefixes.execute(
  prefixes: ['users', 'application'], action_name: 'index',
  themes: [{ theme_name: 'secure', theme_path: 'users' }]
)

# => [
#   'users/index',
#   'users',
#   'secure/index',
#   'secure',
#   'application/index',
#   'application'
# ]

To extend given prefixes with mapped action:

Wallaby::View::CustomPrefixes.execute(
  prefixes: ['users', 'application'], action_name: 'edit',
  options: { 'edit' => 'form' }
)

# => [
#   'users/form',
#   'users',
#   'application/form',
#   'application'
# ]

Parameters:

  • prefixes (Array<String>)
  • action_name (String)
  • themes (String, nil) (defaults to: nil)
  • options (Hash, nil) (defaults to: nil)

Returns:

  • (Array<String>)


72
73
74
75
76
77
78
79
# File 'lib/wallaby/view/custom_prefixes.rb', line 72

def self.execute(
  prefixes:, action_name:, themes: nil, options: nil, &block
)
  new(
    prefixes: prefixes, action_name: action_name,
    themes: themes, options: options
  ).execute(&block)
end

Instance Method Details

#execute(&block) ⇒ Array<String>

Extend given prefixes with action name and theme name

Returns:

  • (Array<String>)


95
96
97
98
99
100
101
# File 'lib/wallaby/view/custom_prefixes.rb', line 95

def execute(&block)
  new_prefixes(&block).each_with_object([]) do |prefix, array|
    # Extend the prefix with actions
    actions.each { |action| array << "#{prefix}/#{action}" }
    array << prefix
  end
end