gummi 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. data/.gitignore +1 -0
  2. data/gummi.gemspec +7 -6
  3. data/lib/gummi.rb +32 -26
  4. data/lib/gummi/api.rb +3 -1
  5. data/lib/gummi/db_layer/default_index.rb +15 -0
  6. data/lib/gummi/db_layer/document.rb +206 -0
  7. data/lib/gummi/db_layer/document/attributes.rb +40 -0
  8. data/lib/gummi/db_layer/document/object.rb +15 -0
  9. data/lib/gummi/db_layer/document/search/filtered.rb +42 -0
  10. data/lib/gummi/db_layer/document/search/raw.rb +12 -0
  11. data/lib/gummi/db_layer/document/search/result.rb +34 -0
  12. data/lib/gummi/db_layer/document/search/searching.rb +51 -0
  13. data/lib/gummi/db_layer/fields/boolean.rb +13 -0
  14. data/lib/gummi/db_layer/fields/integer.rb +16 -0
  15. data/lib/gummi/db_layer/fields/keyword.rb +15 -0
  16. data/lib/gummi/db_layer/fields/ngram_and_plain.rb +20 -0
  17. data/lib/gummi/db_layer/fields/path_hierarchy.rb +15 -0
  18. data/lib/gummi/db_layer/fields/positive_integer.rb +21 -0
  19. data/lib/gummi/db_layer/fields/sanitized_string.rb +30 -0
  20. data/lib/gummi/db_layer/fields/string.rb +17 -0
  21. data/lib/gummi/db_layer/fields/time.rb +17 -0
  22. data/lib/gummi/db_layer/index.rb +150 -0
  23. data/lib/gummi/entity_layer/entity.rb +22 -0
  24. data/lib/gummi/errors.rb +7 -0
  25. data/lib/gummi/repository_layer/repository.rb +39 -0
  26. data/lib/gummi/repository_layer/repository/result.rb +42 -0
  27. data/lib/gummi/version.rb +1 -1
  28. data/lib/repobahn/repository.rb +25 -33
  29. data/lib/repobahn/repository/active_record.rb +17 -0
  30. data/spec/fixtures/admin/auto.rb +6 -0
  31. data/spec/fixtures/admin/cars.rb +12 -0
  32. data/spec/fixtures/admin/countries.rb +9 -0
  33. data/spec/fixtures/admin/country.rb +6 -0
  34. data/spec/fixtures/admin/db/country.rb +7 -0
  35. data/spec/fixtures/admin/db/vehicle.rb +7 -0
  36. data/spec/fixtures/cities.rb +7 -0
  37. data/spec/fixtures/city.rb +6 -0
  38. data/spec/fixtures/db/animal.rb +9 -0
  39. data/spec/fixtures/db/boat.rb +9 -0
  40. data/spec/fixtures/db/car.rb +9 -0
  41. data/spec/fixtures/db/city.rb +8 -0
  42. data/spec/fixtures/db/enemy.rb +10 -0
  43. data/spec/fixtures/db/game.rb +7 -0
  44. data/spec/fixtures/db/person.rb +15 -0
  45. data/spec/fixtures/db/rating.rb +11 -0
  46. data/spec/fixtures/db/ship.rb +18 -0
  47. data/spec/{models → fixtures}/people.rb +6 -2
  48. data/spec/{models → fixtures}/person.rb +3 -2
  49. data/spec/lib/gummi/db_layer/document_spec.rb +124 -0
  50. data/spec/lib/gummi/{entity_spec.rb → entity_layer/entity_spec.rb} +3 -1
  51. data/spec/lib/gummi/repository_layer/repository_spec.rb +63 -0
  52. data/spec/lib/repobahn/repository_spec.rb +72 -0
  53. data/spec/spec_helper.rb +37 -9
  54. metadata +87 -37
  55. data/lib/gummi/default_index.rb +0 -13
  56. data/lib/gummi/document.rb +0 -139
  57. data/lib/gummi/document/attributes.rb +0 -28
  58. data/lib/gummi/document/object.rb +0 -12
  59. data/lib/gummi/document/search/filtered.rb +0 -39
  60. data/lib/gummi/document/search/raw.rb +0 -9
  61. data/lib/gummi/document/search/result.rb +0 -25
  62. data/lib/gummi/document/search/searching.rb +0 -45
  63. data/lib/gummi/entity.rb +0 -20
  64. data/lib/gummi/fields/boolean.rb +0 -10
  65. data/lib/gummi/fields/integer.rb +0 -14
  66. data/lib/gummi/fields/keyword.rb +0 -13
  67. data/lib/gummi/fields/ngram_and_plain.rb +0 -18
  68. data/lib/gummi/fields/path_hierarchy.rb +0 -13
  69. data/lib/gummi/fields/positive_integer.rb +0 -19
  70. data/lib/gummi/fields/sanitized_string.rb +0 -28
  71. data/lib/gummi/fields/string.rb +0 -15
  72. data/lib/gummi/fields/time.rb +0 -15
  73. data/lib/gummi/index.rb +0 -146
  74. data/lib/gummi/repository.rb +0 -38
  75. data/lib/gummi/repository/result.rb +0 -30
  76. data/spec/lib/gummi/document_spec.rb +0 -73
  77. data/spec/lib/gummi/repository/result_spec.rb +0 -18
  78. data/spec/lib/gummi/repository_spec.rb +0 -81
  79. data/spec/models/db/person.rb +0 -15
