Module: Howitzer::Web::ElementDsl

Includes:
CapybaraContextHolder, Helpers
Included in:
BaseSection, Page
Defined in:
lib/howitzer/web/element_dsl.rb

Overview

This module combines element dsl methods

Defined Under Namespace

Modules: ClassMethods, Helpers

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CapybaraContextHolder

#capybara_context

Class Method Details

.included(base) ⇒ Object

:nodoc:


23
24
25
# File 'lib/howitzer/web/element_dsl.rb', line 23

def self.included(base) # :nodoc:
  base.extend(ClassMethods)
end

Instance Method Details

#convert_arguments(args, options, block_args, block_options) ⇒ Object


27
28
29
30
31
32
33
34
35
# File 'lib/howitzer/web/element_dsl.rb', line 27

def convert_arguments(args, options, block_args, block_options)
  conv_args = args.map { |el| el.is_a?(Proc) ? proc_to_selector(el, block_args, block_options) : el }
  args_options = pop_options_from_array(conv_args)
  block_args_options = pop_options_from_array(block_args)
  conv_options = [args_options, options, block_args_options, block_options].map do |el|
    el.transform_keys(&:to_sym)
  end.reduce(&:merge).except(:lambda_args)
  [conv_args, conv_options]
end

#extract_lambda_args(block_args, block_options) ⇒ Object


52
53
54
# File 'lib/howitzer/web/element_dsl.rb', line 52

def extract_lambda_args(block_args, block_options)
  (block_args.first.is_a?(Hash) && block_args.first[:lambda_args]) || block_options[:lambda_args]
end

#pop_options_from_array(value) ⇒ Object


56
57
58
59
60
61
62
# File 'lib/howitzer/web/element_dsl.rb', line 56

def pop_options_from_array(value)
  if value.last.is_a?(Hash) && !value.last.key?(:lambda_args)
    value.pop
  else
    {}
  end
end

#proc_to_selector(proc, block_args, block_options) ⇒ Object


37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/howitzer/web/element_dsl.rb', line 37

def proc_to_selector(proc, block_args, block_options)
  lambda_args = extract_lambda_args(block_args, block_options)
  if lambda_args
    if lambda_args[:keyword_args].present?
      proc.call(*lambda_args[:args], **lambda_args[:keyword_args])
    else
      proc.call(*lambda_args[:args])
    end
  else
    puts "WARNING! Passing lambda arguments with element options is deprecated.\n" \
         "Please use 'lambda_args' method, for example: foo_element(lambda_args(title: 'Example'), wait: 10)"
    proc.call(*block_args.shift(proc.arity))
  end
end