Module: Bike::Path

Defined in:
lib/_path.rb

Overview

Author

Akira FUNAI

Copyright

Copyright © 2009 Akira FUNAI

Class Method Summary collapse

Class Method Details

._basename(path) ⇒ Object

returns nil for ‘/foo/bar/’



81
82
83
# File 'lib/_path.rb', line 81

def _basename(path) # returns nil for '/foo/bar/'
  path[%r{[^/]+$}]
end

._dirname(path) ⇒ Object

returns ‘/foo/bar/’ for ‘/foo/bar/’



77
78
79
# File 'lib/_path.rb', line 77

def _dirname(path) # returns '/foo/bar/' for '/foo/bar/'
  path[%r{^.*/}] || ''
end

.action_of(path) ⇒ Object



44
45
46
47
# File 'lib/_path.rb', line 44

def action_of(path)
  a = _basename(path).to_s[/^[a-z]+/]
  a.intern if a && a != 'index'
end

.base_of(path) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/_path.rb', line 21

def base_of(path)
  base = Bike::Set::Static::Folder.root.item steps_of(path)
  if base.is_a? Bike::Set::Static::Folder
    base.item('main') || base.find {|item| item.is_a? Bike::Set::Dynamic } || base
  else
    base
  end
end

.conds_of(path) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/_path.rb', line 30

def conds_of(path)
  dir   = _dirname path.gsub(Bike::REX::PATH_ID, '')
  conds = $& ? {:id => sprintf('%.8d_%.4d', $1, $2)} : {}

  dir.split('/').inject(conds) {|conds, step_or_cond|
    if step_or_cond =~ Bike::REX::COND
      conds[$1.intern] = $2
    elsif step_or_cond =~ Bike::REX::COND_D
      conds[:d] = $&
    end
    conds
  }
end

.path_of(conds) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/_path.rb', line 54

def path_of(conds)
  conds = {} unless conds.is_a? ::Hash
  (
    (conds.keys - [:order, :p, :id]) |
    ([:order, :p, :id] & conds.keys)
  ).collect {|cid|
    if cid == :id
      ids = Array(conds[:id]).collect {|id|
        (id =~ /_(#{Bike::REX::ID_SHORT})$/) ? $1 : (id if Bike::REX::ID)
      }.compact
      if (ids.size == 1) && (ids.first =~ Bike::REX::ID)
        '%s/%d/' % [$1, $2.to_i]
      elsif ids.size > 0
        "id=#{ids.join ','}/"
      end
    elsif cid == :d
      conds[:id] ? '' : "#{conds[:d]}/"
    else
      "#{cid}=#{Array(conds[cid]).join ','}/"
    end
  }.join.sub(%r{/p=1/$}, '/')
end

.steps_of(path) ⇒ Object



14
15
16
17
18
19
# File 'lib/_path.rb', line 14

def steps_of(path)
  _dirname(path).gsub(Bike::REX::PATH_ID, '').split('/').select {|step_or_cond|
    step_or_cond != '' &&
    step_or_cond !~ Regexp.union(Bike::REX::COND, Bike::REX::COND_D, Bike::REX::TID)
  }
end

.sub_action_of(path) ⇒ Object



49
50
51
52
# File 'lib/_path.rb', line 49

def sub_action_of(path)
  a = _basename(path).to_s[/_([a-z]+)/, 1]
  a.intern if a
end

.tid_of(path) ⇒ Object



10
11
12
# File 'lib/_path.rb', line 10

def tid_of(path)
  path[Bike::REX::TID]
end