llt-helpers 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/Gemfile +6 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +29 -0
  6. data/Rakefile +8 -0
  7. data/lib/llt/helpers.rb +15 -0
  8. data/lib/llt/helpers/configuration.rb +44 -0
  9. data/lib/llt/helpers/constantize.rb +25 -0
  10. data/lib/llt/helpers/equality.rb +28 -0
  11. data/lib/llt/helpers/functions.rb +26 -0
  12. data/lib/llt/helpers/initialize.rb +28 -0
  13. data/lib/llt/helpers/metrical.rb +31 -0
  14. data/lib/llt/helpers/normalizer.rb +28 -0
  15. data/lib/llt/helpers/pluralize.rb +23 -0
  16. data/lib/llt/helpers/positions.rb +93 -0
  17. data/lib/llt/helpers/primitive_cache.rb +37 -0
  18. data/lib/llt/helpers/query_methods.rb +30 -0
  19. data/lib/llt/helpers/roman_numerals.rb +50 -0
  20. data/lib/llt/helpers/terminology.rb +219 -0
  21. data/lib/llt/helpers/transformer.rb +33 -0
  22. data/lib/llt/helpers/version.rb +5 -0
  23. data/llt-helpers.gemspec +27 -0
  24. data/spec/lib/llt/helpers/configuration_spec.rb +77 -0
  25. data/spec/lib/llt/helpers/constantize_spec.rb +79 -0
  26. data/spec/lib/llt/helpers/equality_spec.rb +48 -0
  27. data/spec/lib/llt/helpers/functions_spec.rb +47 -0
  28. data/spec/lib/llt/helpers/initialize_spec.rb +50 -0
  29. data/spec/lib/llt/helpers/metrical_spec.rb +40 -0
  30. data/spec/lib/llt/helpers/normalizer_spec.rb +57 -0
  31. data/spec/lib/llt/helpers/pluralize_spec.rb +38 -0
  32. data/spec/lib/llt/helpers/positions_spec.rb +108 -0
  33. data/spec/lib/llt/helpers/primitive_cache_spec.rb +65 -0
  34. data/spec/lib/llt/helpers/query_methods_spec.rb +67 -0
  35. data/spec/lib/llt/helpers/roman_numerals_spec.rb +41 -0
  36. data/spec/lib/llt/helpers/terminology_spec.rb +165 -0
  37. data/spec/lib/llt/helpers/transformer_spec.rb +49 -0
  38. data/spec/spec_helper.rb +22 -0
  39. metadata +182 -0
