Skip to content

Commit

Permalink
Require Ruby 2.3+ and rubocop driven cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
twalpole committed Oct 15, 2018
1 parent c7d087f commit 994e09c
Show file tree
Hide file tree
Showing 15 changed files with 343 additions and 244 deletions.
89 changes: 89 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
require:
- rubocop-rspec

AllCops:
DisabledByDefault: false
TargetRubyVersion: 2.3
Exclude:
- 'xpath.gemspec'

#################### Lint ################################

Metrics/LineLength:
Exclude:
- 'spec/**/*'
Max: 120
Enabled: false

Metrics/BlockLength:
Exclude:
- 'spec/**/*'

Metrics/AbcSize:
Enabled: false

Metrics/ClassLength:
CountComments: false
Enabled: false

Metrics/CyclomaticComplexity:
Enabled: false

Metrics/MethodLength:
CountComments: false
Enabled: false

Metrics/ModuleLength:
Enabled: false
CountComments: false

Metrics/PerceivedComplexity:
Enabled: false

Metrics/ParameterLists:
Enabled: false

Layout/EndAlignment:
EnforcedStyleAlignWith: variable

Style/Alias:
Enabled: false

Style/Documentation:
Exclude:
- 'spec/**/*'
Enabled: false

Style/EvenOdd:
Enabled: false

Lint/BooleanSymbol:
Enabled: false

Style/Attr:
Enabled: false

Layout/AccessModifierIndentation:
EnforcedStyle: outdent

RSpec/ExampleWording:
Enabled: false

RSpec/InstanceVariable:
Enabled: false

RSpec/ExampleLength:
Enabled: false

RSpec/DescribedClass:
Enabled: false

RSpec/DescribeClass:
Enabled: false

RSpec/FilePath:
Enabled: false

Style/MethodCallWithoutArgsParentheses:
Exclude:
- spec/xpath_spec.rb
5 changes: 0 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,10 @@ env:
- JAVA_OPTS=-Djava.security.egd=file:/dev/urandom

rvm:
- 2.2
- 2.3
- 2.4
- jruby-9.1.15.0
- 2.5

matrix:
allow_failures:
- rvm: 2.5

before_install:
- gem update bundler
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
source "https://rubygems.org"
# frozen_string_literal: true

source 'https://rubygems.org'

gemspec
12 changes: 7 additions & 5 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'rspec/core/rake_task'
require 'yard'

desc "Run all examples"
desc 'Run all examples'
RSpec::Core::RakeTask.new(:spec) do |t|
#t.rspec_path = 'bin/rspec'
# t.rspec_path = 'bin/rspec'
t.rspec_opts = %w[--color]
end

YARD::Rake::YardocTask.new do |t|
t.files = ['lib/**/*.rb', 'README.md']
#t.options = ['--any', '--extra', '--opts'] # optional
t.files = ['lib/**/*.rb', 'README.md']
# t.options = ['--any', '--extra', '--opts'] # optional
end

task :default => :spec
task default: :spec
2 changes: 2 additions & 0 deletions lib/xpath.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'nokogiri'

require 'xpath/dsl'
Expand Down
56 changes: 27 additions & 29 deletions lib/xpath/dsl.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module XPath
module DSL
def current
Expand Down Expand Up @@ -80,11 +82,11 @@ def position
# boolean
:boolean, :not, :true, :false, :lang,
# number
:number, :sum, :floor, :ceiling, :round,
]
:number, :sum, :floor, :ceiling, :round
].freeze

METHODS.each do |key|
name = key.to_s.gsub("_", "-").to_sym
name = key.to_s.tr('_', '-').to_sym
define_method key do |*args|
method(name, *args)
end
Expand All @@ -101,20 +103,20 @@ def qname
alias_method :n, :normalize_space

OPERATORS = [
[:equals, :"=", :==],
[:or, :or, :|],
[:and, :and, :&],
[:not_equals, :!=, :!=],
[:lte, :<=, :<=],
[:lt, :<, :<],
[:gte, :>=, :>=],
[:gt, :>, :>],
[:plus, :+],
[:minus, :-],
[:multiply, :*, :*],
[:divide, :div, :/],
[:mod, :mod, :%],
]
%i[equals = ==],
%i[or or |],
%i[and and &],
%i[not_equals != !=],
%i[lte <= <=],
%i[lt < <],
%i[gte >= >=],
%i[gt > >],
%i[plus +],
%i[minus -],
%i[multiply * *],
%i[divide div /],
%i[mod mod %]
].freeze

