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,65 @@
1
+ require 'spec_helper'
2
+
3
+ describe LLT::Helpers::PrimitiveCache do
4
+ class Dummy
5
+ include LLT::Helpers::PrimitiveCache
6
+ end
7
+
8
+ describe "#cached" do
9
+ it "takes a key and a block, whose result gets cached" do
10
+ d = Dummy.new
11
+ d.enable_cache
12
+ d.cached(:a) { 1 }.should == 1
13
+ d.cached(:a) { 2 }.should == 1
14
+ end
15
+
16
+ it "is disabled by default" do
17
+ d = Dummy.new
18
+ d.cached(:a) { 1 }.should == 1
19
+ d.cached(:a) { 2 }.should == 2
20
+ end
21
+ end
22
+
23
+ describe "#cache" do
24
+ it "returns the hash cache of the class" do
25
+ d = Dummy.new
26
+ d.enable_cache
27
+ d.cached(:a) { 1 }
28
+ d.cache.should == Dummy.cache
29
+ end
30
+
31
+ it "is shared across instances of a class" do
32
+ d1 = Dummy.new
33
+ d2 = Dummy.new
34
+ d1.enable_cache
35
+ d2.enable_cache
36
+ d1.cached(:a) { 1 }
37
+ d1.cached(:b) { 2 }
38
+
39
+ cache = Dummy.cache
40
+ d1.cache.should == cache
41
+ d2.cache.should == cache
42
+ end
43
+ end
44
+
45
+ describe "#enable_cache" do
46
+ it "enables the cache" do
47
+ d = Dummy.new
48
+ d.enable_cache
49
+ d.cached(:a) { 1 }.should == 1
50
+ d.cached(:a) { 2 }.should == 1
51
+ end
52
+ end
53
+
54
+ describe "#disable_cache" do
55
+ it "disables the cache" do
56
+ d = Dummy.new
57
+ d.enable_cache
58
+ d.cached(:a) { 1 }.should == 1
59
+ d.cached(:a) { 2 }.should == 1
60
+
61
+ d.disable_cache
62
+ d.cached(:a) { 3 }.should == 3
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,67 @@
1
+ require 'spec_helper'
2
+
3
+ describe LLT::Helpers::QueryMethods do
4
+ class Test
5
+ extend LLT::Helpers::QueryMethods
6
+
7
+ def initialize(type = nil)
8
+ @type = type
9
+ @other_type = :noun
10
+ end
11
+ end
12
+
13
+
14
+
15
+ describe ".add_query_methods_for" do
16
+
17
+ context "without optional arguments" do
18
+ it "adds all available query methods deriving from values for a key term as defined in LLT::Constants::Terminology" do
19
+ Test.add_query_methods_for(:type)
20
+ t = Test.new
21
+ t.should respond_to :noun?
22
+ t.should respond_to :adj?
23
+ t.should respond_to :adjective?
24
+ end
25
+
26
+ it "checks equality of a normalized value, stashed in an inst var named like the key term" do
27
+ Test.add_query_methods_for(:type)
28
+ t = Test.new(:adjective)
29
+ t.adj?.should be_true
30
+ t.adjective?.should be_true
31
+ t.noun?.should be_false
32
+ end
33
+ end
34
+
35
+ context "with option :use" do
36
+ it "redirects to another inst var" do
37
+ Test.add_query_methods_for(:type, use: :@other_type)
38
+ t = Test.new(:adj)
39
+ t.adj?.should be_false
40
+ t.noun?.should be_true
41
+ end
42
+ end
43
+
44
+ context "with option :delegate_to" do
45
+ it "delegates the equality check to the given method or variable" do
46
+ class Dummy
47
+ def noun?; true; end
48
+ def adj?; false; end
49
+ end
50
+ Test.add_query_methods_for(:type, delegate_to: :@type)
51
+ t = Test.new(Dummy.new)
52
+ t.adj?.should be_false
53
+ t.noun?.should be_true
54
+ end
55
+ end
56
+
57
+ context "with options :use and :delegate_to" do
58
+ it "raises an error" do
59
+ expect { Test.add_query_methods_for(:type, use: :x, delegate_to: :y) }.to raise_error(ArgumentError)
60
+ end
61
+ end
62
+
63
+ context "with a lambda" do
64
+ it "uses the block to handle the query"
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe LLT::Helpers::RomanNumerals do
4
+ describe ".roman?" do
5
+ it "detects if given value is a roman numeral" do
6
+ subject.roman?("MDCIX").should be_true
7
+ end
8
+
9
+ it "recognizes only fully upcased values as numeral atm" do
10
+ subject.roman?("mdcix").should be_false
11
+ end
12
+
13
+ it "doesn't match potentially misleading strings like M. for Marcus" do
14
+ subject.roman?("M.").should be_false
15
+ end
16
+ end
17
+
18
+ describe ".to_roman" do
19
+ LLT::Constants::NUMERALS.each do |dec, rom|
20
+ it "converts #{dec} to #{rom}" do
21
+ subject.to_roman(dec).should == rom
22
+ end
23
+ end
24
+
25
+ it "converts 1984 to MCMLXXXIV" do
26
+ subject.to_roman(1984).should == "MCMLXXXIV"
27
+ end
28
+ end
29
+
30
+ describe ".to_decimal" do
31
+ LLT::Constants::NUMERALS.each do |dec, rom|
32
+ it "converts #{rom} to #{dec}" do
33
+ subject.to_decimal(rom).should == dec
34
+ end
35
+ end
36
+
37
+ it "converts MCMLXXXIV to 1984" do
38
+ subject.to_decimal("MCMLXXXIV").should == 1984
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,165 @@
1
+ require 'spec_helper'
2
+
3
+ describe LLT::Helpers::Terminology do
4
+ let(:t) { LLT::Helpers::Terminology }
5
+
6
+ describe "#key_term_for" do
7
+ context "returns a normalized key for category names like tempus" do
8
+ it "handles different words for inflection_class" do
9
+ t.key_term_for(:inflectable_class).should == t.inflection_class
10
+ t.key_term_for(:itype).should == t.inflection_class
11
+ t.key_term_for(:inflection_class).should == t.inflection_class
12
+ end
13
+
14
+ it "handles different words for tempus" do
15
+ t.key_term_for(:tense).should == t.tempus
16
+ t.key_term_for(:tempus).should == t.tempus
17
+ end
18
+
19
+ it "handles different words for casus" do
20
+ t.key_term_for(:casus).should == t.casus
21
+ t.key_term_for(:case).should == t.casus
22
+ end
23
+
24
+ it "handles different words for modus" do
25
+ t.key_term_for(:modus).should == t.modus
26
+ t.key_term_for(:mood).should == t.modus
27
+ end
28
+
29
+ it "handles different words for modus" do
30
+ t.key_term_for(:deponens).should == t.deponens
31
+ t.key_term_for(:dep).should == t.deponens
32
+ end
33
+
34
+ it "normalizes strings to symbols" do
35
+ t.key_term_for("tempus").should == t.tempus
36
+ end
37
+ end
38
+ end
39
+
40
+ describe "#value_term_for" do
41
+ context "returns a normalized value - a key/category needs to given" do
42
+ it "handles different tempus names" do
43
+ t.value_term_for(:tempus, :pr).should == t.pr
44
+ t.value_term_for(:tempus, :praesens).should == t.pr
45
+ t.value_term_for(:tempus, :present).should == t.pr
46
+ end
47
+
48
+ it "normalizes strings to symbols" do
49
+ t.value_term_for(:casus, "nominative").should == t.nom
50
+ end
51
+
52
+ it "normalizes pure digits strings to integers" do
53
+ t.value_term_for(:inflection_class, '1').should == 1
54
+
55
+ # edge case for this hacky solution
56
+ t.value_term_for(:inflection_class, :esse).should == :esse
57
+ end
58
+
59
+ it "leaves alone Fixnums" do
60
+ t.value_term_for(:casus, 2).should == t.gen
61
+ end
62
+
63
+ it "leaves alone booleans" do
64
+ t.value_term_for(:deponens, true).should == true
65
+ t.value_term_for(:deponens, false).should == false
66
+ end
67
+
68
+ it "tries to normalize the key when initial lookup fails" do
69
+ t.value_term_for(:case, :acc).should == t.acc
70
+ end
71
+ end
72
+ end
73
+
74
+ describe "#values_for" do
75
+ it "returns all valid values for a given key term" do
76
+ t.values_for(:type).should include(:noun, :adjective, :adj)
77
+ end
78
+
79
+ it "key term is normalized" do
80
+ t.values_for(:pos).should include(:noun, :adjective, :adj)
81
+ end
82
+ end
83
+
84
+ describe "#norm_values_for" do
85
+ it "returns all values for a key term, that are defined as getter methods - default format is used when not given" do
86
+ t.norm_values_for(:numerus).should == [1, 2]
87
+ t.norm_values_for(:numerus, format: :numeric).should == [1, 2]
88
+ t.norm_values_for(:numerus, format: :abbr).should == [:sg, :pl]
89
+ t.norm_values_for(:numerus, format: :full).should == [:singularis, :pluralis]
90
+ t.norm_values_for(:numerus, format: :camelcase).should == [:Singularis, :Pluralis]
91
+
92
+ t.norm_values_for(:persona).should == [1, 2, 3]
93
+ end
94
+ end
95
+
96
+ describe ".method_missing" do
97
+ it "returns the sent method name name" do
98
+ t.whatever.should == :whatever
99
+ end
100
+ end
101
+
102
+ context "contains methods that identify a given terminology standard" do
103
+ it "returns a camelcases symbol if called with :camelcase" do
104
+ t.pr(:camelcase).should == :Praesens
105
+ t.fut_ex(:camelcase).should == :FuturumExactum
106
+
107
+ end
108
+ context "with tempora" do
109
+ it "returns symbolic value in abbreviated form by default" do
110
+ t.pr.should == :pr
111
+ end
112
+
113
+ it "returns symbolic value in unabbreviated latin with an argument of false" do
114
+ t.pr(:full).should == :praesens
115
+ end
116
+
117
+ it "can return a numeric value" do
118
+ t.pr(:numeric) .should == 1
119
+ t.impf(:numeric) .should == 2
120
+ t.fut(:numeric) .should == 3
121
+ t.pf(:numeric) .should == 4
122
+ t.pqpf(:numeric) .should == 5
123
+ t.fut_ex(:numeric).should == 6
124
+ end
125
+ end
126
+
127
+ context "with casus and numerus" do
128
+ it "returns numeric value of a casus by default" do
129
+ t.genetivus.should == 2
130
+ t.ablativus.should == 6
131
+ t.singularis.should == 1
132
+ end
133
+
134
+ it "methods are aliased to an abbreviated form" do
135
+ t.gen.should == 2
136
+ t.dat.should == 3
137
+ t.pl.should == 2
138
+ end
139
+
140
+ it "returns an abbreviated symbol with an argument of :abbr" do
141
+ t.accusativus(:abbr).should == :acc
142
+ t.pl(:abbr).should == :pl
143
+ end
144
+
145
+ it "returns an unabbreviated symbol with an argument of :full" do
146
+ t.nom(:full).should == :nominativus
147
+ t.pl(:full).should == :pluralis
148
+ end
149
+ end
150
+
151
+ context "with sexus" do
152
+ it "returns a one letter abbreviation by default" do
153
+ t.m.should == :m
154
+ t.f.should == :f
155
+ t.n.should == :n
156
+ end
157
+
158
+ it "can return a numeric value" do
159
+ t.m(:numeric).should == 1
160
+ t.f(:numeric).should == 2
161
+ t.n(:numeric).should == 3
162
+ end
163
+ end
164
+ end
165
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ describe LLT::Helpers::Transformer do
4
+ class Dummy
5
+ include LLT::Helpers::Transformer
6
+
7
+ def initialize
8
+ @a = 1
9
+ @b = 2
10
+ @c = 3
11
+ end
12
+ end
13
+
14
+ let(:dummy) { Dummy.new }
15
+
16
+ describe "#to_hash" do
17
+ it "maps all instance variables to key/value pairs" do
18
+ dummy.to_hash.should == { a: 1, b: 2, c: 3 }
19
+ end
20
+
21
+ it "root node can be given" do
22
+ dummy.to_hash(root: "a_root").should == { "a_root" => { a: 1, b: 2, c: 3 } }
23
+ end
24
+
25
+ it "if root node is given as instance of TrueClass, the class name gets used as root node" do
26
+ dummy.to_hash(root: true).should == { "Dummy" => { a: 1, b: 2, c: 3 } }
27
+ end
28
+
29
+ it "if symbols are given in a whitelist, only corresponding values will get returned" do
30
+ dummy.to_hash(whitelist: [:a, :b]).should == { a: 1, b: 2 }
31
+ end
32
+
33
+ it "if symbols are given in a blacklist, these values will be skipped" do
34
+ dummy.to_hash(blacklist: [:b]).should == { a: 1, c: 3 }
35
+ end
36
+
37
+ it "default type for keys are symbols, can be manipulated with keys: #method" do
38
+ dummy.to_hash(whitelist: [:a], keys: :to_s).should == { "a" => 1 }
39
+ end
40
+
41
+ it "custom contents can be given" do
42
+ dummy.to_hash(custom: { a: 5 }, whitelist: [:b]).should == { a: 5, b: 2 }
43
+ end
44
+
45
+ it "custom contents would still be inside a root node" do
46
+ dummy.to_hash(custom: { a: 5 }, blacklist: [:b, :c], root: :root).should == { root: { a: 5 } }
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,22 @@
1
+ require 'simplecov'
2
+ require 'coveralls'
3
+
4
+ Coveralls.wear!
5
+
6
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
7
+ SimpleCov::Formatter::HTMLFormatter,
8
+ Coveralls::SimpleCov::Formatter
9
+ ]
10
+
11
+ SimpleCov.start do
12
+ add_filter '/spec/'
13
+ end
14
+
15
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
16
+ require 'llt/helpers'
17
+
18
+ RSpec.configure do |config|
19
+ config.treat_symbols_as_metadata_keys_with_true_values = true
20
+ config.run_all_when_everything_filtered = true
21
+ config.filter_run :focus
22
+ end
metadata ADDED
@@ -0,0 +1,182 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: llt-helpers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - LFDM
8
+ - lichtr
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-12-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.3'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.3'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: simplecov
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '0.7'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '0.7'
70
+ - !ruby/object:Gem::Dependency
71
+ name: dalli
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :runtime
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: llt-constants
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :runtime
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ description: LLT Helpers
99
+ email:
100
+ - 1986gh@gmail.com
101
+ - robert.lichtensteiner@gmail.com
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - ".gitignore"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - lib/llt/helpers.rb
112
+ - lib/llt/helpers/configuration.rb
113
+ - lib/llt/helpers/constantize.rb
114
+ - lib/llt/helpers/equality.rb
115
+ - lib/llt/helpers/functions.rb
116
+ - lib/llt/helpers/initialize.rb
117
+ - lib/llt/helpers/metrical.rb
118
+ - lib/llt/helpers/normalizer.rb
119
+ - lib/llt/helpers/pluralize.rb
120
+ - lib/llt/helpers/positions.rb
121
+ - lib/llt/helpers/primitive_cache.rb
122
+ - lib/llt/helpers/query_methods.rb
123
+ - lib/llt/helpers/roman_numerals.rb
124
+ - lib/llt/helpers/terminology.rb
125
+ - lib/llt/helpers/transformer.rb
126
+ - lib/llt/helpers/version.rb
127
+ - llt-helpers.gemspec
128
+ - spec/lib/llt/helpers/configuration_spec.rb
129
+ - spec/lib/llt/helpers/constantize_spec.rb
130
+ - spec/lib/llt/helpers/equality_spec.rb
131
+ - spec/lib/llt/helpers/functions_spec.rb
132
+ - spec/lib/llt/helpers/initialize_spec.rb
133
+ - spec/lib/llt/helpers/metrical_spec.rb
134
+ - spec/lib/llt/helpers/normalizer_spec.rb
135
+ - spec/lib/llt/helpers/pluralize_spec.rb
136
+ - spec/lib/llt/helpers/positions_spec.rb
137
+ - spec/lib/llt/helpers/primitive_cache_spec.rb
138
+ - spec/lib/llt/helpers/query_methods_spec.rb
139
+ - spec/lib/llt/helpers/roman_numerals_spec.rb
140
+ - spec/lib/llt/helpers/terminology_spec.rb
141
+ - spec/lib/llt/helpers/transformer_spec.rb
142
+ - spec/spec_helper.rb
143
+ homepage: http://christof.github.io/llt
144
+ licenses:
145
+ - MIT
146
+ metadata: {}
147
+ post_install_message:
148
+ rdoc_options: []
149
+ require_paths:
150
+ - lib
151
+ required_ruby_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ required_rubygems_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ requirements: []
162
+ rubyforge_project:
163
+ rubygems_version: 2.1.5
164
+ signing_key:
165
+ specification_version: 4
166
+ summary: Helper modules used by several llt gems
167
+ test_files:
168
+ - spec/lib/llt/helpers/configuration_spec.rb
169
+ - spec/lib/llt/helpers/constantize_spec.rb
170
+ - spec/lib/llt/helpers/equality_spec.rb
171
+ - spec/lib/llt/helpers/functions_spec.rb
172
+ - spec/lib/llt/helpers/initialize_spec.rb
173
+ - spec/lib/llt/helpers/metrical_spec.rb
174
+ - spec/lib/llt/helpers/normalizer_spec.rb
175
+ - spec/lib/llt/helpers/pluralize_spec.rb
176
+ - spec/lib/llt/helpers/positions_spec.rb
177
+ - spec/lib/llt/helpers/primitive_cache_spec.rb
178
+ - spec/lib/llt/helpers/query_methods_spec.rb
179
+ - spec/lib/llt/helpers/roman_numerals_spec.rb
180
+ - spec/lib/llt/helpers/terminology_spec.rb
181
+ - spec/lib/llt/helpers/transformer_spec.rb
182
+ - spec/spec_helper.rb