Sha256: bbc78667560c0335ba6da8e2ce98441d3ef13310dda672bbe97299d0f1d902cb

Contents?: true

Size: 1.1 KB

Versions: 15

Compression:

Stored size: 1.1 KB

Contents

#
# A way to filter certain keys of data provided to the LazyHighCharts#series method and the LazyHighCharts#options
#
# Add methods or keys to the FILTER_MAP hash to have them applied to the options in a series
# In the FILTER_MAP, the hash keys are methods, and the values are arrays of the hash keys the filter should be
# applied to
#
# Be careful that it is OK to filter the hash keys you specify for every key of the options or series hash
#
module LazyHighCharts
  module OptionsKeyFilter
    def self.milliseconds value
      value * 1000
    end

    def self.date_to_js_code date
      "Date.UTC(#{date.year}, #{date.month - 1}, #{date.day})".js_code
    end

    def self.filter options
      new_options = options.map do |k, v|
        if v.is_a? ::Hash
          v = filter v
        else
          FILTER_MAP.each_pair do |method, matchers|
            v = method.call(v) if matchers.include?(k)
          end
        end
        [k, v]
      end
      Hash[new_options]
    end

    FILTER_MAP = {
        method(:milliseconds) => [:pointInterval],
        method(:date_to_js_code) => [:pointStart]
    }
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
lazy_high_charts-1.5.4 lib/lazy_high_charts/options_key_filter.rb
lazy_high_charts-1.5.2 lib/lazy_high_charts/options_key_filter.rb
lazy_high_charts-1.5.1 lib/lazy_high_charts/options_key_filter.rb
lazy_high_charts-1.5.0 lib/lazy_high_charts/options_key_filter.rb
lazy_high_charts-1.5.0.beta1 lib/lazy_high_charts/options_key_filter.rb
lazy_high_charts-1.4.3 lib/lazy_high_charts/options_key_filter.rb
lazy_high_charts-1.4.2 lib/lazy_high_charts/options_key_filter.rb
lazy_high_charts-1.4.2.beta1 lib/lazy_high_charts/options_key_filter.rb
lazy_high_charts-1.4.1 lib/lazy_high_charts/options_key_filter.rb
lazy_high_charts-1.4.0 lib/lazy_high_charts/options_key_filter.rb
lazy_high_charts-1.3.3 lib/lazy_high_charts/options_key_filter.rb
lazy_high_charts-1.3.3.beta.2 lib/lazy_high_charts/options_key_filter.rb
lazy_high_charts-1.3.2 lib/lazy_high_charts/options_key_filter.rb
lazy_high_charts-1.3.0.beta lib/lazy_high_charts/options_key_filter.rb
lazy_high_charts_tomgi-1.2.2 lib/lazy_high_charts/options_key_filter.rb