elastic_record 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -35,7 +35,10 @@ module ElasticRecord
35
35
  end
36
36
 
37
37
  def to_a
38
- @records ||= klass.find(to_ids)
38
+ @records ||= begin
39
+ scope = select_values.any? ? klass.select(select_values) : klass
40
+ scope.find(to_ids)
41
+ end
39
42
  end
40
43
 
41
44
  def to_ids
@@ -60,6 +60,19 @@ module ElasticRecord
60
60
  clone.offset! value
61
61
  end
62
62
 
63
+ def select!(value)
64
+ self.select_values += Array.wrap(value)
65
+ self
66
+ end
67
+
68
+ def select(value, &block)
69
+ if block_given?
70
+ to_a.select(&block)
71
+ else
72
+ clone.select! value
73
+ end
74
+ end
75
+
63
76
  def facet!(name_or_facet, options = {})
64
77
  if name_or_facet.is_a?(String)
65
78
  self.facet_values += [arelastic.facet[name_or_facet].terms(name_or_facet, options)]
@@ -1,6 +1,6 @@
1
1
  module ElasticRecord
2
2
  class Relation
3
- MULTI_VALUE_METHODS = [:extending, :filter, :facet, :order]
3
+ MULTI_VALUE_METHODS = [:extending, :facet, :filter, :order, :select]
4
4
  SINGLE_VALUE_METHODS = [:query, :limit, :offset]
5
5
  end
6
6
  end
@@ -128,6 +128,20 @@ class ElasticRecord::Relation::SearchMethodsTest < MiniTest::Spec
128
128
  assert_equal expected, relation.as_elastic['sort']
129
129
  end
130
130
 
131
+ def test_select
132
+ selectable_klass = Widget.anon do
133
+ def self.select(values)
134
+ @latest_select = values
135
+ self
136
+ end
137
+ end
138
+
139
+ relation = selectable_klass.elastic_relation.select 'foo'
140
+ relation.to_a
141
+
142
+ assert_equal ['foo'], selectable_klass.instance_variable_get('@latest_select')
143
+ end
144
+
131
145
  def test_extending_with_block
132
146
  relation.extending! do
133
147
  def foo
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elastic_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-09 00:00:00.000000000 Z
12
+ date: 2012-08-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: arelastic