Sha256: 4f32cfcc395a6c3ef6eea2539f704eb58234442b65d3c14ae7770dd1b18b4b13

Contents?: true

Size: 1.14 KB

Versions: 4

Compression:

Stored size: 1.14 KB

Contents

require 'rhet-butler/yaml-type'

module RhetButler
  class LayoutRule < YamlType
    register "layout-rule"

    def initialize
      @match = {}
      @layout_args = nil
    end
    attr_accessor :match, :layout_type, :layout_args

    def self.optional_config
      %w[match]
    end

    def self.required_config
      %w[layout]
    end

    def setup_defaults

    end

    def normalize_config(coder)
      case coder.type
      when :seq
        { 'match' => coder.seq[0], 'layout' => coder.seq[1] }
      when :map
        coder.map
      else
        raise "Tried to configure a layout rule with non-sequence: #{coder.inspect}"
      end
    end

    def configure
      value_from_config("match") do |value|
        if value == 'default'
          @match = {}
        else
          @match = value
        end
      end

      @layout_type, @layout_args = *@config_hash['layout']
    end

    def layout
      Arrangement[@layout_type].new(*(@layout_args||[]))
    end

    def match?(group)
      @match.all? do |key, value|
        target = group.metadata.fetch(key)
        value === target
      end
    rescue KeyError
      false
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rhet-butler-0.11.1 lib/rhet-butler/layout-rule.rb
rhet-butler-0.11.0 lib/rhet-butler/layout-rule.rb
rhet-butler-0.10.0 lib/rhet-butler/layout-rule.rb
rhet-butler-0.9.0 lib/rhet-butler/layout-rule.rb