Sha256: eddcca0467efb7bb9042804508a24f5c9f9c9eaf79171954631f827714ea9e00

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

class BaseGenerator

  # generate assets for chart ( .js and .css )
  # and put them into rails assets path
  def add_assets( f_name, code )
    d_path = create_dir( f_name )
    f_path = file_path( d_path, f_name )
    make_file( f_path, code )
  end

  # get chart's js-code form template
  def js_code( element, data )
    erb = ERB.new( temp_js )
    erb.result( binding )
  end

  # get chart's css-code form template
  def css_code( element )
    erb = ERB.new( temp_css )
    erb.result( binding )      
  end

  private 

  def file_type( f_name )
    File.extname(f_name) == '.css' ? 'stylesheets' : 'javascripts'
  end

  def create_dir( f_name )
    type   = file_type(f_name)
    d_path = "app/assets/#{type}/d3js-charts"
    make_dir( d_path )
    d_path
  end

  def file_path( f_path, f_name )
    File.join f_path, f_name
  end

  def make_dir( d_path )
    Dir.mkdir( d_path ) unless File.exists?( d_path )
  end

  def make_file( f_path, code )
    File.open( f_path,'w' ) do |f| 
      f.write( code ) 
    end #unless File.exists?( f_path )
  end

  def temp_js
    File.read temp_path('js')
  end

  def temp_css
    File.read temp_path('css')
  end

  def gem_root
    Gem::Specification.find_by_name('d3js-charts').gem_dir
  end

  def temp_path( type )
    File.join temp_dir(type), temp_name(type)
  end

  def temp_dir( type ) 
    File.join gem_root, 'lib/chart_templates', type 
  end

  def temp_name( type )
    tn = self.class.name.sub('Generator', '')
    tn.gsub!(/([^\^])([A-Z])/,'\1_\2')
    "#{tn.downcase}.#{type}.erb"
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
d3js-charts-1.1.0 lib/chart_generators/base_generator.rb