Sha256: 54d1e192bf97dffe0bc0a613fa64f1c85d6e518c47fde2926cff4729d3c34c4a

Contents?: true

Size: 1.54 KB

Versions: 15

Compression:

Stored size: 1.54 KB

Contents

class FnordMetric::Widget

  attr_accessor :gauges, :tick

  def initialize(opts={})
    @opts = opts

    unless opts.has_key?(:title)
      error! "widget can't be initialized without a title"
    end

    add_gauges(opts.delete(:gauges))
  end

  def title
    @opts[:title]
  end

  def token
    title.to_s.gsub(/[\W]/, '').downcase
  end

  def add_gauges(gauges)
    if gauges.blank? && has_tick?
      error! "initializing a widget without gauges is void"
    else
      @gauges = gauges
    end
    
    if (ticks = gauges.map{ |g| g.tick }).uniq.length == 1
      @tick = ticks.first
    elsif !!self.try(:has_tick?)
      error! "you can't add gauges with different ticks to the same widget"
    end
  end

  def error!(msg)
    FnordMetric.error!(msg)
  end

  def range
    ensure_has_tick!
    #@opts[:range] || default_range # FIXME: allow custom ranges, but assure that the range-start is 'on a tick'
    default_range
  end

  def ticks
    ensure_has_tick!
    range.step(@tick)
  end

  def default_range(now=Time.now)
    ensure_has_tick!
    te = gauges.first.tick_at(now.to_i)
    te -= @tick unless include_current?
    rs = (@opts[:ticks] || (@tick == 1.hour.to_i ? 24 : 30)).to_i
    (te-(@tick*rs)..te)
  end

  def include_current?
    !(@opts[:include_current] == false)
  end
    
  def data
    { 
      :title => @opts[:title], 
      :width => @opts[:width] || 100,
      :klass => self.class.name.split("::").last 
    }
  end

  def render
    data
  end

  def ensure_has_tick!
    error! "widget does not have_tick" unless has_tick?
  end

end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
fnordmetric-0.6.6 lib/fnordmetric/widget.rb
fnordmetric-0.6.5 lib/fnordmetric/widget.rb
fnordmetric-0.6.4 lib/fnordmetric/widget.rb
fnordmetric-0.6.3 lib/fnordmetric/widget.rb
fnordmetric-0.6.2 lib/fnordmetric/widget.rb
fnordmetric-0.6.1 lib/fnordmetric/widget.rb
fnordmetric-0.6.0 lib/fnordmetric/widget.rb
fnordmetric-0.5.9 lib/fnordmetric/widget.rb
fnordmetric-0.5.8 lib/fnordmetric/widget.rb
fnordmetric-0.5.7 lib/fnordmetric/widget.rb
fnordmetric-0.5.6 lib/fnordmetric/widget.rb
fnordmetric-0.5.5 lib/fnordmetric/widget.rb
fnordmetric-0.5.4 lib/fnordmetric/widget.rb
fnordmetric-0.5.3 lib/fnordmetric/widget.rb
fnordmetric-0.5.2 lib/fnordmetric/widget.rb