Class: Nav::Link

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Link

Returns a new instance of Link.



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/rails_nav.rb', line 257

def initialize(*args, &block)
#
  @options =
    if args.last.is_a?(Hash)
      args.extract_options!.to_options!
    else
      {}
    end
#
  args.each{|arg| @nav = arg if arg.is_a?(Nav)}
  args.delete_if{|arg| arg.is_a?(Nav)}

  unless @nav
    @nav = Nav.new
    @nav.controller = Current.mock_controller
  end

#
  @content        = getopt!(:content){ args.shift || 'Slash' }
  @url            = getopt!(:url){ args.shift || {} }
  @pattern        = getopt!(:pattern){ args.shift || Link.default_active_pattern_for(@content) }
  @compute_active = getopt!(:active){ block || Link.default_active_block_for(@pattern) }
  @default        = getopt!(:default){ nil }

#
  @slug = Slug.for(@content, :join => '-')
  @already_computed_active = nil

  @config = Map.new
end

Instance Attribute Details

#activeObject

Returns the value of attribute active.



251
252
253
# File 'lib/rails_nav.rb', line 251

def active
  @active
end

#argsObject

Returns the value of attribute args.



244
245
246
# File 'lib/rails_nav.rb', line 244

def args
  @args
end

#compute_activeObject

Returns the value of attribute compute_active.



250
251
252
# File 'lib/rails_nav.rb', line 250

def compute_active
  @compute_active
end

#configObject

Returns the value of attribute config.



255
256
257
# File 'lib/rails_nav.rb', line 255

def config
  @config
end

#contentObject

Returns the value of attribute content.



247
248
249
# File 'lib/rails_nav.rb', line 247

def content
  @content
end

#controllerObject

Returns the value of attribute controller.



246
247
248
# File 'lib/rails_nav.rb', line 246

def controller
  @controller
end

#defaultObject

Returns the value of attribute default.



252
253
254
# File 'lib/rails_nav.rb', line 252

def default
  @default
end

Returns the value of attribute nav.



243
244
245
# File 'lib/rails_nav.rb', line 243

def nav
  @nav
end

#optionsObject

Returns the value of attribute options.



245
246
247
# File 'lib/rails_nav.rb', line 245

def options
  @options
end

#patternObject

Returns the value of attribute pattern.



249
250
251
# File 'lib/rails_nav.rb', line 249

def pattern
  @pattern
end

#slugObject

Returns the value of attribute slug.



254
255
256
# File 'lib/rails_nav.rb', line 254

def slug
  @slug
end

#weightObject

Returns the value of attribute weight.



253
254
255
# File 'lib/rails_nav.rb', line 253

def weight
  @weight
end

Class Method Details

.default_active_block_for(pattern) ⇒ Object



337
338
339
340
341
342
343
344
345
# File 'lib/rails_nav.rb', line 337

def Link.default_active_block_for(pattern)
  proc do |link|
    path_info = link.path_info
    depth = -1
    matched = false
    path_info.each{|path| depth += 1; break if(matched = path =~ pattern)}
    weight = matched ? depth : nil
  end
end

.default_active_pattern_for(content) ⇒ Object



332
333
334
335
# File 'lib/rails_nav.rb', line 332

def Link.default_active_pattern_for(content)
  path_info = Slug.for(content.to_s.split('/').first, :join => '[_-]*')
  %r/\b#{ path_info }\b/i
end

Instance Method Details

#compute_active!Object



292
293
294
295
296
297
298
299
300
301
# File 'lib/rails_nav.rb', line 292

def compute_active!
  @active = 
    if @compute_active.respond_to?(:call)
      @nav.evaluate(@compute_active, link = self)
    else
      !!@compute_active
    end
ensure
  @already_computed_active = true
end

#default?Boolean

Returns:

  • (Boolean)


307
308
309
# File 'lib/rails_nav.rb', line 307

def default?
  !!@default
end

#getopt!(key, &block) ⇒ Object



288
289
290
# File 'lib/rails_nav.rb', line 288

def getopt!(key, &block)
  @options.has_key?(key) ? @options.delete(key) : (block && block.call)
end

#inspectObject



328
329
330
# File 'lib/rails_nav.rb', line 328

def inspect
  super
end

#to_sObject



324
325
326
# File 'lib/rails_nav.rb', line 324

def to_s
  content.to_s
end

#urlObject Also known as: href



319
320
321
# File 'lib/rails_nav.rb', line 319

def url
  controller.send(:url_for, *@url)
end