Sha256: aaa8e22dd54e155ac7f4b863e75adfcb80edfcfa0a044790ced59342d3d04b75

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

module Mache
  # Provides the mache DSL for nodes.
  module DSL
    def self.included(base)
      base.extend ClassMethods
    end

    module ClassMethods #:nodoc:
      def automation(*ids)
        ids.map { |id| %([data-automation="#{id}"]) }.join(" ")
      end

      def element(name, selector)
        define_method(name.to_s) do
          @node.find(selector)
        end
        define_helper_methods(name, selector)
      end

      def elements(name, selector)
        define_method(name.to_s) do
          @node.all(selector, minimum: 1)
        end
        define_helper_methods(name, selector)
      end

      def component(name, type, selector)
        define_method(name.to_s) do
          type.new(@node.find(selector))
        end
        define_helper_methods(name, selector)
      end

      def components(name, type, selector)
        define_method(name.to_s) do
          @node.all(selector, minimum: 1).map { |element| type.new(element) }
        end
        define_helper_methods(name, selector)
      end

      private

      def define_helper_methods(name, selector)
        define_existence_predicates(name, selector)
      end

      def define_existence_predicates(name, selector)
        define_method("has_#{name}?") do
          @node.has_selector?(selector)
        end

        define_method("has_no_#{name}?") do
          @node.has_no_selector?(selector)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mache-1.0.0 lib/mache/dsl.rb