Sha256: ba51e3d3c662c36e900584a3a655a9f066b8ec86f7c03b610f51a3b9209ad294

Contents?: true

Size: 1.06 KB

Versions: 6

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

class Scarpe
  module GlimmerLibUISpacing
    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

6 entries across 6 versions & 2 rubygems

Version Path
scarpe-0.4.0 spikes/glibui/spacing.rb
scarpe-0.3.0 spikes/glibui/spacing.rb
scarpe-0.2.2 spikes/glibui/spacing.rb
lacci-0.2.1 lib/scarpe/glibui/spacing.rb
scarpe-0.2.1 lib/scarpe/glibui/spacing.rb
scarpe-0.2.0 lib/scarpe/glibui/spacing.rb