class Tram::Page

Constants

VERSION

Attributes

i18n_scope[RW]

Public Class Methods

section(name, overload: false, value: nil, **options) click to toggle source

Defines a section of the page as a reference to its public method, and creates/overloads the method if necessary.

@param [#to_sym] name The name of the section

@option [Boolean] :overload

If this definition can overload a previous one

@option [Proc] :value (nil)

A new content of the referred method

@option options [#to_sym] :method (name)

The name of public method to take value from

@option options [#to_sym] :if (nil)

The name of public method to check if the section should be displayed

@option options [#to_sym] :unless (nil)

The name of public method to check if the section should be hidden

@return [self] itself

# File lib/tram/page.rb, line 31
def section(name, overload: false, value: nil, **options)
  name = name.to_sym
  raise "Section #{name} already exists" if !overload && sections.key?(name)

  section = Section.new(name, options)
  define_method(section.source, &value) if value

  sections[name] = section
  self
end
sections() click to toggle source

The hash of definitions for the page's sections

@return [Hash]

# File lib/tram/page.rb, line 58
def sections
  @sections ||= {}
end
url_helper(name) click to toggle source

Makes Rails url helper method accessible from within a page instance

@param [#to_s] name The name of the helper method

@return [self] itself

# File lib/tram/page.rb, line 48
def url_helper(name)
  raise "Rails url_helpers module is not defined" unless defined?(Rails)
  delegate name, to: :"Rails.application.routes.url_helpers"
  self
end

Public Instance Methods

to_h(except: nil, only: nil, **) click to toggle source
# File lib/tram/page.rb, line 63
def to_h(except: nil, only: nil, **)
  sections = self.class.sections.dup
  sections.select! { |k, _| Array(only).include? k }   if only
  sections.reject! { |k, _| Array(except).include? k } if except
  sections.map { |_, section| section.call(self) }.reduce({}, :merge!)
end

Private Instance Methods

t(key, **options) click to toggle source
# File lib/tram/page.rb, line 72
def t(key, **options)
  raise "I18n is not defined" unless defined?(I18n)
  default_scope = [Tram::Page.i18n_scope, self.class.name.underscore]
  I18n.t key, scope: default_scope, **options
end