lhs 3.1.1 → 3.1.3

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: f4f70cff889440ba4d2bca3c7b4d9480a763a2af
4
- data.tar.gz: b8d18ad5a5a3951b24d96e9de37073fbd199267a
3
+ metadata.gz: 0fea30df48822225921db5b070d8b3581c64451e
4
+ data.tar.gz: b29781282f62a5a68e97b0f4643af68b221c6bf1
5
5
  SHA512:
6
- metadata.gz: eebcb68bb74d56f301e44928874e72fc9e02c6754a21bd4a92bdec2200d26de45e49199bfc29366e2b3e8063ee28108eaf1cb3d9df1040e6c5f5a7027c09f323
7
- data.tar.gz: 1a402c99a72282ecac133bb3de3627851cbe9ee79378ab8dbac9792fdaa4aff429064d653b7e011cbe1e0c3e6ccf850c035ccb76bcbcd17e62f776c599e743f1
6
+ metadata.gz: b1a1529a764d30f17a15346b102ac3a68c3a52709fc12d55fd6454f6dfae9131f30baa61a7bb67931217748f34804b5715cbda3438ba2702d655137f7b153812
7
+ data.tar.gz: 321a76d203b0d7122af4ad1d38bf4c6feb0bf848c8991dd14c33205267d39deef3cf4ce931b5e6f7941471ddef6c44e5aa9c0958bea362f9012bdff6257cedd6
@@ -1,8 +1,10 @@
1
1
  require File.join(__dir__, 'proxy.rb')
2
+ Dir[File.dirname(__FILE__) + '/concerns/collection/*.rb'].each { |file| require file }
2
3
 
3
4
  # A collection is a special type of data
4
5
  # that contains multiple items
5
6
  class LHS::Collection < LHS::Proxy
7
+ include InternalCollection
6
8
 
7
9
  delegate :select, to: :_collection
8
10
 
@@ -34,41 +36,19 @@ class LHS::Collection < LHS::Proxy
34
36
 
35
37
  def method_missing(name, *args, &block)
36
38
  value = _collection.send(name, *args, &block)
37
- if value.is_a? Hash
38
- data = LHS::Data.new(value, _data)
39
- item = LHS::Item.new(data)
40
- LHS::Data.new(item, _data)
41
- else
42
- value
43
- end
39
+ return enclose_in_data(value) if value.is_a? Hash
40
+ value
44
41
  end
45
42
 
46
43
  def respond_to_missing?(name, include_all = false)
47
44
  _collection.respond_to?(name, include_all)
48
45
  end
49
46
 
50
- # The internal collection class that includes enumerable
51
- # and insures to return LHS::Items in case of iterating items
52
- class Collection
53
- include Enumerable
47
+ private
54
48
 
55
- attr_accessor :raw
56
- delegate :last, :sample, :[], :present?, :blank?, :empty?, to: :raw
57
-
58
- def initialize(raw, parent, record)
59
- self.raw = raw
60
- @parent = parent
61
- @record = record
62
- end
63
-
64
- def each(&_block)
65
- raw.each do |item|
66
- if item.is_a? Hash
67
- yield LHS::Data.new(item, @parent, @record)
68
- else
69
- yield item
70
- end
71
- end
72
- end
49
+ def enclose_in_data(value)
50
+ data = LHS::Data.new(value, _data)
51
+ item = LHS::Item.new(data)
52
+ LHS::Data.new(item, _data)
73
53
  end
74
54
  end
@@ -0,0 +1,41 @@
1
+ require 'active_support'
2
+
3
+ class LHS::Collection < LHS::Proxy
4
+
5
+ module InternalCollection
6
+ extend ActiveSupport::Concern
7
+
8
+ # The internal collection class that includes enumerable
9
+ # and insures to return LHS::Items in case of iterating items
10
+ class Collection
11
+ include Enumerable
12
+
13
+ attr_accessor :raw
14
+ delegate :last, :sample, :[], :present?, :blank?, :empty?, to: :raw
15
+
16
+ def initialize(raw, parent, record)
17
+ self.raw = raw
18
+ @parent = parent
19
+ @record = record
20
+ end
21
+
22
+ def each(&_block)
23
+ raw.each do |item|
24
+ if item.is_a? Hash
25
+ yield cast_item(item)
26
+ else
27
+ yield item
28
+ end
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def cast_item(item)
35
+ data = LHS::Data.new(item, @parent, @record)
36
+ return @record.new(data) if @record
37
+ data
38
+ end
39
+ end
40
+ end
41
+ end
@@ -14,6 +14,7 @@ class LHS::Record
14
14
  else
15
15
  find_by_id(args)
16
16
  end
17
+ return data unless data._record_class
17
18
  data._record_class.new(data)
18
19
  end
19
20
 
@@ -23,13 +23,11 @@ class LHS::Record
23
23
  def _find_by(params)
24
24
  params = params.dup.merge(limit: 1)
25
25
  data = request(params: params)
26
- data =
27
- if data._proxy.is_a?(LHS::Collection)
28
- data.first || fail(LHC::NotFound.new('No item was found.', data._request.response))
29
- else
30
- data
31
- end
32
- data._record_class.new(data)
26
+ if data._proxy.is_a?(LHS::Collection)
27
+ data.first || fail(LHC::NotFound.new('No item was found.', data._request.response))
28
+ else
29
+ data._record_class.new(data)
30
+ end
33
31
  end
34
32
  end
35
33
  end
@@ -1,3 +1,3 @@
1
1
  module LHS
2
- VERSION = "3.1.1"
2
+ VERSION = "3.1.3"
3
3
  end
@@ -22,6 +22,10 @@ describe LHS::Collection do
22
22
  it 'initalises a collection' do
23
23
  expect(collection.first.name).to eq 'Steve'
24
24
  end
25
+
26
+ it 'casts items to be instance of defined LHS::Record' do
27
+ expect(collection.first).to be_kind_of Account
28
+ end
25
29
  end
26
30
 
27
31
  context 'items key' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lhs
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.1
4
+ version: 3.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - https://github.com/local-ch/lhs/graphs/contributors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-23 00:00:00.000000000 Z
11
+ date: 2016-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lhc
@@ -150,6 +150,7 @@ files:
150
150
  - lhs.gemspec
151
151
  - lib/lhs.rb
152
152
  - lib/lhs/collection.rb
153
+ - lib/lhs/concerns/collection/internal_collection.rb
153
154
  - lib/lhs/concerns/data/json.rb
154
155
  - lib/lhs/concerns/item/destroy.rb
155
156
  - lib/lhs/concerns/item/save.rb