horza 0.2.0 → 0.2.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6313d7a6f90f6db3f51ed94b16b0de3498f2209f
4
- data.tar.gz: eb2ecb68bb20c23db8032fe660a1b401b3b71fcf
3
+ metadata.gz: ad75669d3af7d16418b004a3c6ddea50258d8ae7
4
+ data.tar.gz: dc77c4becdffc82ff00016ad6a7f84c49be6965a
5
5
  SHA512:
6
- metadata.gz: c73d0a6d329c3326d8161ab9358407d6aa253f0137d2d5bb8afbd60301189b85aa7965aa5189ed95b39855243bf8fefb19ab7d0f288e5a27fb5a47c79175f322
7
- data.tar.gz: 91988862e8911b424c7feeedcdba37b4bdd09dcfdcdf2c7885311df15ebb9548ba9037562f2b4f18e17355a3d97f2ecc66b9fa336df7062b703b37b36420ef58
6
+ metadata.gz: fb4f6e4a305f50191357ff2fa7aed0e962961d12f020210833d45ca0b85ecd401835fb41576d37167ee285462a7355013dfd1309cf0eeeda0c821f9384cc7857
7
+ data.tar.gz: f434ceacd8d0c426c2ab251ed547c675602599de999e10f9d2666c907742b3989b1d63ec4fbb5d6148831216df2c8339883506ac03818abcc6d55fd91ee7f1f5
data/README.md CHANGED
@@ -16,8 +16,8 @@ end
16
16
  **Get Adapter for your ORM Object**
17
17
  ```ruby
18
18
  # ActiveRecord Example
19
- user = User.create(user_params)
20
- horza_user = Horza.adapter.new(user)
19
+ # Don't worry, We don't actually call things horza_users in our codebase, this is just for emphasis
20
+ horza_user = Horza.adapt(User)
21
21
 
22
22
  # Examples
23
23
  horza_user.get(id) # Find by id - Return nil on fail
@@ -25,6 +25,12 @@ horza_user.get!(id) # Find by id - Error on fail
25
25
  horza_user.find_first(params) # Find 1 user - Orders by id desc by default - Return nil on fail
26
26
  horza_user.find_first!(params) # Find 1 user - Orders by id desc by default - Error nil on fail
27
27
  horza_user.find_all(params) # Find all users that match parameters
28
+ horza_user.create(params) # Create record - return nil on fail
29
+ horza_user.create!(params) # Create record - raise error on fail
30
+ horza_user.update(params) # Update record - return nil on fail
31
+ horza_user.update!(params) # Update record - raise error on fail
32
+ horza_user.delete(params) # Delete record - return nil on fail
33
+ horza_user.delete!(params) # Delete record - raise error on fail
28
34
  horza_user.ancestors(target: :employer, via: []) # Traverse relations
29
35
  ```
30
36
 
@@ -36,24 +42,25 @@ Collection entities behave like arrays.
36
42
 
37
43
  ```ruby
38
44
  # Singular Entity
39
- result = horza_user.find_first(first_name: 'Blake') # => {"id"=>1, "first_name"=>"Blake", "last_name"=>"Turner", "employer_id"=>nil}
40
- result.class.name # => "Horza::Entities::Single"
45
+ result = horza_user.find_first(first_name: 'Blake')
41
46
 
47
+ result # => {"id"=>1, "first_name"=>"Blake", "last_name"=>"Turner", "employer_id"=>1}
48
+ result.class.name # => "Horza::Entities::Single"
42
49
  result['id'] # => 1
43
50
  result.id # => 1
44
51
  result.id? # => true
45
52
 
46
53
  # Collection Entity
47
54
  result = horza_user.find_all(last_name: 'Turner')
48
- result.class.name # => "Horza::Entities::Collection"
49
55
 
56
+ result.class.name # => "Horza::Entities::Collection"
50
57
  result.length # => 1
51
58
  result.size # => 1
52
59
  result.empty? # => false
53
60
  result.present? # => true
54
- result.first # => {"id"=>1, "first_name"=>"Blake", "last_name"=>"Turner", "employer_id"=>nil}
55
- result.last # => {"id"=>1, "first_name"=>"Blake", "last_name"=>"Turner", "employer_id"=>nil}
56
- result[0] # => {"id"=>1, "first_name"=>"Blake", "last_name"=>"Turner", "employer_id"=>nil}
61
+ result.first # => {"id"=>1, "first_name"=>"Blake", "last_name"=>"Turner", "employer_id"=>1}
62
+ result.last # => {"id"=>2, "first_name"=>"Morgan", "last_name"=>"Bruce", "employer_id"=>2}
63
+ result[0] # => {"id"=>1, "first_name"=>"Blake", "last_name"=>"Turner", "employer_id"=>1}
57
64
  ```
58
65
 
59
66
  ## Custom Entities
@@ -5,23 +5,25 @@ module Horza
5
5
  @collection = collection
6
6
  end
7
7
 
8
- def each
9
- @collection.each do |result|
10
- yield singular_entity(result)
11
- end
12
- end
13
-
14
8
  def [](index)
15
9
  singular_entity(@collection[index])
16
10
  end
17
11
 
18
12
  private
19
13
 
20
- def method_missing(method)
14
+ def method_missing(method, &block)
21
15
  if [:length, :size, :empty?, :present?].include? method
22
16
  @collection.send(method)
23
17
  elsif [:first, :last].include? method
24
18
  singular_entity(@collection.send(method))
19
+ elsif [:each, :map, :collect]
20
+ enum_method(method, &block)
21
+ end
22
+ end
23
+
24
+ def enum_method(method, &block)
25
+ @collection.send(method) do |result|
26
+ yield singular_entity(result)
25
27
  end
26
28
  end
27
29
 
@@ -300,6 +300,42 @@ describe Horza do
300
300
 
301
301
  describe 'Entities' do
302
302
  describe 'Collection' do
303
+ let(:members) do
304
+ 3.times.map { HorzaSpec::User.create }
305
+ end
306
+
307
+ subject { Horza::Entities::Collection.new(members) }
308
+
309
+ context '#each' do
310
+ context 'when name is of ancestry type' do
311
+ it 'yields a Get::Entities::Single with each iteration' do
312
+ subject.each do |member|
313
+ expect(member.is_a? Horza::Entities::Single).to be true
314
+ end
315
+ end
316
+ end
317
+ end
318
+
319
+ context '#map' do
320
+ context 'when name is of ancestry type' do
321
+ it 'yields a Get::Entities::Single with each iteration, returns array' do
322
+ map = subject.map(&:id)
323
+ expect(map.is_a? Array).to be true
324
+ expect(map.length).to eq 3
325
+ end
326
+ end
327
+ end
328
+
329
+ context '#collect' do
330
+ context 'when name is of ancestry type' do
331
+ it 'yields a Get::Entities::Single with each iteration, returns array' do
332
+ map = subject.collect(&:id)
333
+ expect(map.is_a? Array).to be true
334
+ expect(map.length).to eq 3
335
+ end
336
+ end
337
+ end
338
+
303
339
  context '#singular_entity_class' do
304
340
  context 'when singular entity class does not exist' do
305
341
  let(:dummy_model) { HorzaSpec::DummyModel.create }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: horza
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
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-28 00:00:00.000000000 Z
11
+ date: 2015-06-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hashie