Sha256: 12ad3fed93f114c1e135071c1f4113d4bdb94ee25eecddaf8147636c0a94febf

Contents?: true

Size: 1.38 KB

Versions: 7

Compression:

Stored size: 1.38 KB

Contents

module Tabulatr
  class ParamsBuilder
    ALLOWED_PARAMS    = [:header, :filter, :sortable, :data_html,
                         :header_html, :filter_sql, :sort_sql, :sql, :width,
                         :align, :wrap, :format, :filter_label, :name, :classes,
                         :default_filter
                        ]
    DEPRECATED_PARAMS = []

    attr_accessor *ALLOWED_PARAMS

    def initialize(params = {})
      apply_params(params)
    end

    def update(params = {})
      apply_params(params)
    end

    private

    def style_options
      self.data_html ||= {}
      self.header_html ||= {}
      self.data_html[:style] ||= ''
      self.header_html[:style] ||= ''
      apply_style_option('text-align', align)
      apply_style_option('width', width)
      apply_style_option('white-space', wrap)
    end

    def apply_params(params)
      params.each do |k, v|
        if DEPRECATED_PARAMS.include?(k.to_sym)
          self.public_send(k)
        elsif ALLOWED_PARAMS.exclude?(k.to_sym)
          raise ArgumentError, "`#{k}` is not allowed as a parameter"
        else
          self.public_send("#{k}=", v)
        end
      end
      style_options
    end

    def apply_style_option(attribute, value)
      if value.present?
        self.header_html[:style].concat("#{attribute}: #{value};")
        self.data_html[:style].concat("#{attribute}: #{value};")
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
tabulatr2-0.10.4 lib/tabulatr/params_builder.rb
tabulatr2-0.10.3 lib/tabulatr/params_builder.rb
tabulatr2-0.10.2 lib/tabulatr/params_builder.rb
tabulatr2-0.10.1 lib/tabulatr/params_builder.rb
tabulatr2-0.10.0 lib/tabulatr/params_builder.rb
tabulatr2-0.9.48 lib/tabulatr/params_builder.rb
tabulatr2-0.9.47 lib/tabulatr/params_builder.rb