Sha256: c84e1a107d7df61f51b7215f5851cfee67ebc17834c7c6a41ea0b8c9f211b24a

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

class Scarpe
  module WebviewSpacing
    SPACING_DIRECTIONS = [:left, :right, :top, :bottom]

    def style
      styles = (super if defined?(super)) || {}

      extract_spacing_styles_for(:margin, styles, @margin)
      extract_spacing_styles_for(:padding, styles, @padding)

      styles
    end

    def extract_spacing_styles_for(attribute, styles, values)
      values ||= spacing_values_from_options(attribute)

      case values
      when Hash
        values.each do |direction, value|
          styles["#{attribute}-#{direction}"] = Dimensions.length(value)
        end
      when Array
        SPACING_DIRECTIONS.zip(values).to_h.compact.each do |direction, value|
          styles["#{attribute}-#{direction}"] = Dimensions.length(value)
        end
      else
        styles[attribute] = Dimensions.length(values)
      end

      styles.compact!
    end

    def spacing_values_from_options(attribute)
      SPACING_DIRECTIONS.map do |direction|
        @options.delete("#{attribute}_#{direction}".to_sym)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
scarpe-0.2.1 lib/scarpe/wv/spacing.rb
scarpe-0.2.0 lib/scarpe/wv/spacing.rb