Class: Welo::Link

Inherits:
Object
  • Object
show all
Includes:
CanBeLazy
Defined in:
lib/welo/core/link.rb

Instance Attribute Summary collapse

Attributes included from CanBeLazy

#lazy, #lazy_blocks

Instance Method Summary collapse

Methods included from CanBeLazy

#lazy?

Constructor Details

#initialize(from, to = nil, params = {}, &blk) ⇒ Link

Creates a link to the pointed resource to, it must respond to :path



37
38
39
40
41
42
43
44
# File 'lib/welo/core/link.rb', line 37

def initialize(from, to=nil, params={}, &blk)
  @from = from
  @to = to
  @lazy = params.has_key?(:lazy) ? params[:lazy] : true
  @label = params[:label] 
  @local = params[:local] 
  lazy_blocks[:to] = blk if block_given?
end

Instance Attribute Details

#fromObject

The pointing resource



24
25
26
# File 'lib/welo/core/link.rb', line 24

def from
  @from
end

#labelObject

The labeling for this link



30
31
32
# File 'lib/welo/core/link.rb', line 30

def label
  @label
end

#localObject

The locality for this link



33
34
35
# File 'lib/welo/core/link.rb', line 33

def local
  @local
end

#toObject

If the link is lazy and there is no value to @to, will evaluate the :to lazy_block and set to to this value. If the link is not lazy, will just return @to



27
28
29
# File 'lib/welo/core/link.rb', line 27

def to
  @to
end

Instance Method Details

#identifyObject

Gets the identifying scheme corresponding to this link. For now the identification scheme is written in the to resource. Later, we’ll try to be able to identify pointed resources with an identifying scheme local to the from resource.



66
67
68
69
70
71
72
# File 'lib/welo/core/link.rb', line 66

def identify
  identify = if nesting
               nesting.identifier_sym
             else
               :default
             end
end

#nestingObject

Gets the nesting of the from resource of this link.



58
59
60
# File 'lib/welo/core/link.rb', line 58

def nesting
  @nesting ||= from.nesting(label)
end

#to_absolute_pathObject

Returns an absolute version of the path. Meaning that we use the default identifying scheme.



76
77
78
# File 'lib/welo/core/link.rb', line 76

def to_absolute_path
  File.join('', to.path(:default).to_s)
end

#to_local_pathObject



88
89
90
# File 'lib/welo/core/link.rb', line 88

def to_local_path
  File.join('.', from.epithet_path_to(to, label).to_s)
end

#to_relative_pathObject

Returns a relative version of the path. We can use a different identification scheme of the pointed resource. Later, we’ll try to be able to identify pointed resources with a identifying scheme local to the from resource.



84
85
86
# File 'lib/welo/core/link.rb', line 84

def to_relative_path
  File.join('.', to.path(identify).to_s)
end

#to_sObject

Returns a string representation of this link (i.e., an path)



94
95
96
97
98
99
100
101
102
# File 'lib/welo/core/link.rb', line 94

def to_s
  if nesting
    to_relative_path
  elsif local
    to_local_path
  else
    to_absolute_path
  end
end