get 0.1.3.0 → 0.1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 65fd187b7fdf6d925755baa22b7a7ce32cebc00e
4
- data.tar.gz: 48fa9d577da086b0a6f90986106aa1c9581e8b40
3
+ metadata.gz: 2d18fd6e0ff97b254927a5dda39633fd3074cc4a
4
+ data.tar.gz: 93fcab2142e08cdc90f911002211bf519b34b2c5
5
5
  SHA512:
6
- metadata.gz: 11e0607221f9bb3c6c42770f55eef24d43a5f2bac228fd6e876bccf648394a28e3ed815005a3179930d88077b74f2a7e403c78bd04e775943986ab3bdd66aec9
7
- data.tar.gz: 80f305335e0721add5e78c2879849a1e6fed6eaedc4d2cf2fd4ae1a6d240025923dc4e7d3a14fea37c346b86b3ffef66c99061793de2b8e47c45c02abff6df0f
6
+ metadata.gz: 06a1e30bbe933922db557da9756cca4a273532952b4d3048d15beb536ae48dd537f59ac7a40937dcd0f831ef14f459f8e4c99773c16f9ae946921289803859e4
7
+ data.tar.gz: 620749e0087939ef868d95e6546cb8ac8dab40849d89ea529fb5dbe043a1c7e902002f8d76f7603d91c5e76c4558201e00a535223e14647b7e43cbd0991a59a5
data/README.md CHANGED
@@ -139,24 +139,15 @@ but UserFromEmployer will throw `Get::Errors::InvalidAncestry`.
139
139
 
140
140
  ## Entities
141
141
 
142
- Ironically, one of the "features" of Get is its removal of the ability to query associations from the query response object.
142
+ Ironically, one of the "features" of Get is its removal of the ability to query associations from the ORM response object.
143
143
  This choice was made to combat query pollution throughout the app, particularly in the view layer.
144
144
 
