Class: NSWTopo::Layer

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

Constant Summary collapse

TYPES =
Set[Vegetation, Import, ArcGISRaster, Feature, Contour, Spot, Overlay, Relief, Grid, Declination, Control, Labels]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, map, params) ⇒ Layer

Returns a new instance of Layer.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/nswtopo/layer.rb', line 20

def initialize(name, map, params)
  @type = begin
    NSWTopo.const_get params["type"]
  rescue NameError, TypeError
  end

  raise "unrecognised layer type: %s" % params["type"].inspect unless TYPES === @type
  extend @type

  @params = @type.const_defined?(:DEFAULTS) ? @type.const_get(:DEFAULTS).transform_keys(&:to_s).merge(params) : params
  @name, @map, @source, @path, @resolution = Layer.sanitise(name), map, @params.delete("source"), @params.delete("path"), @params.delete("resolution")

  @type.const_get(:CREATE).map(&:to_s).each do |attr|
    instance_variable_set ?@ + attr.tr_s(?-, ?_), @params.delete(attr)
  end if @type.const_defined?(:CREATE)
end

Instance Attribute Details

#nameObject (readonly) Also known as: to_s

Returns the value of attribute name.



37
38
39
# File 'lib/nswtopo/layer.rb', line 37

def name
  @name
end

#paramsObject (readonly)

Returns the value of attribute params.



37
38
39
# File 'lib/nswtopo/layer.rb', line 37

def params
  @params
end

Class Method Details

.sanitise(name) ⇒ Object



74
75
76
# File 'lib/nswtopo/layer.rb', line 74

def self.sanitise(name)
  name&.tr_s '^_a-zA-Z0-9*\-', ?.
end

Instance Method Details

#<=>(other) ⇒ Object



57
58
59
# File 'lib/nswtopo/layer.rb', line 57

def <=>(other)
  [self, other].map(&:level).inject(&:<=>)
end

#==(other) ⇒ Object



61
62
63
# File 'lib/nswtopo/layer.rb', line 61

def ==(other)
  Layer === other && self.name == other.name
end

#levelObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/nswtopo/layer.rb', line 40

def level
  case
  when Vegetation   == @type then 0
  when Import       == @type then 1
  when ArcGISRaster == @type then 1
  when Feature      == @type then 2
  when Contour      == @type then 2
  when Spot         == @type then 2
  when Overlay      == @type then 3
  when Relief       == @type then 4
  when Grid         == @type then 5
  when Declination  == @type then 6
  when Control      == @type then 7
  when Labels       == @type then 99
  end
end

#pairObject



70
71
72
# File 'lib/nswtopo/layer.rb', line 70

def pair
  return name, params
end

#uptodate?Boolean

Returns:

  • (Boolean)


65
66
67
68
# File 'lib/nswtopo/layer.rb', line 65

def uptodate?
  mtimes = [@source&.mtime, @map.mtime(filename)]
  mtimes.all? && mtimes.inject(&:<)
end