Sha256: ecdc2431c991a258d54fccf3620cdcddcd8aaecd010dd30c2338fcdfcc6ca28d

Contents?: true

Size: 1.13 KB

Versions: 6

Compression:

Stored size: 1.13 KB

Contents

require 'erb'
require 'mutex_m'

module MyChartkick
  class Bundle
    include MyChartkick

    MyChartkick.
      instance_methods.
      select do |m|
        m =~ /^my/
      end.
      each do |method_id|
        o_method = "#{method_id}_in_bundle"
        alias_method o_method, method_id
        define_method method_id do |data, opt|
          give_id! opt
          chart = send o_method, data, opt
          charts << chart
        end
      end

    attr_reader :charts

    def initialize
      @charts = []
    end

    def give_id! opt
      opt.merge!({id: ChartId.next}) unless opt[:id]
    end

    def sample cdn: false
      jslib = cdn ? MyChartkick::CDN : MyChartkick::Inline
      ERB.new(Template).result binding
    end

    Template = <<-EOHTML
<!DOCTYPE HTML>
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <%= jslib %>
  </head>
  <body>
    <%= charts.join %>
  </body>
</html>
EOHTML

  ChartId = Class.new do
    include Mutex_m
    def initialize
      @id = -1
      super
    end

    def next
      lock
      @id += 1
      unlock
      @id.to_s
    end
  end.new

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
my_chartkick-0.1.5 lib/my_chartkick/bundle.rb
my_chartkick-0.1.4 lib/my_chartkick/bundle.rb
my_chartkick-0.1.3 lib/my_chartkick/bundle.rb
my_chartkick-0.1.2 lib/my_chartkick/bundle.rb
my_chartkick-0.1.1 lib/my_chartkick/bundle.rb
my_chartkick-0.1.0 lib/my_chartkick/bundle.rb