OPERATORS.each do |(name, operator, alias_name)|
define_method name do |rhs|
Expand All @@ -123,14 +125,14 @@ def qname
alias_method alias_name, name if alias_name
end

AXES = [
:ancestor, :ancestor_or_self, :attribute, :descendant_or_self,
:following, :following_sibling, :namespace, :parent, :preceding,
:preceding_sibling, :self,
]
AXES = %i[
ancestor ancestor_or_self attribute descendant_or_self
following following_sibling namespace parent preceding
preceding_sibling self
].freeze

AXES.each do |key|
name = key.to_s.gsub("_", "-").to_sym
name = key.to_s.tr('_', '-').to_sym
define_method key do |*element_names|
axis(name, *element_names)
end
Expand All @@ -143,7 +145,7 @@ def ends_with(suffix)
end

def contains_word(word)
function(:concat, " ", current.normalize_space, " ").contains(" #{word} ")
function(:concat, ' ', current.normalize_space, ' ').contains(" #{word} ")
end

UPPERCASE_LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞŸŽŠŒ'
Expand All @@ -158,11 +160,7 @@ def uppercase
end

def one_of(*expressions)
expressions.map do |e|
current.equals(e)
end.reduce do |a, b|
a.or(b)
end
expressions.map { |e| current.equals(e) }.reduce(:or)
end

def next_sibling(*expressions)
Expand Down
4 changes: 3 additions & 1 deletion lib/xpath/expression.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module XPath
class Expression
attr_accessor :expression, :arguments
Expand All @@ -12,7 +14,7 @@ def current
self
end

def to_xpath(type=nil)
def to_xpath(type = nil)
Renderer.render(self, type)
end
alias_method :to_s, :to_xpath
Expand Down
2 changes: 2 additions & 0 deletions lib/xpath/literal.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module XPath
class Literal
attr_reader :value
Expand Down
22 changes: 12 additions & 10 deletions lib/xpath/renderer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module XPath
class Renderer
def self.render(node, type)
Expand All @@ -15,19 +17,19 @@ def render(node)

def convert_argument(argument)
case argument
when Expression, Union then render(argument)
when Array then argument.map { |element| convert_argument(element) }
when String then string_literal(argument)
when Literal then argument.value
else argument.to_s
when Expression, Union then render(argument)
when Array then argument.map { |element| convert_argument(element) }
when String then string_literal(argument)
when Literal then argument.value
else argument.to_s
end
end

def string_literal(string)
if string.include?("'")
string = string.split("'", -1).map do |substr|
"'#{substr}'"
end.join(%q{,"'",})
end.join(%q(,"'",))
"concat(#{string})"
else
"'#{string}'"
Expand All @@ -51,7 +53,7 @@ def axis(current, name, element_names)
end

def anywhere(element_names)
with_element_conditions("//", element_names)
with_element_conditions('//', element_names)
end

def where(on, condition)
Expand All @@ -72,7 +74,7 @@ def binary_operator(name, left, right)

def is(one, two)
if @type == :exact
binary_operator("=", one, two)
binary_operator('=', one, two)
else
function(:contains, one, two)
end
Expand Down Expand Up @@ -102,7 +104,7 @@ def union(*expressions)
end

def function(name, *arguments)
"#{name}(#{arguments.join(", ")})"
"#{name}(#{arguments.join(', ')})"
end

private
Expand All @@ -111,7 +113,7 @@ def with_element_conditions(expression, element_names)
if element_names.length == 1
"#{expression}#{element_names.first}"
elsif element_names.length > 1
"#{expression}*[#{element_names.map { |e| "self::#{e}" }.join(" | ")}]"
"#{expression}*[#{element_names.map { |e| "self::#{e}" }.join(' | ')}]"
else
"#{expression}*"
end
Expand Down
6 changes: 4 additions & 2 deletions lib/xpath/union.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module XPath
class Union
include Enumerable
Expand All @@ -17,11 +19,11 @@ def each(&block)
arguments.each(&block)
end

def method_missing(*args)
def method_missing(*args) # rubocop:disable Style/MethodMissingSuper, Style/MissingRespondToMissing
XPath::Union.new(*arguments.map { |e| e.send(*args) })
end

def to_xpath(type=nil)
def to_xpath(type = nil)
Renderer.render(self, type)
end
alias_method :to_s, :to_xpath
Expand Down
2 changes: 2 additions & 0 deletions lib/xpath/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module XPath
VERSION = '3.1.0'
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'xpath'
require 'pry'

Expand Down
Loading

0 comments on commit 994e09c

Please sign in to comment.