Class: Titleist::Title
- Inherits:
-
Object
- Object
- Titleist::Title
- Defined in:
- lib/titleist/title.rb
Overview
Object that turns passed-in scope details into a String of title text derived from the I18n
locale configuration.
Constant Summary collapse
- ROOT_SCOPE =
Top-level scope in the i18n locale for all title configuration.
:titles
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#controller ⇒ Object
readonly
Returns the value of attribute controller.
-
#page ⇒ String
Page title from given scope.
Instance Method Summary collapse
-
#app ⇒ String
Global application title.
-
#initialize(controller: '', action: '', context: {}, root: false) ⇒ Title
constructor
A new instance of Title.
-
#root? ⇒ Boolean
Whether the page we’re generating a title for is the root path.
-
#to_h ⇒ Hash
Format this title as a
Hash
of attributes. -
#to_s ⇒ String
Format this title as a
String
.
Constructor Details
#initialize(controller: '', action: '', context: {}, root: false) ⇒ Title
Returns a new instance of Title.
24 25 26 27 28 29 |
# File 'lib/titleist/title.rb', line 24 def initialize(controller: '', action: '', context: {}, root: false) @controller = controller @action = action @context = context @root = root end |
Instance Attribute Details
#action ⇒ Object (readonly)
Returns the value of attribute action.
14 15 16 |
# File 'lib/titleist/title.rb', line 14 def action @action end |
#context ⇒ Object (readonly)
Returns the value of attribute context.
18 19 20 |
# File 'lib/titleist/title.rb', line 18 def context @context end |
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
10 11 12 |
# File 'lib/titleist/title.rb', line 10 def controller @controller end |
#page ⇒ String
Page title from given scope.
44 45 46 47 48 49 |
# File 'lib/titleist/title.rb', line 44 def page @page ||= I18n.t action.to_sym, context.merge( scope: scope, default: default_page_title ) end |
Instance Method Details
#app ⇒ String
Global application title.
34 35 36 37 38 39 |
# File 'lib/titleist/title.rb', line 34 def app I18n.t :application, context.merge( scope: ROOT_SCOPE, default: default_app_title ) end |
#root? ⇒ Boolean
Whether the page we’re generating a title for is the root path. This will cause no page title to display, and is set when the title object is instantiated.
81 82 83 |
# File 'lib/titleist/title.rb', line 81 def root? @root end |
#to_h ⇒ Hash
Format this title as a Hash
of attributes.
68 69 70 71 72 73 74 |
# File 'lib/titleist/title.rb', line 68 def to_h { scope: ROOT_SCOPE, app: app, page: page } end |
#to_s ⇒ String
Format this title as a String
.
59 60 61 62 63 |
# File 'lib/titleist/title.rb', line 59 def to_s return app if root? I18n.t :format, to_h end |