Class: Glark::Range
- Inherits:
-
Object
- Object
- Glark::Range
- Includes:
- Comparable, Loggable
- Defined in:
- lib/glark/input/range.rb
Constant Summary collapse
- PCT_RE =
Regexp.new '([\.\d]+)%'
Instance Attribute Summary collapse
-
#from ⇒ Object
Returns the value of attribute from.
-
#to ⇒ Object
Returns the value of attribute to.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #add_as_option(optdata) ⇒ Object
- #as_pct(val) ⇒ Object
- #bound? ⇒ Boolean
- #check_range!(from, to) ⇒ Object
- #clear ⇒ Object
-
#compare(x, y) ⇒ Object
there is no nil <=> nil in Ruby.
-
#initialize(from = nil, to = nil) ⇒ Range
constructor
A new instance of Range.
- #to_line(var, linecount) ⇒ Object
- #validate! ⇒ Object
Constructor Details
#initialize(from = nil, to = nil) ⇒ Range
Returns a new instance of Range.
16 17 18 19 |
# File 'lib/glark/input/range.rb', line 16 def initialize from = nil, to = nil @from = from @to = to end |
Instance Attribute Details
#from ⇒ Object
Returns the value of attribute from.
13 14 15 |
# File 'lib/glark/input/range.rb', line 13 def from @from end |
#to ⇒ Object
Returns the value of attribute to.
14 15 16 |
# File 'lib/glark/input/range.rb', line 14 def to @to end |
Instance Method Details
#<=>(other) ⇒ Object
67 68 69 |
# File 'lib/glark/input/range.rb', line 67 def <=> other compare(from, other.from) || compare(to, other.to) || 0 end |
#add_as_option(optdata) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/glark/input/range.rb', line 76 def add_as_option optdata optdata << { :tags => %w{ --after }, :arg => [ :required, :regexp, %r{ (\d+%?) $ }x ], :set => Proc.new { |md| @from = md[1] } } optdata << { :tags => %w{ --before }, :arg => [ :required, :regexp, %r{ (\d+%?) $ }x ], :set => Proc.new { |md| @to = md[1] } } optdata << { :tags => %w{ -R --range }, :arg => [ :required, :regexp, Regexp.new('(\d+%?),(\d+%?)') ], :set => Proc.new do |md, opt, args| if md && md[1] && md[2] @from = md[1] @to = md[2] else @from = args.shift @to = args.shift end end } end |
#as_pct(val) ⇒ Object
36 37 38 |
# File 'lib/glark/input/range.rb', line 36 def as_pct val (md = PCT_RE.match(val)) && md[1] end |
#bound? ⇒ Boolean
21 22 23 |
# File 'lib/glark/input/range.rb', line 21 def bound? @from.nil? && @to.nil? end |
#check_range!(from, to) ⇒ Object
56 57 58 59 60 |
# File 'lib/glark/input/range.rb', line 56 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 |
#clear ⇒ Object
71 72 73 74 |
# File 'lib/glark/input/range.rb', line 71 def clear @from = nil @to = nil end |
#compare(x, y) ⇒ Object
there is no nil <=> nil in Ruby
63 64 65 |
# File 'lib/glark/input/range.rb', line 63 def compare x, y x.nil? && y.nil? ? nil : (x <=> y).nonzero? end |
#to_line(var, linecount) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/glark/input/range.rb', line 25 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
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/glark/input/range.rb', line 40 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 |