llt-db_handler 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 84082e870e818bb1bc72a8b9201b23a80d645b39
4
+ data.tar.gz: ba26046cb7b5ab0ead5aafbd04648cf2838d8387
5
+ SHA512:
6
+ metadata.gz: 8dfa39ffb5c43cfb2f22108808d59a44971e5f8a81b6eae7e955bbbe695d2467474ca3a3b09f647e0f710ae2829fd59857e24635765790bc3b865040193a0727
7
+ data.tar.gz: ab37d71755e9dd08c9f70336dfff0d8b5e8384a3800d17546bb4f0e4948825b0da68e91817f005f46e3e29af1bc7597999ebcb1d4a61c5db4a7352a05b2d6a28
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --tty
data/Gemfile ADDED
@@ -0,0 +1,18 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in llt-db_handler.gemspec
4
+ gemspec
5
+ gem 'coveralls', require: false
6
+ gem 'llt-core_extensions', git: 'git@github.com:latin-language-toolkit/llt-core_extensions.git'
7
+ gem 'llt-constants', git: 'git@github.com:latin-language-toolkit/llt-constants.git'
8
+ gem 'llt-form_builder', git: 'git@github.com:latin-language-toolkit/llt-form_builder.git'
9
+ gem 'llt-helpers', git: 'git@github.com:latin-language-toolkit/llt-helpers.git'
10
+
11
+ platform :ruby do
12
+ gem 'pg'
13
+ end
14
+
15
+ platform :jruby do
16
+ gem 'activerecord-jdbcpostgresql-adapter'
17
+ end
18
+
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 LFDM
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,80 @@
1
+ # LLT::DbHandler
2
+
3
+ LLT abstraction to communicate with stem dictionaries/databases.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'llt-db_handler'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install llt-db_handler
18
+
19
+ You will also want to install a proper postgresql adapter for the stem
20
+ dictionary. You could add this to your Gemfile:
21
+
22
+ ```ruby
23
+ platform :ruby do
24
+ gem 'pg'
25
+ end
26
+
27
+ platform :jruby do
28
+ gem 'activerecord-jdbcpostgresql-adapter'
29
+ end
30
+ ```
31
+
32
+
33
+ ## Usage
34
+
35
+ The Prometheus Stem Dictionary comes with this gem. To use it make sure
36
+ you have postgresql installed and a user called prometheus ready:
37
+
38
+ ```
39
+ psql
40
+ create user prometheus with password 'admin'
41
+ alter user prometheus with create_db
42
+ ```
43
+ Create the database and seed data:
44
+
45
+ ```
46
+ rake db:prometheus:create
47
+ rake db:prometheus:seed
48
+ ```
49
+
50
+ The database prometheus_stems will now be available.
51
+ <!-->
52
+ # This should not be needed as the db is created by the user prometheus
53
+ anyway.
54
+ You might have to grant privileges to the user prometheus before going
55
+ further:
56
+
57
+ ```
58
+ psql
59
+ grant all on database prometheus_stems to prometheus
60
+ ```
61
+ -->
62
+
63
+ ```ruby
64
+ require 'llt/db_handler/prometheus'
65
+
66
+ db = LLT::DbHandler::Prometheus.new
67
+ db.count # => returns the total number of entries
68
+ db.all_entries # => returns all entries as AR models
69
+ db.lemma_list # => returns an Array of lemmas as strings
70
+ db.lemma_list(true) # => returns detailed lemma strings
71
+ ```
72
+
73
+
74
+ ## Contributing
75
+
76
+ 1. Fork it
77
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
78
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
79
+ 4. Push to the branch (`git push origin my-new-feature`)
80
+ 5. Create new Pull Request
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+ require "llt/db_handler/tasks"
4
+
5
+ RSpec::Core::RakeTask.new(:spec) do |t|
6
+ t.rspec_opts = '-f d --color'
7
+ end
8
+
9
+ task :default => :spec
@@ -0,0 +1,12 @@
1
+ require "llt/db_handler/version"
2
+ require 'llt/db_handler/common_db'
3
+
4
+ module LLT
5
+ module DbHandler
6
+ def self.use(db)
7
+ db = db.capitalize
8
+ raise ArgumentError, "No database handler called #{db} defined" unless const_defined?(db)
9
+ const_get(db.capitalize).new
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,15 @@
1
+ require 'llt/helpers/normalizer'
2
+
3
+ module LLT
4
+ module DbHandler
5
+ class CommonDb
6
+ include Helpers::Normalizer
7
+
8
+ require 'llt/db_handler/prometheus'
9
+
10
+ def look_up_stem(args)
11
+ @args = normalize_args(args)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,95 @@
1
+ require 'forwardable'
2
+ require 'llt/db_handler/common_db'
3
+ require 'llt/helpers'
4
+ require 'llt/stem_builder'
5
+
6
+ module LLT
7
+ module DbHandler
8
+ class Prometheus < CommonDb
9
+ extend Forwardable
10
+
11
+ require 'active_record'
12
+ require 'llt/db_handler/prometheus/db/models'
13
+ require 'llt/db_handler/prometheus/stats'
14
+
15
+ include Helpers::Constantize
16
+ include Helpers::PrimitiveCache
17
+
18
+ def_delegators :stats, :all_entries, :count, :lemma_list
19
+
20
+ attr_reader :type
21
+
22
+ def initialize(cache: false)
23
+ @type = :prometheus
24
+ enable_cache if cache
25
+ end
26
+
27
+ def direct_lookup(table, string)
28
+ query_db(table, string)
29
+ end
30
+
31
+ def look_up_stem(args)
32
+ cached(args) { new_lookup(args) }
33
+ end
34
+
35
+ def stats
36
+ @stats ||= Stats.new
37
+ end
38
+
39
+ private
40
+
41
+ def new_lookup(args)
42
+ table = args[:type]
43
+ column = args[:stem_type]
44
+ column = (column.to_s << "_stem").to_sym if column == :pr || column == :pf
45
+ stem = args[:stem]
46
+ restrictions = args[:restrictions]
47
+
48
+ entries = query_db(table, stem, column)
49
+
50
+ if restrictions
51
+ restr_type = normalized(restrictions[:type])
52
+ valids = restrictions[:values]
53
+ entries.keep_if { |entry| valids.include?(entry.send(restr_type)) }
54
+ end
55
+
56
+ stemify(entries)
57
+ end
58
+
59
+ def query_db(table, string, column = :word)
60
+ # I tried to make an sql string out of this, but it's a lot slower actually...
61
+ constant_by_type(table, prefix: :Db, namespace: StemDatabase).where(column => string)
62
+ end
63
+
64
+ def stemify(entries)
65
+ entries.flat_map do |entry|
66
+ type = entry.type
67
+ args = case type
68
+ when :noun then hashify(entry, :nom, :stem, :inflectable_class, :sexus)
69
+ when :persona then hashify(entry, :nom, :stem, :inflectable_class, :sexus)
70
+ when :place then hashify(entry, :nom, :stem, :inflectable_class, :sexus)
71
+ when :adjective then hashify(entry, :nom, :stem, :inflectable_class, :number_of_endings)
72
+ when :ethnic then hashify(entry, :stem, :inflectable_class)
73
+ when :verb then hashify(entry, :pr, :pf, :ppp, :inflectable_class, :pf_composition, :deponens, :dir_objs, :indir_objs)
74
+ end
75
+
76
+ args ? StemBuilder.build(type, args, @type) : []
77
+ end
78
+ end
79
+
80
+ def hashify(entry, *args)
81
+ h = {}
82
+ args.each { |arg| h[arg] = entry.send(arg) }
83
+ h[:lemma_key] = entry.id
84
+ h
85
+ end
86
+
87
+ def normalized(restr_type)
88
+ case restr_type.to_sym
89
+ when :inflection_class then :inflectable_class
90
+ else restr_type
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,8 @@
1
+ adapter: postgresql
2
+ encoding: unicode
3
+ database: prometheus_stems
4
+ pool: 20
5
+ port: 5432
6
+ username: prometheus
7
+ password: admin
8
+ host: localhost
@@ -0,0 +1,111 @@
1
+ require 'llt/db_handler/prometheus/db_to_lemma'
2
+
3
+ module StemDatabase
4
+ class Db < ActiveRecord::Base
5
+ self.abstract_class = true
6
+ db = YAML::load(File.open(File.expand_path("../database.yml", __FILE__)))
7
+ establish_connection(db)
8
+
9
+ include LLT::DbHandler::Prometheus::DbToLemma
10
+
11
+ def type
12
+ self.class.name.match(/Db(.*)/)[1].downcase.to_sym
13
+ end
14
+ end
15
+
16
+ class DbAdjective < Db
17
+ attr_accessible :inflectable_class, :nom, :number_of_endings, :stem, :number
18
+ validates_presence_of :nom, :stem, :number_of_endings, :inflectable_class
19
+ validates :stem, uniqueness: { scope: %i{ nom number_of_endings inflectable_class number } }
20
+
21
+ def base_lemma
22
+ nom
23
+ end
24
+ end
25
+
26
+ class DbAdverb < Db
27
+ attr_accessible :double, :word
28
+
29
+ validates_presence_of :word
30
+ validates :word, uniqueness: true
31
+
32
+ def base_lemma
33
+ word
34
+ end
35
+ end
36
+
37
+ class DbEthnic < Db
38
+ attr_accessible :inflectable_class, :stem
39
+ validates_presence_of :stem, :inflectable_class
40
+ validates :stem, uniqueness: { scope: :inflectable_class }
41
+
42
+ def base_lemma
43
+ "#{stem}#{lemma_ending}"
44
+ end
45
+
46
+ private
47
+
48
+ def lemma_ending
49
+ case inflectable_class
50
+ when 1 then 'us'
51
+ when 3 then 'is'
52
+ end
53
+ end
54
+ end
55
+
56
+ class DbPersona < Db
57
+ attr_accessible :defective, :inflectable_class, :nom, :sexus, :stem
58
+ validates_presence_of :stem, :inflectable_class
59
+ validates :stem, uniqueness: { scope: %i{ nom inflectable_class sexus } }
60
+
61
+ def base_lemma
62
+ nom
63
+ end
64
+ end
65
+
66
+ class DbPlace < Db
67
+ attr_accessible :defective, :inflectable_class, :nom, :sexus, :stem
68
+ validates_presence_of :stem, :inflectable_class
69
+ validates :stem, uniqueness: { scope: %i{ nom inflectable_class sexus } }
70
+
71
+ def base_lemma
72
+ nom
73
+ end
74
+ end
75
+
76
+ class DbNoun < Db
77
+ attr_accessible :defective, :inflectable_class, :nom, :sexus, :stem
78
+ validates_presence_of :stem, :inflectable_class
79
+ validates :stem, uniqueness: { scope: %i{ nom inflectable_class sexus } }
80
+
81
+ def base_lemma
82
+ nom
83
+ end
84
+ end
85
+
86
+ class DbVerb < Db
87
+ attr_accessible :pr_stem, :pf_stem, :ppp, :inflectable_class, :dir_objs,
88
+ :indir_objs, :deponens, :number
89
+ validates_presence_of :pr_stem, :inflectable_class
90
+ validates :pr_stem, uniqueness: { scope: %i{ pf_stem ppp deponens inflectable_class } }
91
+
92
+ def pr; pr_stem; end
93
+ def pf; pf_stem; end
94
+ def extension; tempus_sign; end
95
+
96
+ def base_lemma
97
+ "#{lemma_stem}#{lemma_ending}"
98
+ end
99
+
100
+ private
101
+
102
+ def lemma_stem
103
+ inflectable_class == 1 ? pr_stem.chop : pr_stem
104
+ end
105
+
106
+ def lemma_ending
107
+ ending = inflectable_class == 5 ? 'io' : 'o'
108
+ deponens ? (ending + 'r') : ending
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,46 @@
1
+ module LLT
2
+ module DbHandler
3
+ class Prometheus < CommonDb
4
+ module DbToLemma
5
+ # to be included by Db classes, the module
6
+ # is tightly coupled to them
7
+
8
+ def to_lemma(detailed = false)
9
+ if detailed
10
+ "#{base_lemma}##{number_attr}, #{category}#{str_helper(:inflectable_class, 'iclass')}#{str_helper(:sexus)}"
11
+ else
12
+ base_lemma
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ def category
19
+ # DbVerb to 'verb'
20
+ self.class.name[2..-1].downcase
21
+ end
22
+
23
+ def str_helper(meth, appearance = meth)
24
+ val = guarded(meth)
25
+ (val ? ", #{appearance}: #{val}" : '')
26
+ end
27
+
28
+ def base_lemma
29
+ raise NoMethodError.new("Has to be overwritten by class that includes the module DbToLemma")
30
+ end
31
+
32
+ def number_attr
33
+ guarded(:number, 1)
34
+ end
35
+
36
+ def guarded(meth, default_return = nil)
37
+ if respond_to?(meth)
38
+ send(meth)
39
+ else
40
+ default_return
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,44 @@
1
+ module LLT
2
+ module DbHandler
3
+ class Prometheus
4
+ class Stats
5
+ include Enumerable
6
+
7
+ CATEGORIES = %i{ noun adjective adverb verb persona ethnic place }
8
+ TABLES = CATEGORIES.map { |cat| cat.to_s.capitalize.prepend('Db').to_sym }
9
+
10
+ def count
11
+ @count ||= compute_count
12
+ end
13
+
14
+ def all_entries
15
+ @all_entries ||= flat_map(&:all)
16
+
17
+ if block_given?
18
+ @all_entries.map { |entry| yield(entry) }
19
+ else
20
+ @all_entries
21
+ end
22
+ end
23
+
24
+ def lemma_list(detailed = false)
25
+ all_entries.map { |entry| entry.to_lemma(detailed) }
26
+ end
27
+
28
+ private
29
+
30
+ def compute_count
31
+ map(&:count).inject(:+)
32
+ end
33
+
34
+ def each(&blk)
35
+ TABLES.map { |table| as_const(table) }.each(&blk)
36
+ end
37
+
38
+ def as_const(symbol)
39
+ StemDatabase.const_get(symbol)
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,61 @@
1
+ require 'llt/db_handler/common_db'
2
+
3
+ module LLT
4
+ module DbHandler
5
+ class Stub < CommonDb
6
+ require 'llt/db_handler/stub/stub_entries'
7
+
8
+ @stems = {}
9
+
10
+ class << self
11
+ include Helpers::Normalizer
12
+
13
+ attr_reader :stems
14
+
15
+ def create_stem_stub(return_val, args)
16
+ args = normalize_args(args)
17
+ @stems[args] = return_val
18
+ end
19
+ alias :create :create_stem_stub
20
+
21
+ def setup
22
+ StubEntries.setup
23
+ end
24
+ end
25
+
26
+ def type
27
+ :stub
28
+ end
29
+
30
+ def look_up_stem(args)
31
+ super
32
+ stems.select do |stored_args|
33
+ if stored_args.merge(args_to_query) == stored_args
34
+ if restr = @args[:restrictions]
35
+ restr[:values].include?(stored_args[restr[:type]])
36
+ end
37
+ end
38
+ end.values
39
+ end
40
+
41
+ def direct_lookup(type, string)
42
+ args = dl_to_query(type, string)
43
+ stems.select do |stored|
44
+ stored.merge(args) == stored
45
+ end.values
46
+ end
47
+
48
+ def dl_to_query(type, string)
49
+ { type: type, word: string}
50
+ end
51
+
52
+ def args_to_query
53
+ {type: @args[:type], @args[:stem_type] => @args[:stem] }
54
+ end
55
+
56
+ def stems
57
+ self.class.stems
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,87 @@
1
+ require 'ostruct'
2
+
3
+ class LLT::DbHandler::Stub::StubEntries
4
+ class << self
5
+ def setup(size = :medium)
6
+ db_stub.stems.clear
7
+
8
+ medium_setup
9
+ # not implemented yet
10
+ #case size
11
+ #when :big then big_setup
12
+ #when :small then small_setup
13
+ #else medium_setup
14
+ #end
15
+ end
16
+
17
+ private
18
+
19
+ def small_setup
20
+ end
21
+
22
+ def medium_setup
23
+ wsr(type: :noun, nom: "homo", stem: "homin", itype: 3, sexus: :m)
24
+ wsr(type: :noun, nom: "vir", stem: "vir", itype: 2, sexus: :m)
25
+ wsr(type: :noun, nom: "ratio", stem: "ration", itype: 3, sexus: :f)
26
+ wsr(type: :noun, nom: "magnitudo", stem: "magnitudin", itype: 3, sexus: :f)
27
+ wsr(type: :noun, nom: "libido", stem: "libidin", itype: 3, sexus: :f)
28
+ wsr(type: :noun, nom: "nox", stem: "noct", itype: 3, sexus: :f)
29
+ wsr(type: :noun, nom: "filius", stem: "fili", itype: 2, sexus: :m)
30
+ wsr(type: :noun, nom: "servus", stem: "serv", itype: 2, sexus: :m)
31
+
32
+ wsr(type: :noun, nom: "flumen", stem: "flumin", itype: 3, sexus: :n)
33
+ wsr(type: :noun, nom: "arma", stem: "arm", itype: 2, sexus: :n) # this might actually be wrong?
34
+
35
+ wsr(type: :persona, nom: "Plato", stem: "Platon", itype: 3)
36
+ wsr(type: :persona, nom: "Solon", stem: "Solon", itype: 3)
37
+
38
+ wsr(type: :adjective, nom: "communis", stem: "commun", itype: 3, number_of_endings: 1)
39
+ wsr(type: :adjective, nom: "diligens", stem: "diligent", itype: 3, number_of_endings: 1)
40
+ wsr(type: :adjective, nom: "laetus", stem: "laet", itype: 1, number_of_endings: 3)
41
+ wsr(type: :adjective, nom: "ferus", stem: "fer", itype: 1, number_of_endings: 3)
42
+
43
+ wsr(type: :adjective, nom: "aestivus", stem: "aestiv", itype: 1, number_of_endings: 3)
44
+ wsr(type: :adjective, nom: "suavis", stem: "suav", itype: 3, number_of_endings: 2)
45
+
46
+
47
+ wsr(type: :ethnic, stem: "Haedu", inflection_class: 1)
48
+ wsr(type: :ethnic, stem: "Redon", inflection_class: 3)
49
+
50
+ wsr(type: :verb, pr: "ita", pf: "itav", pf_composition: "v", itype: 1, dep: false, dir_objs: "", indir_objs: "")
51
+ wsr(type: :verb, pr: "cana", pf: "canav", ppp: "canat", pf_composition: "v", itype: 1, dep: false, dir_objs: "", indir_objs: "")
52
+ wsr(type: :verb, pr: "mone", pf: "monu", ppp: "monit", pf_composition: "u", itype: 2, dep: false, dir_objs: "", indir_objs: "")
53
+ wsr(type: :verb, pr: "move", pf: "movi", ppp: "mot", pf_composition: "ablaut", itype: 2, dep: false, dir_objs: "", indir_objs: "")
54
+ wsr(type: :verb, pr: "mitt", pf: "mis", ppp: "miss", pf_composition: "s", itype: 3, dep: false, dir_objs: "", indir_objs: "")
55
+ wsr(type: :verb, pr: "viv", pf: "vix", ppp: "-", pf_composition: "s", itype: 3, dep: false, dir_objs: "", indir_objs: "")
56
+ wsr(type: :verb, pr: "audi", pf: "audiv", ppp: "audit", pf_composition: "v", itype: 4, dep: false, dir_objs: "", indir_objs: "")
57
+ wsr(type: :verb, pr: "horta", ppp: "hortat", itype: 1, dep: true , dir_objs: "", indir_objs: "")
58
+
59
+ dl(type: :adverb, word: "ita")
60
+ dl(type: :adverb, word: "iam")
61
+ dl(type: :adverb, word: "subito")
62
+ end
63
+
64
+ def big_setup
65
+ end
66
+
67
+ def db_stub
68
+ LLT::DbHandler::Stub
69
+ end
70
+
71
+ def factory(type, args)
72
+ LLT::StemBuilder.build(type, args, "stub")
73
+ end
74
+
75
+ # with_single_return
76
+ def wsr(args)
77
+ ret_val = factory(args[:type], args.reject { |k, _| k == :type })
78
+ db_stub.create(ret_val, args)
79
+ end
80
+
81
+ # direct_lookup
82
+ def dl(args)
83
+ ret_val = OpenStruct.new(word: args[:word])
84
+ db_stub.create(ret_val, args)
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,32 @@
1
+ namespace :db do
2
+ namespace :prometheus do
3
+ DUMP_FILE = 'lib/llt/db_handler/prometheus/db/prometheus_stems.dump'
4
+
5
+ desc 'Opens a pry console with a prometheus instance preloaded as db'
6
+ task :console do
7
+ exec %{pry -e "require 'llt/db_handler/prometheus';
8
+ db = LLT::DbHandler::Prometheus.new;
9
+ puts 'A Prometheus instance is waiting for you in the variable db\!'; db"}
10
+ end
11
+
12
+ desc 'Creates the stem database'
13
+ task :create do
14
+ exec 'createdb -U prometheus -h localhost -T template0 prometheus_stems'
15
+ end
16
+
17
+ desc 'Opens the psql console'
18
+ task :db_console do
19
+ exec 'psql -U prometheus prometheus_stems'
20
+ end
21
+
22
+ desc "Dumps the stem databases' contents to a psql dump file"
23
+ task :dump do
24
+ exec "pg_dump -U prometheus -Fc prometheus_stems > #{DUMP_FILE}"
25
+ end
26
+
27
+ desc 'Loads the seed data - UNDUMPED CHANGES WILL BE LOST!'
28
+ task :seed do
29
+ exec "pg_restore --verbose --clean --no-acl --no-owner -h localhost -U prometheus -d prometheus_stems #{DUMP_FILE}"
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,5 @@
1
+ module LLT
2
+ module DbHandler
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'llt/db_handler/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "llt-db_handler"
8
+ spec.version = LLT::DbHandler::VERSION
9
+ spec.authors = ["LFDM"]
10
+ spec.email = ["1986gh@gmail.com"]
11
+ spec.description = %q{LLT DB Handler}
12
+ spec.summary = %q{LLT DB Handler}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "pry"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "simplecov", "~> 0.7"
26
+ spec.add_development_dependency "yard"
27
+ spec.add_dependency 'activerecord', "~> 3.2"
28
+ spec.add_dependency "llt-core_extensions"
29
+ spec.add_dependency "llt-constants"
30
+ spec.add_dependency "llt-form_builder"
31
+ spec.add_dependency "llt-helpers"
32
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+ require 'llt/db_handler/prometheus/db_to_lemma'
3
+
4
+ describe LLT::DbHandler::Prometheus::DbToLemma do
5
+ class DummyDbClass
6
+ include LLT::DbHandler::Prometheus::DbToLemma
7
+ end
8
+
9
+ let(:dummy) { DummyDbClass.new }
10
+
11
+ # private stuff, just to be safe
12
+ describe "#number_attr" do
13
+ it "returns the entries number attribute" do
14
+ dummy.stub(:number) { 2 }
15
+ dummy.send(:number_attr).should == 2
16
+ end
17
+
18
+ it "defaults to one when number attribute is not present" do
19
+ dummy.send(:number_attr).should == 1
20
+ end
21
+ end
22
+
23
+ describe "#base_lemma" do
24
+ it "raises an error when the including class doesn't overwrite this method" do
25
+ expect { dummy.send(:base_lemma) }.to raise_error NoMethodError, /overwritten/
26
+ end
27
+
28
+ it "returns whatever base_lemma returns when implemented by the including class" do
29
+ dummy.stub(:base_lemma) { 1 }
30
+ dummy.send(:base_lemma).should == 1
31
+ end
32
+ end
33
+
34
+ describe "#category" do
35
+ it "returns the category name" do
36
+ dummy.stub_chain(:class, :name).and_return('DbSomething')
37
+ dummy.send(:category).should == 'something'
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ describe LLT::DbHandler::Prometheus::Stats do
4
+ let(:stats) { LLT::DbHandler::Prometheus::Stats.new }
5
+
6
+ describe "#count" do
7
+ it "returns the total number of db entries" do
8
+ stats.count.should be_kind_of Fixnum
9
+ end
10
+
11
+ it "returns the number 40000+ entries" do
12
+ # stupid to test a concrete count, but let it scream
13
+ # if we have strangly few entries
14
+ stats.count.should > 40000
15
+ end
16
+ end
17
+
18
+ describe "#all_entries", :slow do
19
+ it "returns an Array of all db objects" do
20
+ stats.all_entries.should be_kind_of Array
21
+ end
22
+
23
+ it "takes a block to be executed on each entry" do
24
+ mapped_entries = stats.all_entries { |entry| entry.kind_of?(Object)}
25
+ uniq_for_easier_test = mapped_entries.uniq
26
+ uniq_for_easier_test.should have(1).item
27
+ uniq_for_easier_test.first.should be_true
28
+ end
29
+ end
30
+
31
+ describe "#lemma_list", :slow do
32
+ let(:lemma_list_entry) { stats.lemma_list.first}
33
+ let(:lemma_format) { '\p{L}[a-z]*' }
34
+
35
+ it "returns an Array of lemma strings" do
36
+ stats.lemma_list.count.should == stats.count
37
+ end
38
+
39
+ it "lemmas are represented with their base form" do
40
+ stats.lemma_list.first.should =~ /^#{lemma_format}$/
41
+ end
42
+
43
+ it "returns a more detailed lemma string with a truthy param" do
44
+ stats.lemma_list(true).first.should =~ /^#{lemma_format}#\d.+$/
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,83 @@
1
+ require 'spec_helper'
2
+
3
+ describe LLT::DbHandler::Prometheus do
4
+ let(:db) { LLT::DbHandler::Prometheus.new }
5
+
6
+ describe "#type" do
7
+ it "returns :prometheus as its type" do
8
+ db.type.should == :prometheus
9
+ end
10
+ end
11
+
12
+ describe "#look_up_stem" do
13
+ let(:query) { { type: :noun, stem: "ros", stem_type: :stem} }
14
+ let(:query2) { { type: :verb, stem: "ama", stem_type: :pr} }
15
+
16
+ let(:failing_query) { { type: :verb, stem: "ama", stem_type: :ppp} }
17
+
18
+ it "searches the db - with a noun" do
19
+ db.look_up_stem(query).should_not be_empty
20
+ end
21
+
22
+ it "searches the db - with a verb" do
23
+ db.look_up_stem(query2).should_not be_empty
24
+ end
25
+
26
+ it "returns an empty array when nothing is found" do
27
+ db.look_up_stem(failing_query).should be_empty
28
+ end
29
+
30
+ # bypassing the query in the two following specs, we're only interested in the
31
+ # conversion process
32
+
33
+ let(:fake_entry1) do
34
+ f = double(nom: "rosa", stem: "ros", inflectable_class: 1, sexus: "f", id: 2)
35
+ f.stub(:type) { :noun }
36
+ f
37
+ end
38
+
39
+ let(:fake_entry2) do
40
+ f = double(nom: "rosa", stem: "ros", inflectable_class: 2, sexus: "f", id: 1)
41
+ f.stub(:type) { :noun }
42
+ f
43
+ end
44
+
45
+ it "returns stem packs" do
46
+ db.stub(:query_db) { [fake_entry1] }
47
+
48
+ result = db.look_up_stem(query)
49
+ result.should have(1).item
50
+ result.first.should be_a LLT::Stem::NounPack
51
+ end
52
+
53
+ it "returns only valid entries if a restriction hash is passed" do
54
+ restrictions = { type: :inflection_class, values: [1] }
55
+ db.stub(:query_db) { [fake_entry1, fake_entry2] }
56
+
57
+ result = db.look_up_stem(query.merge(restrictions: restrictions))
58
+ result.should have(1).item
59
+ result.first.should be_a LLT::Stem::NounPack
60
+ end
61
+ end
62
+
63
+ describe "#direct_lookup" do
64
+ it "searches the db with a simplistic lookup, only a type and a string needed" do
65
+ db.direct_lookup(:adverb, "iam").should have(1).item
66
+ end
67
+ end
68
+
69
+ describe "#stats" do
70
+ it "returns a stats object" do
71
+ db.stats.should_not be_nil
72
+ end
73
+ end
74
+
75
+ %i{ all_entries count lemma_list }.each do |delegated_method|
76
+ describe "##{delegated_method}", :slow do
77
+ it "is delegated to the stats object" do
78
+ db.stats.should_receive(delegated_method)
79
+ db.send(delegated_method)
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+ require 'llt/db_handler/stub'
3
+
4
+ describe LLT::DbHandler::Stub do
5
+ let(:db_stub) { LLT::DbHandler::Stub }
6
+
7
+ describe "#type" do
8
+ it "identifies the db_stub as stub" do
9
+ db_stub.new.type.should == :stub
10
+ end
11
+ end
12
+ describe ".create_stem_stub" do
13
+ it "creates a new stem stub" do
14
+ db_stub.stems.clear
15
+ db_stub.create_stem_stub("test", test: "val")
16
+ db_stub.stems.should have(1).item
17
+ end
18
+
19
+ it "has alias create" do
20
+ db_stub.stems.clear
21
+ db_stub.create("test", test: "val")
22
+ db_stub.stems.should have(1).item
23
+ end
24
+ end
25
+
26
+ describe "#look_up_stem" do
27
+ it "returns an array of db entries" do
28
+ db_stub.create_stem_stub("test", type: :noun, nom: "rosa", stem: "ros", inflection_class: 1)
29
+ result = db_stub.new.look_up_stem(type: :noun, stem_type: :stem, stem: "ros", restrictions: { type: :inflection_class, values: [1] })
30
+ result2 = db_stub.new.look_up_stem(type: :noun, stem_type: :stem, stem: "ros", restrictions: { type: :inflection_class, values: [2] })
31
+ result.should have(1).item
32
+ result.first.should == "test"
33
+
34
+ result2.should be_empty
35
+ end
36
+ end
37
+
38
+ describe "#direct_lookup" do
39
+ it "returns an array of db entries" do
40
+ db_stub.create(:success, type: :adverb, word: "ita")
41
+ result = db_stub.new.direct_lookup(:adverb, "ita")
42
+ result.should have(1).item
43
+ result.first.should == :success
44
+ end
45
+ end
46
+
47
+ describe ".setup" do
48
+ it "creates stub entries as defined in stub_entries.rb" do
49
+ db_stub.stems.clear
50
+ db_stub.setup
51
+ db_stub.stems.should_not be_empty
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe LLT::DbHandler do
4
+ describe ".use" do
5
+ it "returns a new LLT db handler instance by a given type" do
6
+ LLT::DbHandler.use(:prometheus).should be_an_instance_of LLT::DbHandler::Prometheus
7
+ end
8
+
9
+ it "raises an exception when the requested handler is unknown" do
10
+ expect { LLT::DbHandler.use(:undefined_handler) }.to raise_error(ArgumentError)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,21 @@
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
+ require 'llt/db_handler'
16
+
17
+ RSpec.configure do |config|
18
+ config.treat_symbols_as_metadata_keys_with_true_values = true
19
+ config.run_all_when_everything_filtered = true
20
+ config.filter_run :focus
21
+ end
metadata ADDED
@@ -0,0 +1,230 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: llt-db_handler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - LFDM
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.7'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.7'
83
+ - !ruby/object:Gem::Dependency
84
+ name: yard
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: activerecord
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.2'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.2'
111
+ - !ruby/object:Gem::Dependency
112
+ name: llt-core_extensions
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: llt-constants
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: llt-form_builder
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :runtime
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: llt-helpers
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :runtime
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ description: LLT DB Handler
168
+ email:
169
+ - 1986gh@gmail.com
170
+ executables: []
171
+ extensions: []
172
+ extra_rdoc_files: []
173
+ files:
174
+ - ".gitignore"
175
+ - ".rspec"
176
+ - Gemfile
177
+ - LICENSE.txt
178
+ - README.md
179
+ - Rakefile
180
+ - lib/llt/db_handler.rb
181
+ - lib/llt/db_handler/common_db.rb
182
+ - lib/llt/db_handler/prometheus.rb
183
+ - lib/llt/db_handler/prometheus/db/database.yml
184
+ - lib/llt/db_handler/prometheus/db/models.rb
185
+ - lib/llt/db_handler/prometheus/db/prometheus_stems.dump
186
+ - lib/llt/db_handler/prometheus/db_to_lemma.rb
187
+ - lib/llt/db_handler/prometheus/stats.rb
188
+ - lib/llt/db_handler/stub.rb
189
+ - lib/llt/db_handler/stub/stub_entries.rb
190
+ - lib/llt/db_handler/tasks.rb
191
+ - lib/llt/db_handler/version.rb
192
+ - llt-db_handler.gemspec
193
+ - spec/lib/llt/db_handler/prometheus/db_to_lemma_spec.rb
194
+ - spec/lib/llt/db_handler/prometheus/stats_spec.rb
195
+ - spec/lib/llt/db_handler/prometheus_spec.rb
196
+ - spec/lib/llt/db_handler/stub_spec.rb
197
+ - spec/lib/llt/db_handler_spec.rb
198
+ - spec/spec_helper.rb
199
+ homepage: ''
200
+ licenses:
201
+ - MIT
202
+ metadata: {}
203
+ post_install_message:
204
+ rdoc_options: []
205
+ require_paths:
206
+ - lib
207
+ required_ruby_version: !ruby/object:Gem::Requirement
208
+ requirements:
209
+ - - ">="
210
+ - !ruby/object:Gem::Version
211
+ version: '0'
212
+ required_rubygems_version: !ruby/object:Gem::Requirement
213
+ requirements:
214
+ - - ">="
215
+ - !ruby/object:Gem::Version
216
+ version: '0'
217
+ requirements: []
218
+ rubyforge_project:
219
+ rubygems_version: 2.1.5
220
+ signing_key:
221
+ specification_version: 4
222
+ summary: LLT DB Handler
223
+ test_files:
224
+ - spec/lib/llt/db_handler/prometheus/db_to_lemma_spec.rb
225
+ - spec/lib/llt/db_handler/prometheus/stats_spec.rb
226
+ - spec/lib/llt/db_handler/prometheus_spec.rb
227
+ - spec/lib/llt/db_handler/stub_spec.rb
228
+ - spec/lib/llt/db_handler_spec.rb
229
+ - spec/spec_helper.rb
230
+ has_rdoc: