hexpress 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/lib/hexpress.rb +97 -0
  3. data/lib/hexpress/character.rb +26 -0
  4. data/lib/hexpress/main.rb +5 -0
  5. data/lib/hexpress/modifier.rb +26 -0
  6. data/lib/hexpress/modifier/many.rb +25 -0
  7. data/lib/hexpress/modifier/one.rb +19 -0
  8. data/lib/hexpress/nested.rb +38 -0
  9. data/lib/hexpress/nested/find.rb +17 -0
  10. data/lib/hexpress/nested/matching.rb +31 -0
  11. data/lib/hexpress/value.rb +28 -0
  12. data/lib/hexpress/value/ending.rb +22 -0
  13. data/lib/hexpress/value/except.rb +22 -0
  14. data/lib/hexpress/value/starting.rb +22 -0
  15. data/lib/hexpress/value/with.rb +21 -0
  16. data/lib/hexpress/values.rb +20 -0
  17. data/lib/hexpress/values/either.rb +24 -0
  18. data/lib/hexpress/values/range.rb +28 -0
  19. data/lib/hexpress/verbal_expressions.rb +47 -0
  20. data/lib/hexpress/version.rb +3 -0
  21. data/lib/hexpress/web.rb +45 -0
  22. data/lib/hexpress/wrapped.rb +9 -0
  23. data/spec/lib/hexpress/main_spec.rb +14 -0
  24. data/spec/lib/hexpress/modifier/many_spec.rb +19 -0
  25. data/spec/lib/hexpress/modifier/one_spec.rb +21 -0
  26. data/spec/lib/hexpress/modifier_spec.rb +49 -0
  27. data/spec/lib/hexpress/nested/find_spec.rb +13 -0
  28. data/spec/lib/hexpress/nested/matching_spec.rb +15 -0
  29. data/spec/lib/hexpress/nested_spec.rb +42 -0
  30. data/spec/lib/hexpress/value/ending_spec.rb +13 -0
  31. data/spec/lib/hexpress/value/except_spec.rb +9 -0
  32. data/spec/lib/hexpress/value/starting_spec.rb +13 -0
  33. data/spec/lib/hexpress/value/with_spec.rb +9 -0
  34. data/spec/lib/hexpress/value_spec.rb +25 -0
  35. data/spec/lib/hexpress/values/either_spec.rb +9 -0
  36. data/spec/lib/hexpress/values/range_spec.rb +33 -0
  37. data/spec/lib/hexpress/values_spec.rb +22 -0
  38. data/spec/lib/hexpress/verbal_expressions_standard.rb +112 -0
  39. data/spec/lib/hexpress/version_spec.rb +7 -0
  40. data/spec/lib/hexpress_spec.rb +73 -0
  41. data/spec/spec_helper.rb +9 -0
  42. metadata +216 -0