@@ -0,0 +1,79 @@
1
+ require 'spec_helper'
2
+
3
+ describe LLT::Helpers::Constantize do
4
+ class Dummy
5
+ include LLT::Helpers::Constantize
6
+ end
7
+
8
+ module LLT
9
+ class Test; end
10
+ class TestSuffixed; end
11
+ class PrefixedTest; end
12
+ class PrefixedTestSuffixed; end
13
+ class PrefixUsTestUs; end
14
+ end
15
+
16
+ class Test; end
17
+
18
+ module TestModule
19
+ class NestedTest; end
20
+ end
21
+
22
+ let(:dummy) { Dummy.new }
23
+
24
+ describe "#constant_by_type" do
25
+ it "returns a constant from a given type (as string)" do
26
+ dummy.constant_by_type("test").should == LLT::Test
27
+ end
28
+
29
+ it "returns a constant from a given type (as symbol)" do
30
+ dummy.constant_by_type(:test).should == LLT::Test
31
+ end
32
+
33
+ it "can be prefixed" do
34
+ dummy.constant_by_type(:test, prefix: "prefixed").should == LLT::PrefixedTest
35
+ end
36
+
37
+ it "can be suffixed" do
38
+ dummy.constant_by_type(:test, suffix: "suffixed").should == LLT::TestSuffixed
39
+ end
40
+
41
+ it "can be prefixed and suffixed" do
42
+ dummy.constant_by_type(:test, prefix: "prefixed", suffix: "suffixed").should == LLT::PrefixedTestSuffixed
43
+ end
44
+
45
+ it "underscores are handled correctly - test_underscore to TestUnderscore" do
46
+ dummy.constant_by_type(:test_us, prefix: :prefix_us).should == LLT::PrefixUsTestUs
47
+ end
48
+
49
+ it "namespace defaults to LLT" do
50
+ dummy.constant_by_type("test").should == LLT::Test
51
+ end
52
+
53
+ it "namespace can be niled to access top level classes" do
54
+ dummy.constant_by_type("test", namespace: nil).should == Test
55
+ end
56
+
57
+ it "accepts a namespace" do
58
+ dummy.constant_by_type(:nested_test, namespace: TestModule).should == TestModule::NestedTest
59
+ end
60
+
61
+ it "defaults the type argument to @type when no type is given" do
62
+ dummy.instance_variable_set("@type", :test)
63
+ dummy.constant_by_type.should == LLT::Test
64
+ end
65
+
66
+ it "default at @type is returned in its :full form" do
67
+ module LLT::Adjective; end
68
+ module LLT::Adj; end
69
+
70
+ dummy.instance_variable_set("@type", :adj)
71
+ dummy.constant_by_type.should == LLT::Adjective
72
+ dummy.constant_by_type.should_not == LLT::Adj
73
+ end
74
+
75
+ it "throws an error if type is nil" do
76
+ expect { dummy.constant_by_type(prefix: :test) }.to raise_error
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+
3
+ describe LLT::Helpers::Equality do
4
+ class Dummy2
5
+ extend LLT::Helpers::Equality
6
+
7
+ attr_reader :type, :lemma_key, :tempus
8
+
9
+ def initialize(type, lemma_key, tempus)
10
+ @type = type
11
+ @lemma_key = lemma_key
12
+ @tempus = tempus
13
+ end
14
+ end
15
+
16
+ describe ".equality_of_***_defined_by" do
17
+ it "defines attributes through which equality is identified and adds a method to ask for it" do
18
+ Dummy2.equality_of_lemma_defined_by :type, :lemma_key
19
+ d = Dummy2.new(:verb, 1489, :perfectum)
20
+ d.should respond_to(:equality_definition_for_lemma)
21
+ d.should respond_to(:same_lemma_as?)
22
+ end
23
+ end
24
+
25
+ describe "#same_***_as?" do
26
+ it "checks for equality as defined in the class definition" do
27
+ Dummy2.equality_of_lemma_defined_by :type, :lemma_key
28
+ d1 = Dummy2.new(:verb, 1, :perfectum)
29
+ d2 = Dummy2.new(:verb, 1, :futurum)
30
+ d3 = Dummy2.new(:verb, 2, :perfectum)
31
+
32
+ d1.same_lemma_as?(d2).should be_true
33
+ d1.same_lemma_as?(d3).should be_false
34
+ end
35
+
36
+ it "works through method missing" do
37
+ Dummy2.equality_of_form_defined_by :type, :lemma_key, :tempus
38
+ d1 = Dummy2.new(:verb, 1, :perfectum)
39
+ d2 = Dummy2.new(:verb, 1, :futurum)
40
+ d3 = Dummy2.new(:verb, 2, :perfectum)
41
+ d4 = Dummy2.new(:verb, 1, :perfectum)
42
+
43
+ d1.same_form_as?(d2).should be_false
44
+ d1.same_form_as?(d3).should be_false
45
+ d1.same_form_as?(d4).should be_true
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ describe LLT::Helpers::Functions do
4
+ class Dummy
5
+ include LLT::Helpers::Functions
6
+
7
+ def functions
8
+ [:a]
9
+ end
10
+ end
11
+
12
+ let(:dummy) { Dummy.new }
13
+
14
+ describe "#has_function?" do
15
+ it "returns true if argument is included in #functions" do
16
+ dummy.has_function?(:a).should be_true
17
+ end
18
+
19
+ it "returns false if argument is not included in #functions" do
20
+ dummy.has_function?(:b).should be_false
21
+ end
22
+ end
23
+
24
+ describe "#has_f?" do
25
+ it "is a shortcut for #has_function?" do
26
+ dummy.has_f?(:a).should be_true
27
+ dummy.has_f?(:b).should be_false
28
+ end
29
+ end
30
+
31
+ describe "#has_not_function?" do
32
+ it "returns false if argument is included in #functions" do
33
+ dummy.has_not_function?(:a).should be_false
34
+ end
35
+
36
+ it "returns true if argument is not included in #functions" do
37
+ dummy.has_not_function?(:b).should be_true
38
+ end
39
+ end
40
+
41
+ describe "#has_not_f?" do
42
+ it "is a shortcut for #has_not_function?" do
43
+ dummy.has_not_f?(:a).should be_false
44
+ dummy.has_not_f?(:b).should be_true
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+
3
+ describe LLT::Helpers::Initialize do
4
+ class Dummy
5
+ include LLT::Helpers::Initialize
6
+ end
7
+
8
+ let(:dummy) { Dummy.new }
9
+
10
+ describe "#extract_args!" do
11
+ let(:args) { { test1: 1, test2: 2, test3: 3 } }
12
+
13
+ context "with two arguments" do
14
+ it "extracts args given in a hash and maps given keys inside an Array to instance variables" do
15
+ dummy.extract_args!(args, [:test1, :test2])
16
+ dummy.instance_variable_get("@test1").should == 1
17
+ dummy.instance_variable_get("@test2").should == 2
18
+ dummy.instance_variable_get("@test3").should be_nil
19
+ end
20
+ end
21
+
22
+ context "with one argument" do
23
+ it "throws an error when #init_keys is not implemented" do
24
+ expect { dummy.extract_args!(args) }.to raise_error
25
+ end
26
+
27
+ it "maps the keys given in #init_keys" do
28
+ class Dummy
29
+ def init_keys
30
+ %i{ test1 test2 }
31
+ end
32
+ end
33
+
34
+ dummy.extract_args!(args)
35
+ dummy.instance_variable_get("@test1").should == 1
36
+ dummy.instance_variable_get("@test2").should == 2
37
+ dummy.instance_variable_get("@test3").should be_nil
38
+ end
39
+ end
40
+ end
41
+
42
+ describe "#extract_normalized_args!" do
43
+ it "extracts args and normalizes them" do
44
+ class Dummy; def init_keys; %i{ inflection_class }; end; end
45
+
46
+ dummy.extract_normalized_args!(itype: 1)
47
+ dummy.instance_variable_get("@inflection_class").should == 1
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe LLT::Helpers::Metrical do
4
+ class Dummy
5
+ include LLT::Helpers::Metrical
6
+ end
7
+
8
+ let(:dummy) { Dummy.new }
9
+
10
+ describe "#evaluate_metrical_presence" do
11
+ it "evaluates if a given string contains metrical utf8 information" do
12
+ dummy.evaluate_metrical_presence("test").should be_false
13
+ dummy.evaluate_metrical_presence("tēst").should be_true
14
+ end
15
+
16
+ it "returns false if the given string is actually nil" do
17
+ dummy.evaluate_metrical_presence(nil).should be_false
18
+ end
19
+ end
20
+
21
+ describe "#metrical?" do
22
+ it "defaults to false" do
23
+ dummy.metrical?.should be_false
24
+ end
25
+
26
+ it "responds accordingly after successful evaluation" do
27
+ dummy.evaluate_metrical_presence("test")
28
+ dummy.metrical?.should be_false
29
+
30
+ dummy.evaluate_metrical_presence("tēst")
31
+ dummy.metrical?.should be_true
32
+ end
33
+ end
34
+
35
+ describe "#wo_meter" do
36
+ it "strips all quantifying diacritics of a string" do
37
+ dummy.wo_meter('fēmĭnīs').should == 'feminis'
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ describe LLT::Helpers::Normalizer do
4
+ describe "#normalize_args" do
5
+ def norm(args)
6
+ normalizer.normalize_args(args)
7
+ end
8
+
9
+ let(:normalizer) { LLT::Helpers::Normalizer }
10
+ let(:dummy) { Dummy.new }
11
+
12
+ it "can be included as instance method" do
13
+ class Dummy
14
+ include LLT::Helpers::Normalizer
15
+ end
16
+
17
+
18
+ dummy.should respond_to(:normalize_args)
19
+ end
20
+
21
+ it "can be called as module method" do
22
+ normalizer.should respond_to(:normalize_args)
23
+ end
24
+
25
+ describe "rebuilds an argument hash with LLT::Constants::Terminology" do
26
+ it "normalizes" do
27
+ args = { "tense" => "present", case: "acc", numerus: "singular" }
28
+ norm(args).should == { tempus: :pr, casus: 4, numerus: 1 }
29
+ end
30
+
31
+ it "additionally normalizes the key stem to a symbol, even if there is no key term for it" do
32
+ args = { 'type' => 'noun', 'stem' => 'exercit', 'iclass' => '4', 'gender' => 'm' }
33
+ norm(args).should == { type: :noun, stem: 'exercit', inflection_class: 4, sexus: :m }
34
+ end
35
+ end
36
+
37
+ it "normalizes a nested options hash as well" do
38
+ args = { tempus: :present, options: { mood: :indicative } }
39
+ norm(args).should == { tempus: :pr, options: { modus: :ind } }
40
+ end
41
+
42
+ it "normalizes a nested options hash as well II" do
43
+ args = { options: { mood: "gerund" } }
44
+ norm(args).should == { options: { modus: :gerundium } }
45
+ end
46
+
47
+ it "leaves unknown keys intact" do
48
+ args = { "foo" => "bar" }
49
+ norm(args).should == args
50
+ end
51
+
52
+ it "leaves pure string values intact (nominative and stem)" do
53
+ norm(nom: "libido").should == { nom: "libido" }
54
+ norm(nominative: "libido").should == { nom: "libido" }
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe LLT::Helpers::Pluralize do
4
+ class Dummy
5
+ include LLT::Helpers::Pluralize
6
+ end
7
+
8
+ let(:dummy) { Dummy.new }
9
+
10
+ describe "#pluralize" do
11
+ describe "pluralizes like rails" do
12
+ context "when count is 1" do
13
+ it "returns sg" do
14
+ dummy.pluralize(1, "form").should == "form"
15
+ end
16
+ end
17
+
18
+ context "when count is 0 or < 1" do
19
+ context "without pl as param" do
20
+ it "builds s" do
21
+ dummy.pluralize(0, "form").should == "forms"
22
+ dummy.pluralize(2, "form").should == "forms"
23
+ end
24
+
25
+ it "builds y to ies" do
26
+ dummy.pluralize(2, "entry").should == "entries"
27
+ end
28
+ end
29
+
30
+ context "with custom pl as param" do
31
+ it "uses the custom plural" do
32
+ dummy.pluralize(2, "sg", "whatever").should == "whatever"
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,108 @@
1
+ require 'spec_helper'
2
+
3
+ describe LLT::Helpers::Positions do
4
+ class P
5
+ attr_accessor :position
6
+
7
+ include LLT::Helpers::Positions
8
+
9
+ def initialize(position)
10
+ @position = position
11
+ end
12
+ end
13
+
14
+ let(:ab) { [P.new(1), P.new(5)] }
15
+
16
+ describe "#===" do
17
+ it "is true when two objects have the same position" do
18
+ a = P.new(1)
19
+ b = P.new(1)
20
+ c = P.new(2)
21
+
22
+ (a === b).should be_true
23
+ (a === c).should be_false
24
+ end
25
+ end
26
+
27
+ describe "#<, #>, #>=, #<=, <=>" do
28
+ it "do as expected in regards to @position" do
29
+ a = P.new(1)
30
+ b = P.new(1)
31
+ c = P.new(2)
32
+
33
+ (a < c).should be_true
34
+ (a <=> b).should be_zero
35
+ end
36
+ end
37
+
38
+ describe "#surrounding" do
39
+ it "returns the fixnum value of the position before and after an object" do
40
+ a = P.new(1)
41
+ a.surrounding.should == [0, 2]
42
+ end
43
+ end
44
+
45
+ describe "#distance_to" do
46
+ it "returns the distance to another object" do
47
+ a, b = ab
48
+
49
+ a.distance_to(b).should == 4
50
+ end
51
+
52
+ it "returns a negative fixnum when the receiver is behind the object passed in as argument" do
53
+ a, b = ab
54
+
55
+ b.distance_to(a).should == -4
56
+ end
57
+
58
+ it "a negative result is absoluted, when a true flag is passed" do
59
+ a, b = ab
60
+ b.distance_to(a, true).should == 4
61
+ end
62
+ end
63
+
64
+ describe "#close_to?" do
65
+ it "responds to true if objects are positionally close to each other" do
66
+ a = P.new(1)
67
+ b = P.new(2)
68
+
69
+ a.close_to?(b).should be_true
70
+ b.close_to?(a).should be_true
71
+ end
72
+
73
+ it "takes a fixnuma argument to declare a range where close is considered to be true" do
74
+ a, b = ab # 1, 5
75
+ a.close_to?(b, 3).should be_false
76
+ a.close_to?(b, 4).should be_true
77
+ a.close_to?(b, 5).should be_true
78
+ end
79
+ end
80
+
81
+ describe "#between?" do
82
+ it "returns true if the receiver is between arguments a and b" do
83
+ a, b = ab
84
+ c = P.new(3)
85
+ d = P.new(6)
86
+
87
+ c.between?(a, b).should be_true
88
+ d.between?(a, b).should be_false
89
+ end
90
+ end
91
+
92
+ describe "#range_with" do
93
+ it "returns the positions between the receiver and the passed in object in an array - excludes start and end" do
94
+ a, b = ab
95
+ a.range_with(b).should == [2, 3, 4]
96
+ end
97
+
98
+ it "results are always sorted" do
99
+ a, b = ab
100
+ b.range_with(a).should == a.range_with(b)
101
+ end
102
+
103
+ it "returns a real Range object with a true flag" do
104
+ a, b = ab
105
+ a.range_with(b, true).should == (2...5)
106
+ end
107
+ end
108
+ end