Module: Cda::RSpec::ValidationRules
- Defined in:
- lib/cda/rspec/validation_rules.rb
Instance Method Summary collapse
- #_element ⇒ Object
- #find_by_xpath(*args) ⇒ Object
- #shall(matcher, *args) {|result| ... } ⇒ Object
- #shall_contain_at_least_one(*xpath, &block) ⇒ Object
- #shall_contain_exactly_one(*xpath, &block) ⇒ Object
- #shall_not_contain(*xpath) ⇒ Object
- #validate_rules_for(element) ⇒ Object
- #xpath_condition(condition) ⇒ Object
- #xpath_eq(name, value) ⇒ Object
Instance Method Details
#_element ⇒ Object
59 60 61 |
# File 'lib/cda/rspec/validation_rules.rb', line 59 def _element @_element || document.root end |
#find_by_xpath(*args) ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/cda/rspec/validation_rules.rb', line 27 def find_by_xpath(*args) xpath = args.first if xpath.is_a?(Hash) xpath, condition = xpath.keys.first, xpath_condition(xpath.values.first) else condition = xpath_condition(args[1]) end xpath = "#{xpath}[#{condition}]" if condition _element.xpath(xpath.to_s) end |
#shall(matcher, *args) {|result| ... } ⇒ Object
20 21 22 23 24 25 |
# File 'lib/cda/rspec/validation_rules.rb', line 20 def shall(matcher, *args) opts = args. result = find_by_xpath(*args) result.should matcher, opts[:message] yield result if block_given? end |
#shall_contain_at_least_one(*xpath, &block) ⇒ Object
10 11 12 13 14 |
# File 'lib/cda/rspec/validation_rules.rb', line 10 def shall_contain_at_least_one(*xpath, &block) shall have_at_least(1).item, *(xpath + [{message: "Shall contain at least one #{xpath}"}]) do |result| validate_rules_for result.first, &block if block_given? end end |
#shall_contain_exactly_one(*xpath, &block) ⇒ Object
4 5 6 7 8 |
# File 'lib/cda/rspec/validation_rules.rb', line 4 def shall_contain_exactly_one(*xpath, &block) shall have(1).item, *(xpath + [{message: "Shall contain exactly one #{xpath}"}]) do |result| validate_rules_for result.first, &block if block_given? end end |
#shall_not_contain(*xpath) ⇒ Object
16 17 18 |
# File 'lib/cda/rspec/validation_rules.rb', line 16 def shall_not_contain(*xpath) shall be_empty, *(xpath + [{message: "Shall not contain #{xpath}"}]) end |
#validate_rules_for(element) ⇒ Object
63 64 65 66 67 68 |
# File 'lib/cda/rspec/validation_rules.rb', line 63 def validate_rules_for(element) saved_element, @_element = @_element, element yield ensure @_element = saved_element end |
#xpath_condition(condition) ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/cda/rspec/validation_rules.rb', line 38 def xpath_condition(condition) case condition when Hash condition.map { |k, v| xpath_eq(k, v) }.join(' and ') when String %(text()="#{condition}") else nil end end |
#xpath_eq(name, value) ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/cda/rspec/validation_rules.rb', line 49 def xpath_eq(name, value) if name.to_s.include?(':') _, ns_less_name = name.to_s.split(':') %(@#{ns_less_name}="#{value}") else %(@#{name}="#{value}") end end |