@@ -0,0 +1,24 @@
1
+ class Hexpress
2
+ def either(*values)
3
+ add_value(Values::Either, values)
4
+ end
5
+ alias_method :and, :either
6
+ alias_method :any_of, :either
7
+
8
+ module Values
9
+ class Either
10
+ include Values
11
+ include Wrapped
12
+
13
+ def initialize(*values)
14
+ @values = values
15
+ @delimiter = "|"
16
+ @open, @close = "(?:", ")"
17
+ end
18
+
19
+ def to_s
20
+ "#{open}#{values.join(delimiter)}#{close}"
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,28 @@
1
+ class Hexpress
2
+ def letter
3
+ lower and upper
4
+ end
5
+
6
+ def lower(value = "a".."z")
7
+ add_value(Values::Range, value)
8
+ end
9
+
10
+ def upper(value = "A".."Z")
11
+ add_value(Values::Range, value)
12
+ end
13
+
14
+ def number(value = 0..9)
15
+ add_value(Values::Range, value)
16
+ end
17
+
18
+ module Values
19
+ class Range
20
+ include Values
21
+
22
+ def initialize(range)
23
+ @values = [range.first, range.last]
24
+ @delimiter = "-"
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,47 @@
1
+ require "hexpress"
2
+ require "pry"
3
+
4
+ class Hexpress
5
+ alias_method :startOfLine, :starting
6
+ alias_method :then, :with
7
+ alias_method :lineBreak, :line
8
+ alias_method :br, :line
9
+ alias_method :anyOf, :either
10
+
11
+ def anythingBut(value)
12
+ many(matching { without(value) }.to_s, 0)
13
+ end
14
+
15
+ def somethingBut(value)
16
+ something and without(value)
17
+ end
18
+
19
+
20
+ module Values
21
+ class Either
22
+ def open
23
+ "(?:["
24
+ end
25
+
26
+ def close
27
+ "])"
28
+ end
29
+ end
30
+ end
31
+
32
+ module Modifier
33
+ class Many
34
+ def to_s
35
+ "#{open}#{value}#{operator}#{close}"
36
+ end
37
+ end
38
+ end
39
+
40
+ module Nested
41
+ class Find
42
+ def open
43
+ "(?:"
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,3 @@
1
+ class Hexpress
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,45 @@
1
+ class Hexpress
2
+ module Web
3
+ def http
4
+ start("http").maybe("s").protocol
5
+ end
6
+
7
+ def http_only
8
+ start("http").protocol
9
+ end
10
+
11
+ def https_only
12
+ start("https").protocol
13
+ end
14
+
15
+ def protocol
16
+ with("://")
17
+ end
18
+
19
+ def domain(name)
20
+ maybe { words.with(".") }.find(name)
21
+ end
22
+
23
+ def tld(name)
24
+ has(".").with(name).maybe("/")
25
+ end
26
+
27
+ def ftp
28
+ start("ftp").maybe("s").protocol
29
+ end
30
+
31
+ def ftp_only
32
+ start("ftp").protocol
33
+ end
34
+
35
+ def ftps_only
36
+ start("ftps").protocol
37
+ end
38
+
39
+ def path
40
+ has("/").anything.maybe.maybe("?")
41
+ end
42
+ end
43
+
44
+ include Web
45
+ end
@@ -0,0 +1,9 @@
1
+ class Hexpress
2
+ module Wrapped
3
+ attr_reader :open, :close
4
+
5
+ def wrapping(content)
6
+ "#{open}#{content}#{close}"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ require "spec_helper"
2
+ require "hexpress/main"
3
+
4
+ describe Hexpress do
5
+ describe "hexp" do
6
+ it "allow for a chain of methods" do
7
+ expect(hexp.find("foo").to_r).to eq(/(foo)/)
8
+ end
9
+
10
+ it "allow for a block of methods" do
11
+ expect(hexp { find("foo") }.to_r).to eq(/(foo)/)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,19 @@
1
+ require "spec_helper"
2
+
3
+ describe Hexpress::Modifier::Many do
4
+ describe "#operator" do
5
+ class ExampleWithMany < Hexpress::Modifier::Many
6
+ def initialize(minimum)
7
+ @minimum = minimum
8
+ end
9
+ end
10
+
11
+ it "returns * if minimum is 0" do
12
+ expect(ExampleWithMany.new(0).operator).to eq("*")
13
+ end
14
+
15
+ it "returns + if minimum is 1" do
16
+ expect(ExampleWithMany.new(1).operator).to eq("+")
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,21 @@
1
+ require "spec_helper"
2
+
3
+ describe Hexpress::Modifier::One do
4
+ describe "#maybe" do
5
+ it 'returns `(?:\w+)?`' do
6
+ regex = Hexpress.new.maybe { words }.to_r
7
+ expect(regex).to eq(/(?:(?:\w)+)?/)
8
+ end
9
+
10
+ it 'returns only ? if no value given`' do
11
+ regex = Hexpress.new.with("foo").maybe.to_r
12
+ expect(regex).to eq(/foo?/)
13
+ end
14
+ end
15
+
16
+ describe "#operator" do
17
+ it "returns the ? operator" do
18
+ expect(described_class.new("foo").operator).to eq("?")
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,49 @@
1
+ require "spec_helper"
2
+
3
+ describe Hexpress::Modifier do
4
+ class ExampleModifier
5
+ include Hexpress::Modifier
6
+ end
7
+
8
+ describe "#open" do
9
+ it "returns the open operator" do
10
+ expect(ExampleModifier.new.open).to eq("(?:")
11
+ end
12
+ end
13
+
14
+ describe "#close" do
15
+ it "returns the close operator" do
16
+ expect(ExampleModifier.new.close).to eq(")")
17
+ end
18
+ end
19
+
20
+ describe "#to_s" do
21
+ class ExampleModifierWithValue
22
+ include Hexpress::Modifier
23
+ attr_reader :value
24
+
25
+ def initialize
26
+ @value = "foo"
27
+ end
28
+
29
+ def operator
30
+ "%"
31
+ end
32
+ end
33
+
34
+ it "returns the wrapped value with operator if value exists" do
35
+ expect(ExampleModifierWithValue.new.to_s).to eq("(?:foo)%")
36
+ end
37
+
38
+ class ExampleModifierWithoutValue
39
+ include Hexpress::Modifier
40
+ def operator
41
+ "%"
42
+ end
43
+ end
44
+
45
+ it "returns just the operator" do
46
+ expect(ExampleModifierWithoutValue.new.to_s).to eq("%")
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,13 @@
1
+ require "spec_helper"
2
+
3
+ describe Hexpress::Nested::Find do
4
+ describe "#to_s" do
5
+ it "returns a capture of the hexpression" do
6
+ expect(described_class.new { word }.to_s ).to eq('(\w)')
7
+ end
8
+
9
+ it "returns a capture of the string" do
10
+ expect(described_class.new("foo").to_s ).to eq('(foo)')
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ require "spec_helper"
2
+
3
+ describe Hexpress::Nested::Matching do
4
+ describe "#hexpression" do
5
+ it "escapes strings" do
6
+ expect(described_class.new { [word, "-"] }.to_s).to eq('[\w\-]')
7
+ end
8
+ end
9
+
10
+ describe "#to_s" do
11
+ it "returns the instance of hexpressions and wraps them in []" do
12
+ expect(described_class.new { word }.to_s).to eq('[\w]')
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,42 @@
1
+ require "spec_helper"
2
+
3
+ describe Hexpress::Nested do
4
+ describe "#hexpression" do
5
+ class ExampleNestedWithDelimiter
6
+ include Hexpress::Nested
7
+ def initialize
8
+ @hexpression = ["foo", "bar"]
9
+ @delimiter = "."
10
+ end
11
+ end
12
+
13
+ it "returns the hexpression items with the specified delimiter" do
14
+ expect(ExampleNestedWithDelimiter.new.hexpression).to eq("foo.bar")
15
+ end
16
+
17
+ class ExampleNestedWithoutDelimiter
18
+ include Hexpress::Nested
19
+ def initialize
20
+ @hexpression = ["foo", "bar"]
21
+ end
22
+ end
23
+
24
+ it "returns the hexpression items with the specified delimiter" do
25
+ expect(ExampleNestedWithoutDelimiter.new.hexpression).to eq("foobar")
26
+ end
27
+ end
28
+
29
+ describe "#to_s" do
30
+ class ExampleNested
31
+ include Hexpress::Nested
32
+ def initialize
33
+ @hexpression = "foo"
34
+ @open, @close = "{", "}"
35
+ end
36
+ end
37
+
38
+ it "returns the hexpression wrapped in the open and close" do
39
+ expect(ExampleNested.new.to_s).to eq("{foo}")
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,13 @@
1
+ require "spec_helper"
2
+
3
+ describe Hexpress::Value::Ending do
4
+ describe "#to_s" do
5
+ it "returns the end of string pattern" do
6
+ expect(described_class.new.to_s).to eq("$")
7
+ end
8
+
9
+ it "has the given string before the end of string pattern" do
10
+ expect(described_class.new("foo").to_s).to eq("foo$")
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ require "spec_helper"
2
+
3
+ describe Hexpress::Value::Except do
4
+ describe "#to_s" do
5
+ it "returns the not marker followed by the value" do
6
+ expect(described_class.new("f").to_s).to eq("^f")
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,13 @@
1
+ require "spec_helper"
2
+
3
+ describe Hexpress::Value::Starting do
4
+ describe "#to_s" do
5
+ it "returns the start of string pattern" do
6
+ expect(described_class.new.to_s).to eq("^")
7
+ end
8
+
9
+ it "has the given string after the start of string pattern" do
10
+ expect(described_class.new("foo").to_s).to eq("^foo")
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ require "spec_helper"
2
+
3
+ describe Hexpress::Value::With do
4
+ describe "#to_s" do
5
+ it "returns the string given" do
6
+ expect(described_class.new("foo").to_s).to eq("foo")
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,25 @@
1
+ require "spec_helper"
2
+
3
+ describe Hexpress::Value do
4
+ describe "#value" do
5
+ class ExampleValue
6
+ include Hexpress::Value
7
+
8
+ def initialize(value)
9
+ @value = value
10
+ end
11
+ end
12
+
13
+ it "returns the string given" do
14
+ expect(ExampleValue.new("foo").value).to eq("foo")
15
+ end
16
+
17
+ it "returns regex characters escaped" do
18
+ expect(ExampleValue.new("fo.o").value).to eq("fo\\.o")
19
+ end
20
+
21
+ it "returns an empty string if value not given" do
22
+ expect(ExampleValue.new(nil).value).to eq("")
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,9 @@
1
+ require "spec_helper"
2
+
3
+ describe Hexpress::Values::Either do
4
+ describe "#to_s" do
5
+ it "returns items delimited by or matcher inside non-capture group" do
6
+ expect(described_class.new("foo", "bar").to_s).to eq("(?:foo|bar)")
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,33 @@
1
+ require "spec_helper"
2
+
3
+ describe Hexpress::Values::Range do
4
+ describe "#lower" do
5
+ it "returns the lower character matcher" do
6
+ expect(Hexpress.new.lower.to_s).to eq("a-z")
7
+ end
8
+ end
9
+
10
+ describe "#upper" do
11
+ it "returns the upper character matcher" do
12
+ expect(Hexpress.new.upper.to_s).to eq("A-Z")
13
+ end
14
+ end
15
+
16
+ describe "#letter" do
17
+ it "returns the upper and lower character matchers" do
18
+ expect(Hexpress.new.letter.to_s).to eq("a-zA-Z")
19
+ end
20
+ end
21
+
22
+ describe "#number" do
23
+ it "returns the number matcher" do
24
+ expect(Hexpress.new.number.to_s).to eq("0-9")
25
+ end
26
+ end
27
+
28
+ describe "#to_s" do
29
+ it "returns two values joined by range matcher" do
30
+ expect(described_class.new(0..9).to_s).to eq("0-9")
31
+ end
32
+ end
33
+ end