145
- To achieve this, Get returns **entities** instead of ORM objects (`ActiveRecord::Base`, etc.).
146
- These entity classes are generated at runtime with names appropriate to their contents.
147
- You can also register your own entities in the Get config.
148
-
149
- ```ruby
150
- >> result = Get::UserById.run(user.id)
151
- >> result.class.name
152
- >> "Get::Entities::GetUser"
153
- ```
145
+ To achieve this, Get returns Horza Entities instead of ORM objects (`ActiveRecord::Base`, etc.).
146
+ See the [Horza Readme](https://github.com/onfido/horza) for instructions on how to create your own entities.
154
147
 
155
148
  Individual entities will have all attributes accessible via dot notation and hash notation, but attempts to get associations will fail.
156
149
  Collections have all of the common enumerator methods: `first`, `last`, `each`, and `[]`.
157
150
 
158
- Dynamically generated Get::Entities are prefixed with `Get` to avoid colliding with your ORM objects.
159
-
160
151
  ## Testing
161
152
 
162
153
  A big motivation for this library is to make testing database queries easier.
@@ -178,7 +169,7 @@ def index
178
169
  end
179
170
  ```
180
171
 
181
- The above methods do the exact same thing. Cool, let's test them:
172
+ The above methods do pretty much the exact same thing (the only difference is that the second example returns a Horza Entity instead of an ORM object). Cool, let's test them:
182
173
 
183
174
  ```ruby
184
175
  # sportscars_controller.rb
@@ -202,7 +193,7 @@ describe SportscarsController, type: :controller do
202
193
 
203
194
  context 'Get' do
204
195
  let(:user) { FactoryGirl.build_stubbed(:user, employer: employer) }
205
- let(:sportscars) { 3.times { FactoryGirl.build_stubbed(:sportscars) } }
196
+ let(:sportscars) { 3.times { Horza::Entities::Single.new(FactoryGirl.attributes_for(:sportscars)) } }
206
197
 
207
198
  before do
208
199
  allow(Get::SportscarsFromUser).to receive(:run).and_return(sportscars)
@@ -229,24 +220,6 @@ _config/initializers/ask.rb_
229
220
  ```ruby
230
221
  Get.configure { |config| config.adapter = :active_record }
231
222
  ```
232
-
233
- **Configure custom entities**
234
-
235
- The code below will cause Get classes that begin with _Users_ (ie. `UsersByLastName`) to return a MyCustomEntity instead of the default `Get::Entities::User`.
236
-
237
- _config/initializers/ask.rb_
238
- ```ruby
239
- class MyCustomEntity < Get::Entities::Collection
240
- def east_london_length
241
- "#{length}, bruv"
242
- end
243
- end
244
-
245
- Get.config do |config|
246
- config.register_entity(:users_by_last_name, MyCustomEntity)
247
- end
248
- ```
249
-
250
223
  You can reset the config at any time using `Get.reset`.
251
224
 
252
225
  ## Adapters
@@ -17,10 +17,10 @@ module Get
17
17
  include Get
18
18
 
19
19
  class << self
20
- attr_reader :result_key
20
+ attr_reader :target
21
21
  end
22
22
 
23
- @entity, @result_key, @collection, @store = args[:key], args[:result_entity], args[:collection], args[:store]
23
+ @entity, @target, @collection, @store = args[:key], args[:result_entity], args[:collection], args[:store]
24
24
 
25
25
  def initialize(model, options = {})
26
26
  @model, @options = model, options
@@ -43,7 +43,7 @@ module Get
43
43
  {
44
44
  id: id,
45
45
  via: via,
46
- result_key: self.class.result_key
46
+ target: self.class.target
47
47
  }
48
48
  end
49
49
 
@@ -2,7 +2,9 @@ module Get
2
2
  module Builders
3
3
  class BaseBuilder
4
4
  def initialize(class_name)
5
- parse_class_name(class_name)
5
+ parser = ::Get::Parser.new(class_name)
6
+ @result_entity = parser.result_entity
7
+ @key = parser.key
6
8
  end
7
9
 
8
10
  def class
@@ -30,11 +30,11 @@ module Get
30
30
  private
31
31
 
32
32
  def query_params
33
- { query_action => { conditions: conditions } }
33
+ { query_action => conditions }
34
34
  end
35
35
 
36
36
  def query_action
37
- self.class.collection ? :find_all : :find_first
37
+ self.class.collection ? :find_all : :find_first!
38
38
  end
39
39
 
40
40
  # find_first
data/lib/get/builders.rb CHANGED
@@ -1,10 +1,7 @@
1
1
  module Get
2
2
  module Builders
3
- String.send(:include, ::Get::CoreExtensions::String)
4
-
5
3
  class << self
6
- def generate_class(name)
7
- method = name.to_s.match(GET_CLASS_REGEX)[2]
4
+ def generate_class(name, method)
8
5
  Get.const_set(name, builder_for_method(method).new(name).class)
9
6
  end
10
7
 
@@ -17,30 +17,14 @@ module Get
17
17
  raise ::Get::Errors::Base.new('Adapter has not been configured') unless configuration.adapter
18
18
  @adapter ||= configuration.adapter
19
19
  end
20
-
21
- def entity_for(model)
22
- configuration.entity_for(model)
23
- end
24
20
  end
25
21
 
26
22
  class Config
27
23
  attr_accessor :adapter
28
24
 
29
- def initialize
30
- @registered_entities = {}
31
- end
32
-
33
25
  def set_adapter(adapter)
34
26
  Horza.configure { |config| config.adapter = adapter }
35
27
  @adapter = Horza.adapter
36
28
  end
37
-
38
- def entity_for(model)
39
- @registered_entities[model]
40
- end
41
-
42
- def register_entity(model, klass)
43
- @registered_entities[model] = klass
44
- end
45
29
  end
46
30
  end
data/lib/get/db.rb CHANGED
@@ -10,12 +10,11 @@ module Get
10
10
 
11
11
  def initialize(actions)
12
12
  @actions = actions
13
- @query = Get.adapter.new(self.class.store)
13
+ @adapter = Get.adapter.new(self.class.store)
14
14
  end
15
15
 
16
16
  def call
17
17
  execute_queries
18
- self.class.entity_factory.build(@query)
19
18
  rescue Horza::Errors::InvalidAncestry
20
19
  raise Get::Errors::InvalidAncestry
21
20
  end
@@ -23,9 +22,11 @@ module Get
23
22
  private
24
23
 
25
24
  def execute_queries
25
+ res = nil
26
26
  @actions.each do |action, options|
27
- @query.send(action, options)
27
+ res = @adapter.send(action, options)
28
28
  end
29
+ res
29
30
  end
30
31
  end
31
32
  end
data/lib/get/parser.rb ADDED
@@ -0,0 +1,20 @@
1
+ module Get
2
+ class Parser
3
+ CLASS_REGEX = /^(.*)(By|From)(.*)/
4
+ attr_accessor :class_name, :result_entity, :method
5
+
6
+ def initialize(class_name)
7
+ @class_name = class_name
8
+ @match = class_name.to_s.match(CLASS_REGEX)
9
+ @result_entity, @method, @key_string = @match.values_at(1, 2, 3) if @match
10
+ end
11
+
12
+ def match?
13
+ !!@match
14
+ end
15
+
16
+ def key
17
+ @key_string.present? ? @key_string.symbolize : nil
18
+ end
19
+ end
20
+ end
data/lib/get.rb CHANGED
@@ -1,13 +1,11 @@
1
1
  require 'get/builders/base_builder'
2
2
  require 'get/builders/ancestry_builder'
3
3
  require 'get/builders/query_builder'
4
- require 'get/core_extensions/string'
5
4
  require 'get/builders'
6
5
  require 'get/configuration'
7
6
  require 'get/db'
8
- require 'get/entities'
9
- require 'get/entity_factory'
10
7
  require 'get/errors'
8
+ require 'get/parser'
11
9
  require 'get/run_methods'
12
10
  require 'horza'
13
11
 
@@ -26,8 +24,9 @@ module Get
26
24
  end
27
25
 
28
26
  def const_missing(name)
29
- return super(name) unless name.to_s.match(GET_CLASS_REGEX)
30
- Builders.generate_class(name)
27
+ parser = ::Get::Parser.new(name)
28
+ return super(name) unless parser.match?
29
+ Builders.generate_class(name, parser.method)
31
30
  end
32
31
  end
33
32
 
data/spec/get_spec.rb CHANGED
@@ -51,50 +51,6 @@ describe Get do
51
51
  end
52
52
  end
53
53
 
54
- describe '#configure' do
55
- context '#register_entity' do
56
- let(:user_count) { 3 }
57
-
58
- before do
59
- Get.configure { |config| config.register_entity(:users_by_last_name, MyCustomEntity) }
60
- user_count.times { GetSpec::User.create(last_name: last_name) }
61
- end
62
- after { Get.reset }
63
-
64
- it 'gets registers entity' do
65
- expect(Get.configuration.entity_for(:users_by_last_name)).to eq MyCustomEntity
66
- end
67
-
68
- it 'returns specified entity type after querying db' do
69
- result = Get::UsersByLastName.run(last_name)
70
- expect(result.is_a? MyCustomEntity).to be true
71
- expect(result.east_london_length).to eq "#{user_count}, bruv"
72
- end
73
- end
74
- end
75
-
76
- context '#entity_for' do
77
- context 'when entity has been registered' do
78
- before do
79
- Get.configure do |config|
80
- config.set_adapter(adapter)
81
- config.register_entity(:users_by_last_name, MyCustomEntity)
82
- end
83
- end
84
- after { Get.reset }
85
-
86
- it 'registers entity' do
87
- expect(Get.entity_for(:users_by_last_name)).to eq MyCustomEntity
88
- end
89
- end
90
-
91
- context 'when entity has not been registered' do
92
- it 'returns nil' do
93
- expect(Get.entity_for(:users_by_last_name)).to be nil
94
- end
95
- end
96
- end
97
-
98
54
  context '#adapter' do
99
55
  context 'when the adapter is set' do
100
56
  it 'returns the correct adapter class' do
@@ -116,13 +72,11 @@ describe Get do
116
72
  before do
117
73
  Get.configure do |config|
118
74
  config.set_adapter('my_adapter')
119
- config.register_entity(:users_by_last_name, MyCustomEntity)
120
75
  end
121
76
  Get.reset
122
77
  end
123
78
  it 'resets the config' do
124
79
  expect(Get.configuration.adapter).to be nil
125
- expect(Get.entity_for(:users_by_last_name)).to be nil
126
80
  end
127
81
  end
128
82
 
@@ -165,7 +119,7 @@ describe Get do
165
119
  context 'valid ancestry with no saved parent' do
166
120
  let(:user2) { GetSpec::User.create }
167
121
  it 'returns nil' do
168
- expect { Get::EmployerFromUser.run!(user2) }.to raise_error Get::Errors::RecordNotFound
122
+ expect(Get::EmployerFromUser.run!(user2)).to be nil
169
123
  end
170
124
  end
171
125
  end
@@ -325,7 +279,7 @@ describe Get::Builders::AncestryBuilder do
325
279
  end
326
280
 
327
281
  it 'correctly assigns class-level variables' do
328
- [:entity, :query_key, :collection, :store, :result_key].each do |class_var|
282
+ [:entity, :query_key, :collection, :store, :target].each do |class_var|
329
283
  expect(subject.class.respond_to? class_var).to be true
330
284
  end
331
285
  end
@@ -352,3 +306,31 @@ describe Get::Builders::QueryBuilder do
352
306
  end
353
307
  end
354
308
  end
309
+
310
+ describe Get::Parser do
311
+ let(:ancestry_name) { 'UserFromEmployer' }
312
+ let(:query_name) { 'UserFromEmployer' }
313
+
314
+ subject { Get::Parser }
315
+
316
+ describe '#match?' do
317
+ context 'when name is of ancestry type' do
318
+ it 'returns true' do
319
+ expect(subject.new(ancestry_name).match?).to be true
320
+ end
321
+ end
322
+
323
+ context 'when name is of query type' do
324
+ it 'returns true' do
325
+ expect(subject.new(query_name).match?).to be true
326
+ end
327
+ end
328
+
329
+ context 'when name is of no type' do
330
+ it 'returns false' do
331
+ expect(subject.new('Blablabla').match?).to be false
332
+ end
333
+ end
334
+
335
+ end
336
+ end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: get
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3.0
4
+ version: 0.1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Blake Turner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-22 00:00:00.000000000 Z
11
+ date: 2015-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: horza
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 0.0.1
19
+ version: 0.1.1
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 0.0.1
26
+ version: 0.1.1
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -122,11 +122,9 @@ files:
122
122
  - lib/get/builders/base_builder.rb
123
123
  - lib/get/builders/query_builder.rb
124
124
  - lib/get/configuration.rb
125
- - lib/get/core_extensions/string.rb
126
125
  - lib/get/db.rb
127
- - lib/get/entities.rb
128
- - lib/get/entity_factory.rb
129
126
  - lib/get/errors.rb
127
+ - lib/get/parser.rb
130
128
  - lib/get/run_methods.rb
131
129
  - spec/get_spec.rb
132
130
  - spec/spec_helper.rb
@@ -1,17 +0,0 @@
1
- module Get
2
- module CoreExtensions
3
- module String
4
- def singular?
5
- singularize == self
6
- end
7
-
8
- def plural?
9
- pluralize == self
10
- end
11
-
12
- def symbolize
13
- underscore.to_sym
14
- end
15
- end
16
- end
17
- end
data/lib/get/entities.rb DELETED
@@ -1,14 +0,0 @@
1
- module Get
2
- module Entities
3
- CLASS_PREFIX = 'Get'
4
-
5
- class << self
6
- def const_missing(name)
7
- return super(name) unless name.to_s.match(/^#{CLASS_PREFIX}/)
8
-
9
- parent_klass = name.to_s.plural? ? Horza::Entities::Collection : Horza::Entities::Single
10
- Get::Entities.const_set(name, Class.new(parent_klass))
11
- end
12
- end
13
- end
14
- end
@@ -1,35 +0,0 @@
1
- module Get
2
- class EntityFactory
3
- def initialize(entity, query_class_name, is_collection, result_key)
4
- @entity, @query_class_name, @is_collection, @result_key = entity, query_class_name, is_collection, result_key
5
- end
6
-
7
- def build(adapter_result)
8
- raise ::Get::Errors::RecordNotFound.new unless adapter_result.context
9
- klass.new(db_result(adapter_result))
10
- end
11
-
12
- private
13
-
14
- def db_result(adapter_result)
15
- @is_collection ? adapter_result.context : adapter_result.to_hash
16
- end
17
-
18
- def klass
19
- Get.entity_for(key) || Get::Entities.const_get(dynamic_class)
20
- end
21
-
22
- def key
23
- @query_class_name.demodulize.symbolize
24
- end
25
-
26
- def dynamic_class
27
- "#{::Get::Entities::CLASS_PREFIX}#{dynamic_key.camelize}"
28
- end
29
-
30
- def dynamic_key
31
- return @result_key.to_s if @result_key # Special case for ancestor queries
32
- @is_collection ? @entity.to_s.pluralize : @entity.to_s
33
- end
34
- end
35
- end