Sha256: 80a52dd7c3008e186651760b7ab69434d576cdefd6c3088611ec295bd78915e6

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

# experiment for different nav in rooter

class Lux::Controller::Nav

  attr_reader :root, :path, :id, :full, :locale, :subdomain, :domain

  # acepts path as a string
  def initialize request
    @path = request.path.split('/').slice(1, 100) || []
    @root = @path.shift if @path.first

    @subdomain = request.host.split('.')
    @domain    = @subdomain.pop(2).join('.')
    @domain    += ".#{@subdomain.pop}" if @domain.length < 6

    build_full
  end

  def build_full
    @full = '/%s/%s' % [@root, @path.join('/')]
  end

  def shift_to_root
    @root.tap do
      @root = @path.shift
      build_full
    end
  end

  # used to make admin.lvm.me/users to lvh.me/admin/users
  def unshift name
    @path.unshift @root
    @root = name
    build_full
  end

  def parse_locale
    return false unless Locale.all.include?(@root)
    Locale.current = shift_to_root
    true
  end

  def shift
    @path.shift
  end

  def first
    @path.first
  end

  def second
    @path[1]
  end

  def last
    @path.last
  end

  def rest
    @path.slice(1, @path.length-1)
  end

  def to_s
    @full
  end

  # if sets @id to whatever function returns
  # page.nav.id! { |id| StringBase.extract id }
  def id! &block
    @block = block if block

    return unless first
    @id = @block.call first
    shift if @id
    @id
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lux-fw-0.1.17 ./lib/lux/controller/lib/nav.rb