Module: Wgit::Model
- Defined in:
- lib/wgit/model.rb
Overview
Module used to build the Database collection objects, forming a data model.
The models produced are Hash like and therefore DB agnostic. Each model
will contain a unique field used for searching and avoiding duplicates,
this is typically a url
field. Also contained in the model are the
search fields used in Database and Document #search calls.
Constant Summary collapse
- DEFAULT_SEARCH_FIELDS =
The default search fields used in Database and Document #search calls. The number of matches for each field is multiplied by the field weight, the total is the search score, used to sort the search results. Call Wgit::Model.set_default_search_fields` to revert to default.
{ title: 2, description: 2, keywords: 2, text: 1 }.freeze
Class Attribute Summary collapse
-
.include_doc_html ⇒ Object
Whether or not to include the Document#html in the #document model.
-
.include_doc_score ⇒ Object
Whether or not to include the Document#score in the #document model.
-
.search_fields ⇒ Object
readonly
The search fields used in Database and Document #search calls.
Class Method Summary collapse
-
.common_insert_data ⇒ Hash
Common fields when inserting a record into the DB.
-
.common_update_data ⇒ Hash
Common fields when updating a record in the DB.
-
.document(doc) ⇒ Hash
The data model for a Wgit::Document collection object.
-
.select_bson_types(model_hash) ⇒ Hash
Returns the model having removed non bson types (for use with MongoDB).
-
.set_default_search_fields(db = nil) ⇒ Hash<Symbol, Integer>
Sets the search fields used in Database and Document #search calls.
-
.set_search_fields(fields, db = nil) ⇒ Hash<Symbol, Integer>
Sets the search fields used in Database and Document #search calls.
-
.url(url) ⇒ Hash
The data model for a Wgit::Url collection object and for an embedded 'url' inside a Wgit::Document collection object.
Class Attribute Details
.include_doc_html ⇒ Object
Whether or not to include the Document#html in the #document model.
41 42 43 |
# File 'lib/wgit/model.rb', line 41 def include_doc_html @include_doc_html end |
.include_doc_score ⇒ Object
Whether or not to include the Document#score in the #document model.
44 45 46 |
# File 'lib/wgit/model.rb', line 44 def include_doc_score @include_doc_score end |
.search_fields ⇒ Object (readonly)
The search fields used in Database and Document #search calls. A custom setter method is also provided for changing these fields.
38 39 40 |
# File 'lib/wgit/model.rb', line 38 def search_fields @search_fields end |
Class Method Details
.common_insert_data ⇒ Hash
Common fields when inserting a record into the DB.
140 141 142 143 144 145 |
# File 'lib/wgit/model.rb', line 140 def self.common_insert_data { date_added: Wgit::Utils.time_stamp, date_modified: Wgit::Utils.time_stamp } end |
.common_update_data ⇒ Hash
Common fields when updating a record in the DB.
150 151 152 153 154 |
# File 'lib/wgit/model.rb', line 150 def self.common_update_data { date_modified: Wgit::Utils.time_stamp } end |
.document(doc) ⇒ Hash
The data model for a Wgit::Document collection object.
The unique field for this model is model['url']['url']
.
126 127 128 129 130 131 132 133 134 135 |
# File 'lib/wgit/model.rb', line 126 def self.document(doc) raise "doc must respond_to? :to_h" unless doc.respond_to?(:to_h) model = doc.to_h( include_html: @include_doc_html, include_score: @include_doc_score ) model["url"] = url(doc.url) # Expand Url String into full object. select_bson_types(model) end |
.select_bson_types(model_hash) ⇒ Hash
Returns the model having removed non bson types (for use with MongoDB).
160 161 162 |
# File 'lib/wgit/model.rb', line 160 def self.select_bson_types(model_hash) model_hash.select { |_k, v| v.respond_to?(:bson_type) } end |
.set_default_search_fields(db = nil) ⇒ Hash<Symbol, Integer>
Sets the search fields used in Database and Document #search calls.
If the given db (database) param responds to #search_fields= then it will be called and given the fields to set. This should perform whatever the database adapter needs in order to search using the given fields e.g. creating a search index. Calling the DB enables the search_fields to be set globally within Wgit by one method call, this one.
102 103 104 |
# File 'lib/wgit/model.rb', line 102 def self.set_default_search_fields(db = nil) set_search_fields(DEFAULT_SEARCH_FIELDS, db) end |
.set_search_fields(fields, db = nil) ⇒ Hash<Symbol, Integer>
Sets the search fields used in Database and Document #search calls.
You can pass the fields as an Array of Symbols which gives each field a weight of 1 meaning all fields are considered of equal value. Or you can pass a Hash of Symbol => Int and specify the weights yourself, allowing you to customise the search rankings.
Use like:
Wgit::Model.set_search_fields [:title, :text], db
=> { title: 1, text: 1 }
Wgit::Model.set_search_fields {title: 2, text: 1}, db
=> { title: 2, text: 1 }
If the given db (database) param responds to #search_fields= then it will be called and given the fields to set. This should perform whatever the database adapter needs in order to search using the given fields e.g. creating a search index. Calling the DB enables the search_fields to be set globally within Wgit by one method call, this one.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/wgit/model.rb', line 74 def self.set_search_fields(fields, db = nil) # We need a Hash of fields => weights (Symbols => Integers). case fields when Array # of Strings/Symbols. fields = fields.map { |field| [field.to_sym, 1] } when Hash # of Strings/Symbols and Integers. fields = fields.map { |field, weight| [field.to_sym, weight.to_i] } else raise "fields must be an Array or Hash, not a #{fields.class}" end @search_fields = fields.to_h db.search_fields = @search_fields if db.respond_to?(:search_fields=) @search_fields end |
.url(url) ⇒ Hash
The data model for a Wgit::Url collection object and for an embedded 'url' inside a Wgit::Document collection object.
The unique field for this model is model['url']
.
113 114 115 116 117 118 |
# File 'lib/wgit/model.rb', line 113 def self.url(url) raise "url must respond_to? :to_h" unless url.respond_to?(:to_h) model = url.to_h select_bson_types(model) end |