Class: Glark::Range

Inherits:
Object
  • Object
show all
Includes:
Comparable, Logue::Loggable
Defined in:
lib/glark/input/range.rb

Constant Summary collapse

PCT_RE =
Regexp.new '([\.\d]+)%'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from = nil, to = nil) ⇒ Range

Returns a new instance of Range.



18
19
20
21
# File 'lib/glark/input/range.rb', line 18

def initialize from = nil, to = nil
  @from = from
  @to = to
end

Instance Attribute Details

#fromObject

Returns the value of attribute from.



15
16
17
# File 'lib/glark/input/range.rb', line 15

def from
  @from
end

#toObject

Returns the value of attribute to.



16
17
18
# File 'lib/glark/input/range.rb', line 16

def to
  @to
end

Instance Method Details

#<=>(other) ⇒ Object



69
70
71
# File 'lib/glark/input/range.rb', line 69

def <=> other
  compare(from, other.from) || compare(to, other.to) || 0
end

#add_as_option(optdata) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/glark/input/range.rb', line 78

def add_as_option optdata
  optdata << {
    :tags => %w{ --after },
    :arg => [ :required, :regexp, %r{ (\d+%?) $ }x ],
    :set => Proc.new { |md| @from = md }
  }

  optdata << { 
    :tags => %w{ --before },
    :arg => [ :required, :regexp, %r{ (\d+%?) $ }x ],
    :set => Proc.new { |md| @to = md }
  }

  optdata << {
    :tags => %w{ -R --range },
    :arg => [ :required, :regexp, Regexp.new('(\d+%?,\d+%?)') ],
    :set => Proc.new { |md, opt, args| @from, @to = md.split(',') }
  }
end

#as_pct(val) ⇒ Object



38
39
40
# File 'lib/glark/input/range.rb', line 38

def as_pct val
  (md = PCT_RE.match(val)) && md[1]
end

#bound?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/glark/input/range.rb', line 23

def bound?
  @from.nil? && @to.nil?
end

#check_range!(from, to) ⇒ Object



58
59
60
61
62
# File 'lib/glark/input/range.rb', line 58

def check_range! from, to
  if from.to_f > to.to_f
    raise RangeError.new "error: range start (#{@from}) follows range end (#{@to})"
  end
end

#clearObject



73
74
75
76
# File 'lib/glark/input/range.rb', line 73

def clear
  @from = nil
  @to = nil
end

#compare(x, y) ⇒ Object

there is no nil <=> nil in Ruby



65
66
67
# File 'lib/glark/input/range.rb', line 65

def compare x, y
  x.nil? && y.nil? ? nil : (x <=> y).nonzero?
end

#to_line(var, linecount) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/glark/input/range.rb', line 27

def to_line var, linecount
  return nil unless var

  if pct = as_pct(var) 
    count = linecount
    count * pct.to_f / 100
  else
    var.to_f
  end
end

#validate!Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/glark/input/range.rb', line 42

def validate!
  return true if @from.nil? || @to.nil?

  frompct, topct = [ @from, @to ].collect { |val| as_pct val }

  # both or neither are percentages:
  return true if frompct.nil? != topct.nil?
  
  if frompct
    check_range! frompct, topct
  else
    check_range! @from, @to
  end
  true
end