@@ -1,38 +0,0 @@
1
-
2
- module Gummi
3
- module Repository
4
- extend ActiveSupport::Concern
5
-
6
- included do
7
- include Repobahn::Repository
8
- after_conversion :set_id_and_version
9
- end
10
-
11
-
12
- module ClassMethods
13
-
14
- def get(id)
15
- record = db_model.get id
16
- to_entity_from_db record if record
17
- end
18
-
19
- def search(&block)
20
- filtered_search = db_model.new_filtered_search
21
- yield filtered_search
22
- result = filtered_search.execute
23
- Repository::Result.new(result, self, filtered_search.per_page, filtered_search.page)
24
- end
25
-
26
- def overwrite(entity)
27
- return false unless entity.valid?
28
- db_record = db_model.new(entity.attributes)
29
- db_record.overwrite
30
- end
31
-
32
- def set_id_and_version(entity, db)
33
- entity.id = db.id
34
- entity.version = db.version
35
- end
36
- end
37
- end
38
- end
@@ -1,30 +0,0 @@
1
- module Gummi
2
- module Repository
3
- class Result
4
-
5
- attr_reader :took, :total, :hits
6
-
7
- def initialize(db_result, repository, per_page, page)
8
- @took = db_result.took
9
- @total = db_result.total
10
- @hits = db_result.hits
11
- @facets = db_result.facets
12
- @db_records = db_result.records
13
- @repository = repository
14
- @per_page = per_page
15
- @page = page
16
- end
17
-
18
- def facets
19
- @facets.each_with_object({}) do |(name, content), facets|
20
- facets[name] = Hash[content["terms"].collect{|term| term.values}]
21
- end
22
- end
23
-
24
- def records
25
- entities = Array(@repository.to_entity_from_db(@db_records)) if @db_records
26
- Leaflet::Collection.new entities, page: @page, total: total, per_page: @per_page
27
- end
28
- end
29
- end
30
- end
@@ -1,73 +0,0 @@
1
- require 'spec_helper'
2
-
3
- class GummiDummyDocument
4
- include Gummi::Document
5
-
6
- attribute :test, String
7
- end
8
-
9
- describe Gummi::Document do
10
-
11
- context "included" do
12
- it "adds accessors for id and version" do
13
- m = GummiDummyDocument.new
14
- m.respond_to?(:version).should be_true
15
- m.respond_to?(:id).should be_true
16
- end
17
-
18
- it "adds attributes methods" do
19
- m = GummiDummyDocument.new(test: 'hello')
20
- m.test.should == 'hello'
21
- end
22
- end
23
-
24
- context "attributes" do
25
-
26
- context "date times" do
27
- it "coerces from elastics strings to real Time" do
28
- time = Time.now
29
- person = DB::Person.new(born_at: time)
30
- person.overwrite
31
- person_from_es = DB::Person.get person.id
32
- person_from_es.born_at.should be_a Time
33
- end
34
-
35
- it "always stores time in UTC" do
36
- time = Time.now.in_time_zone 'CET'
37
-
38
- person = DB::Person.new(born_at: time)
39
- person.born_at.zone.should == 'UTC'
40
- end
41
- end
42
-
43
- context "computed_attributes" do
44
-
45
- let(:person) { DB::Person.new}
46
-
47
- it "adds them to the attributes hash" do
48
- person.name = "olof palme"
49
- person.attributes[:computed_name].should == 'OLOF PALME'
50
- end
51
-
52
- it "computes every time" do
53
- person.name = "olof palme"
54
- person.computed_name.should == person.name.upcase
55
- person.name = "carl bildt"
56
- person.computed_name.should == person.name.upcase
57
- end
58
-
59
- it "provides a mapping" do
60
- DB::Person.mapping.should include(:computed_name => {:type=>"string"})
61
- end
62
- end
63
- end
64
-
65
- context 'getting from elastic' do
66
- let (:person) { DB::Person.create(name: 'Buzz Lightyear') }
67
-
68
- it "returns an instance of the db_model" do
69
- person_from_es = DB::Person.get(person.id)
70
- person_from_es.should be_a DB::Person
71
- end
72
- end
73
- end
@@ -1,18 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Gummi::Repository::Result do
4
-
5
- describe "#facets" do
6
- it "returns the relevant terms with counts" do
7
- db_result = OpenStruct.new(facets: {"categories"=>
8
- {"_type"=>"terms",
9
- "missing"=>0,
10
- "total"=>3,
11
- "other"=>0,
12
- "terms"=>[{"term"=>"design", "count"=>2}, {"term"=>"silver", "count"=>1}]}})
13
-
14
- result = Gummi::Repository::Result.new(db_result, nil, nil, nil)
15
- result.facets.should == {"categories" => {"design" => 2, "silver" => 1}}
16
- end
17
- end
18
- end
@@ -1,81 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Gummi::Repository do
4
-
5
- let(:repository_model) { People }
6
- let(:entity_model) { Person }
7
- let(:db_model) { DB::Person }
8
-
9
- describe ".entity_model" do
10
- it "should default to singular version of repository_model" do
11
- People.entity_model.should == Person
12
- end
13
- end
14
-
15
- describe ".db_model" do
16
- it "should default to singular version of repository_model in the DB namespace" do
17
- People.db_model.should == DB::Person
18
- end
19
- end
20
-
21
- context "converting from db to entity" do
22
-
23
- let (:db_person) { DB::Person.new(name: 'Buzz Lightyear') }
24
-
25
- it "should map the attributes from db to entity" do
26
- person = People.to_entity_from_db(db_person)
27
- person.name.should == 'Buzz Lightyear'
28
- end
29
-
30
- it "should run hook for after_conversion" do
31
- person = People.to_entity_from_db(db_person)
32
- person.converted_name.should == db_person.name.reverse
33
- end
34
- end
35
-
36
- describe ".get" do
37
- context "existing record" do
38
-
39
- let (:db_person) { DB::Person.create(name: 'Buzz Lightyear') }
40
-
41
- it "should return an entity" do
42
- person = People.get(db_person.id)
43
- person.id.should == db_person.id
44
- end
45
- end
46
-
47
- context "missing record" do
48
- it "returns nil" do
49
- person = People.get('missing_id')
50
- person.should be_nil
51
- end
52
- end
53
- end
54
-
55
- describe ".search" do
56
-
57
- before(:each) do
58
- DB::Person.create(name: 'Buzz Lightyear')
59
- DB::Person.create(name: 'Woody')
60
- Gummi::DefaultIndex.refresh
61
- end
62
-
63
- it "should find the correct documents" do
64
- result = People.search do |search|
65
- search.query_string = "Woody"
66
- end
67
- result.total.should == 1
68
- result.records.first.name.should == "Woody"
69
- end
70
-
71
- it "should convert the result to entities" do
72
- result = People.search do |search|
73
- search.query_string = "Woody"
74
- end
75
- woody = result.records.first
76
- woody.converted_name.should == 'ydooW'
77
- woody.should be_a Person
78
- end
79
- end
80
-
81
- end
@@ -1,15 +0,0 @@
1
- # example db model
2
-
3
- module DB
4
- class Person
5
- include Gummi::Document
6
-
7
- attribute :name, Gummi::Fields::String
8
- attribute :computed_name, Gummi::Fields::String
9
- attribute :born_at, Gummi::Fields::Time
10
-
11
- def computed_name
12
- name.upcase if name
13
- end
14
- end
15
- end