Sha256: 9ccb57e532f9ed140e2140aaef918d78493ab00a047ef95ce6ea7ba421aa75d7

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

require 'active_support/core_ext/hash/deep_merge'

module AxlsxStyler
  module Axlsx
    module Workbook
      attr_accessor :styled_cells

      def add_styled_cell(cell)
        self.styled_cells ||= []
        self.styled_cells << cell
      end

      def apply_styles
        return unless styled_cells
        styled_cells.each do |cell|
          cell.style = styles.add_style(cell.raw_style)
        end
      end
    end

    module Cell
      attr_accessor :raw_style

      def workbook
        row.worksheet.workbook
      end

      def add_style(style)
        self.raw_style ||= {}
        add_to_raw_style(style)
        workbook.add_styled_cell self
      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
        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

3 entries across 3 versions & 1 rubygems

Version Path
axlsx_styler-0.0.3 lib/axlsx_styler/axlsx_extensions.rb
axlsx_styler-0.0.2 lib/axlsx_styler/axlsx_extensions.rb
axlsx_styler-0.0.1 lib/axlsx_styler/axlsx_extensions.rb