Sha256: a490002365b458f2a057d0fe5fa32ae849e5898d63eb4a8749a3831bf25700cf

Contents?: true

Size: 952 Bytes

Versions: 5

Compression:

Stored size: 952 Bytes

Contents

require 'active_support/core_ext/hash/deep_merge'

module AxlsxStyler
  module Axlsx
    module Cell
      attr_accessor :raw_style

      def add_style(style)
        self.raw_style ||= {}
        add_to_raw_style(style)
        workbook.add_styled_cell self
      end

      private

      def workbook
        row.worksheet.workbook
      end

      def add_to_raw_style(style)
        # using deep_merge from active_support:
        # with regular Hash#merge adding borders fails miserably
        new_style = raw_style.deep_merge style
        if with_border?(raw_style) && with_border?(style)
          border_at = raw_style[:border][:edges] + style[:border][:edges]
          new_style[:border][:edges] = border_at.uniq
        elsif with_border?(style)
          new_style[:border] = style[:border]
        end
        self.raw_style = new_style
      end

      def with_border?(style)
        !style[:border].nil?
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
axlsx_styler-0.1.3 lib/axlsx_styler/axlsx_cell.rb
axlsx_styler-0.1.2 lib/axlsx_styler/axlsx_cell.rb
axlsx_styler-0.1.1 lib/axlsx_styler/axlsx_cell.rb
axlsx_styler-0.1.0 lib/axlsx_styler/axlsx_cell.rb
axlsx_styler-0.0.5 lib/axlsx_styler/axlsx_cell.rb