lhs 3.1.1 → 3.1.3
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/lib/lhs/collection.rb +9 -29
- data/lib/lhs/concerns/collection/internal_collection.rb +41 -0
- data/lib/lhs/concerns/record/find.rb +1 -0
- data/lib/lhs/concerns/record/find_by.rb +5 -7
- data/lib/lhs/version.rb +1 -1
- data/spec/collection/collection_items_spec.rb +4 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0fea30df48822225921db5b070d8b3581c64451e
|
4
|
+
data.tar.gz: b29781282f62a5a68e97b0f4643af68b221c6bf1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b1a1529a764d30f17a15346b102ac3a68c3a52709fc12d55fd6454f6dfae9131f30baa61a7bb67931217748f34804b5715cbda3438ba2702d655137f7b153812
|
7
|
+
data.tar.gz: 321a76d203b0d7122af4ad1d38bf4c6feb0bf848c8991dd14c33205267d39deef3cf4ce931b5e6f7941471ddef6c44e5aa9c0958bea362f9012bdff6257cedd6
|
data/lib/lhs/collection.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
51
|
-
# and insures to return LHS::Items in case of iterating items
|
52
|
-
class Collection
|
53
|
-
include Enumerable
|
47
|
+
private
|
54
48
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
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
|
@@ -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
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
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
|
data/lib/lhs/version.rb
CHANGED
@@ -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.
|
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-
|
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
|