stretchy-model 0.3.0 → 0.5.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.
- checksums.yaml +4 -4
- data/.rspec +1 -1
- data/README.md +40 -94
- data/Rakefile +92 -0
- data/lib/stretchy/associations.rb +155 -15
- data/lib/stretchy/attributes/transformers/keyword_transformer.rb +85 -0
- data/lib/stretchy/attributes/type/array.rb +15 -0
- data/lib/stretchy/attributes/type/hash.rb +17 -0
- data/lib/stretchy/attributes/type/keyword.rb +11 -0
- data/lib/stretchy/attributes/type/text.rb +12 -0
- data/lib/stretchy/attributes.rb +30 -0
- data/lib/stretchy/common.rb +2 -3
- data/lib/stretchy/delegation/gateway_delegation.rb +7 -1
- data/lib/stretchy/model/serialization.rb +1 -0
- data/lib/stretchy/querying.rb +6 -5
- data/lib/stretchy/record.rb +8 -9
- data/lib/stretchy/relation.rb +11 -17
- data/lib/stretchy/relations/aggregation_methods.rb +758 -0
- data/lib/stretchy/relations/finder_methods.rb +21 -3
- data/lib/stretchy/relations/merger.rb +11 -7
- data/lib/stretchy/relations/query_builder.rb +48 -11
- data/lib/stretchy/relations/query_methods.rb +76 -38
- data/lib/stretchy/shared_scopes.rb +1 -1
- data/lib/stretchy/utils.rb +21 -0
- data/lib/stretchy/version.rb +1 -3
- data/lib/stretchy.rb +21 -11
- data/lib/stretchy_model.rb +9 -0
- metadata +39 -5
- data/lib/active_model/type/array.rb +0 -13
- data/lib/active_model/type/hash.rb +0 -15
@@ -0,0 +1,30 @@
|
|
1
|
+
module Stretchy
|
2
|
+
module Attributes
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
def [](attribute)
|
6
|
+
self.send(attribute)
|
7
|
+
end
|
8
|
+
|
9
|
+
def []=(attribute, value)
|
10
|
+
self.send("#{attribute}=", value)
|
11
|
+
end
|
12
|
+
|
13
|
+
def inspect
|
14
|
+
"#<#{self.class.name} #{attributes.map { |k,v| "#{k}: #{v.blank? ? 'nil' : v}" }.join(', ')}>"
|
15
|
+
end
|
16
|
+
|
17
|
+
class_methods do
|
18
|
+
def inspect
|
19
|
+
"#<#{self.name} #{attribute_types.map { |k,v| "#{k}: #{v.type}" }.join(', ')}>"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.register!
|
24
|
+
ActiveModel::Type.register(:array, Stretchy::Attributes::Type::Array)
|
25
|
+
ActiveModel::Type.register(:hash, Stretchy::Attributes::Type::Hash)
|
26
|
+
ActiveModel::Type.register(:keyword, Stretchy::Attributes::Type::Keyword)
|
27
|
+
ActiveModel::Type.register(:text, Stretchy::Attributes::Type::Text)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/lib/stretchy/common.rb
CHANGED
@@ -2,11 +2,10 @@ module Stretchy
|
|
2
2
|
module Common
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
|
-
def
|
6
|
-
|
5
|
+
def highlights_for(attribute)
|
6
|
+
highlights[attribute.to_s]
|
7
7
|
end
|
8
8
|
|
9
|
-
|
10
9
|
class_methods do
|
11
10
|
|
12
11
|
# Set the default sort key to be used in sort operations
|
@@ -28,11 +28,17 @@ module Stretchy
|
|
28
28
|
if @index_name.respond_to?(:call)
|
29
29
|
@index_name.call
|
30
30
|
else
|
31
|
-
@index_name || base_class.model_name.collection
|
31
|
+
@index_name || base_class.model_name.collection.parameterize.underscore
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
35
|
+
def reload_gateway_configuration!
|
36
|
+
@gateway = nil
|
37
|
+
end
|
38
|
+
|
35
39
|
def gateway(&block)
|
40
|
+
reload_gateway_configuration! if @gateway && @gateway.client != Stretchy.configuration.client
|
41
|
+
|
36
42
|
@gateway ||= Stretchy::Repository.create(client: Stretchy.configuration.client, index_name: index_name, klass: base_class)
|
37
43
|
block.arity < 1 ? @gateway.instance_eval(&block) : block.call(@gateway) if block_given?
|
38
44
|
@gateway
|
@@ -9,6 +9,7 @@ module Stretchy
|
|
9
9
|
|
10
10
|
def deserialize(document)
|
11
11
|
attribs = ActiveSupport::HashWithIndifferentAccess.new(document['_source']).deep_symbolize_keys
|
12
|
+
attribs[:_highlights] = document["highlight"] if document["highlight"]
|
12
13
|
_id = __get_id_from_document(document)
|
13
14
|
attribs[:id] = _id if _id
|
14
15
|
klass.new attribs
|
data/lib/stretchy/querying.rb
CHANGED
@@ -1,11 +1,12 @@
|
|
1
1
|
module Stretchy
|
2
2
|
module Querying
|
3
3
|
delegate :first, :first!, :last, :last!, :exists?, :has_field, :any?, :many?, to: :all
|
4
|
-
delegate :order, :limit, :size, :sort, :
|
5
|
-
delegate :or_filter, :
|
6
|
-
delegate
|
7
|
-
|
8
|
-
delegate :
|
4
|
+
delegate :order, :limit, :size, :sort, :rewhere, :eager_load, :includes, :create_with, :none, :unscope, to: :all
|
5
|
+
delegate :or_filter, :fields, :source, :highlight, to: :all
|
6
|
+
delegate *Stretchy::Relations::AggregationMethods::AGGREGATION_METHODS, to: :all
|
7
|
+
|
8
|
+
delegate :skip_callbacks, :routing, :search_options, to: :all
|
9
|
+
delegate :must, :must_not, :should, :where_not, :where, :filter_query, :query_string, :regexp, to: :all
|
9
10
|
|
10
11
|
def fetch_results(es)
|
11
12
|
unless es.count?
|
data/lib/stretchy/record.rb
CHANGED
@@ -14,11 +14,6 @@ module Stretchy
|
|
14
14
|
include ActiveModel::Conversion
|
15
15
|
include ActiveModel::Serialization
|
16
16
|
include ActiveModel::Serializers::JSON
|
17
|
-
include ActiveModel::Validations
|
18
|
-
include ActiveModel::Validations::Callbacks
|
19
|
-
extend ActiveModel::Callbacks
|
20
|
-
|
21
|
-
|
22
17
|
|
23
18
|
include Stretchy::Model::Callbacks
|
24
19
|
include Stretchy::Indexing::Bulk
|
@@ -28,6 +23,8 @@ module Stretchy
|
|
28
23
|
include Stretchy::Common
|
29
24
|
include Stretchy::Scoping
|
30
25
|
include Stretchy::Utils
|
26
|
+
include Stretchy::SharedScopes
|
27
|
+
include Stretchy::Attributes
|
31
28
|
|
32
29
|
extend Stretchy::Delegation::DelegateCache
|
33
30
|
extend Stretchy::Querying
|
@@ -44,11 +41,13 @@ module Stretchy
|
|
44
41
|
# overriden by #size
|
45
42
|
default_size 10000
|
46
43
|
|
47
|
-
|
44
|
+
attr_accessor :highlights
|
45
|
+
|
46
|
+
def initialize(attributes = {})
|
47
|
+
@highlights = attributes.delete(:_highlights)
|
48
|
+
super(attributes)
|
49
|
+
end
|
48
50
|
|
49
|
-
def initialize(attributes = {})
|
50
|
-
self.assign_attributes(attributes) if attributes
|
51
|
-
super()
|
52
51
|
end
|
53
52
|
|
54
53
|
end
|
data/lib/stretchy/relation.rb
CHANGED
@@ -3,20 +3,16 @@ module Stretchy
|
|
3
3
|
# It provides methods for querying and manipulating the documents.
|
4
4
|
class Relation
|
5
5
|
|
6
|
-
# These methods can accept multiple values.
|
7
|
-
MULTI_VALUE_METHODS = [:order, :where, :or_filter, :filter, :bind, :extending, :unscope, :skip_callbacks]
|
8
|
-
|
9
|
-
# These methods can accept a single value.
|
10
|
-
SINGLE_VALUE_METHODS = [:limit, :offset, :routing, :size]
|
11
|
-
|
12
6
|
# These methods cannot be used with the `delete_all` method.
|
13
7
|
INVALID_METHODS_FOR_DELETE_ALL = [:limit, :offset]
|
14
8
|
|
15
|
-
# All value methods.
|
16
|
-
VALUE_METHODS = MULTI_VALUE_METHODS + SINGLE_VALUE_METHODS
|
17
|
-
|
18
9
|
# Include modules.
|
19
|
-
include Relations::FinderMethods,
|
10
|
+
include Relations::FinderMethods,
|
11
|
+
Relations::SpawnMethods,
|
12
|
+
Relations::QueryMethods,
|
13
|
+
Relations::AggregationMethods,
|
14
|
+
Relations::SearchOptionMethods,
|
15
|
+
Delegation
|
20
16
|
|
21
17
|
# Getters.
|
22
18
|
attr_reader :klass, :loaded
|
@@ -49,14 +45,13 @@ module Stretchy
|
|
49
45
|
#
|
50
46
|
# @return [Array] The results of the relation.
|
51
47
|
def to_a
|
52
|
-
|
53
48
|
load
|
54
49
|
@records
|
55
50
|
end
|
56
51
|
alias :results :to_a
|
57
52
|
|
58
53
|
def response
|
59
|
-
|
54
|
+
results.response
|
60
55
|
end
|
61
56
|
|
62
57
|
# Returns the results of the relation as a JSON object.
|
@@ -64,7 +59,7 @@ module Stretchy
|
|
64
59
|
# @param options [Hash] The options to pass to the `as_json` method.
|
65
60
|
# @return [Hash] The results of the relation as a JSON object.
|
66
61
|
def as_json(options = nil)
|
67
|
-
|
62
|
+
results.as_json(options)
|
68
63
|
end
|
69
64
|
|
70
65
|
# Returns the Elasticsearch query for the relation.
|
@@ -98,7 +93,6 @@ module Stretchy
|
|
98
93
|
# @return [Relation] The relation object.
|
99
94
|
def load
|
100
95
|
exec_queries unless loaded?
|
101
|
-
|
102
96
|
self
|
103
97
|
end
|
104
98
|
alias :fetch :load
|
@@ -146,8 +140,8 @@ module Stretchy
|
|
146
140
|
begin
|
147
141
|
entries = to_a.results.take([size_value.to_i + 1, 11].compact.min).map!(&:inspect)
|
148
142
|
message = {}
|
149
|
-
message = {total:
|
150
|
-
message.merge!(aggregations:
|
143
|
+
message = {total: results.total, max: results.total}
|
144
|
+
message.merge!(aggregations: response.aggregations.keys) unless response.aggregations.nil?
|
151
145
|
message = message.each_pair.collect { |k,v| "#{k}: #{v}" }
|
152
146
|
message.unshift entries.join(', ') unless entries.size.zero?
|
153
147
|
"#<#{self.class.name} #{message.join(', ')}>"
|
@@ -162,7 +156,7 @@ module Stretchy
|
|
162
156
|
#
|
163
157
|
# @return [QueryBuilder] The query builder for the relation.
|
164
158
|
def query_builder
|
165
|
-
Relations::QueryBuilder.new(values)
|
159
|
+
Relations::QueryBuilder.new(values, klass.attribute_types)
|
166
160
|
end
|
167
161
|
|
168
162
|
end
|