lhs 7.3.0 → 7.4.0
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 +10 -5
- data/lib/lhs/concerns/proxy/accessors.rb +74 -0
- data/lib/lhs/item.rb +1 -62
- data/lib/lhs/proxy.rb +1 -0
- data/lib/lhs/version.rb +1 -1
- data/spec/collection/accessors_spec.rb +29 -0
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4587fc1585c5d53dcbe9470a65cb49af38c965da
|
4
|
+
data.tar.gz: 97345090e54f551ea4361c97f03b566c160c72ca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cb6d9e6fa5ee7585bb7af456ee60f521b322cc484dba2a4714896a1ffc1ef9ed40a1d1ed914446335dc487a486d042f596e9c08d30fad301b685dde75b51f254
|
7
|
+
data.tar.gz: 7915323a720894ffd38e4810ae3f0bce107692f3db2df801b8d092918b7698d33b2ec4879788c15bcf0711371d1daadabae2893443714ea18e1d9011ad6a5300
|
data/lib/lhs/collection.rb
CHANGED
@@ -37,13 +37,18 @@ class LHS::Collection < LHS::Proxy
|
|
37
37
|
protected
|
38
38
|
|
39
39
|
def method_missing(name, *args, &block)
|
40
|
-
|
41
|
-
|
42
|
-
|
40
|
+
if _collection.respond_to?(name)
|
41
|
+
value = _collection.send(name, *args, &block)
|
42
|
+
return enclose_in_data(value) if value.is_a? Hash
|
43
|
+
value
|
44
|
+
elsif _data._raw.is_a?(Hash)
|
45
|
+
get(name, *args)
|
46
|
+
end
|
43
47
|
end
|
44
48
|
|
45
|
-
def respond_to_missing?(name,
|
46
|
-
|
49
|
+
def respond_to_missing?(name, _include_all = false)
|
50
|
+
# We accept every message that does not belong to set of keywords and is not a setter
|
51
|
+
BLACKLISTED_KEYWORDS.exclude?(name.to_s) && !name.to_s[/=$/]
|
47
52
|
end
|
48
53
|
|
49
54
|
private
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
|
3
|
+
class LHS::Proxy
|
4
|
+
module Accessors
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
# Keywords that would not be forwarded via method missing
|
8
|
+
# FIXME: Extend the set of keywords
|
9
|
+
BLACKLISTED_KEYWORDS = %w(new proxy_association)
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def set(name, value)
|
14
|
+
key = name.to_s.gsub(/=$/, '')
|
15
|
+
_data._raw[key.to_sym] = value
|
16
|
+
end
|
17
|
+
|
18
|
+
def get(name, *args)
|
19
|
+
name = args.first if name == :[]
|
20
|
+
value = _data._raw[name.to_s]
|
21
|
+
if value.nil? && _data._raw.present?
|
22
|
+
value = _data._raw[name.to_sym]
|
23
|
+
value = _data._raw[name.to_s.classify.to_sym] if value.nil?
|
24
|
+
end
|
25
|
+
|
26
|
+
return access_item(value) if value.is_a?(Hash)
|
27
|
+
return access_collection(value) if value.is_a?(Array)
|
28
|
+
convert(value)
|
29
|
+
end
|
30
|
+
|
31
|
+
def convert(value)
|
32
|
+
return value unless value.is_a?(String)
|
33
|
+
if date_time?(value)
|
34
|
+
Time.zone.parse(value)
|
35
|
+
elsif date?(value)
|
36
|
+
Date.parse(value)
|
37
|
+
else
|
38
|
+
value
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def access_item(value)
|
43
|
+
record = LHS::Record.for_url(value[:href]) if value[:href]
|
44
|
+
data = LHS::Data.new(value, _data)
|
45
|
+
if record
|
46
|
+
record.new(data)
|
47
|
+
else
|
48
|
+
data
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def access_collection(value)
|
53
|
+
data = LHS::Data.new(value, _data)
|
54
|
+
collection = LHS::Collection.new(data)
|
55
|
+
LHS::Data.new(collection, _data)
|
56
|
+
end
|
57
|
+
|
58
|
+
def date?(value)
|
59
|
+
value[date_time_regex, :date].presence
|
60
|
+
end
|
61
|
+
|
62
|
+
def time?(value)
|
63
|
+
value[date_time_regex, :time].presence
|
64
|
+
end
|
65
|
+
|
66
|
+
def date_time?(value)
|
67
|
+
date?(value) && time?(value)
|
68
|
+
end
|
69
|
+
|
70
|
+
def date_time_regex
|
71
|
+
/(?<date>\d{4}-\d{2}-\d{2})?(?<time>T\d{2}:\d{2}:\d{2}(\.\d*.\d{2}:\d{2})*)?/
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
data/lib/lhs/item.rb
CHANGED
@@ -34,72 +34,11 @@ class LHS::Item < LHS::Proxy
|
|
34
34
|
|
35
35
|
def method_missing(name, *args, &_block)
|
36
36
|
return set(name, args.try(&:first)) if name.to_s[/=$/]
|
37
|
-
name
|
38
|
-
value = _data._raw[name.to_s]
|
39
|
-
if value.nil? && _data._raw.present?
|
40
|
-
value = _data._raw[name.to_sym]
|
41
|
-
value = _data._raw[name.to_s.classify.to_sym] if value.nil?
|
42
|
-
end
|
43
|
-
if value.is_a?(Hash)
|
44
|
-
handle_hash(value)
|
45
|
-
elsif value.is_a?(Array)
|
46
|
-
data = LHS::Data.new(value, _data)
|
47
|
-
collection = LHS::Collection.new(data)
|
48
|
-
LHS::Data.new(collection, _data)
|
49
|
-
else
|
50
|
-
convert(value)
|
51
|
-
end
|
37
|
+
get(name, *args)
|
52
38
|
end
|
53
39
|
|
54
40
|
def respond_to_missing?(name, _include_all = false)
|
55
41
|
# We accept every message that does not belong to set of keywords
|
56
42
|
BLACKLISTED_KEYWORDS.exclude?(name.to_s)
|
57
43
|
end
|
58
|
-
|
59
|
-
private
|
60
|
-
|
61
|
-
# FIXME: Extend the set of keywords
|
62
|
-
BLACKLISTED_KEYWORDS = %w(new proxy_association)
|
63
|
-
|
64
|
-
def convert(value)
|
65
|
-
return value unless value.is_a?(String)
|
66
|
-
if date_time?(value)
|
67
|
-
Time.zone.parse(value)
|
68
|
-
elsif date?(value)
|
69
|
-
Date.parse(value)
|
70
|
-
else
|
71
|
-
value
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
def handle_hash(value)
|
76
|
-
record = LHS::Record.for_url(value[:href]) if value[:href]
|
77
|
-
data = LHS::Data.new(value, _data)
|
78
|
-
if record
|
79
|
-
record.new(data)
|
80
|
-
else
|
81
|
-
data
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
def set(name, value)
|
86
|
-
key = name.to_s.gsub(/=$/, '')
|
87
|
-
_data._raw[key.to_sym] = value
|
88
|
-
end
|
89
|
-
|
90
|
-
def date?(value)
|
91
|
-
value[date_time_regex, :date].presence
|
92
|
-
end
|
93
|
-
|
94
|
-
def time?(value)
|
95
|
-
value[date_time_regex, :time].presence
|
96
|
-
end
|
97
|
-
|
98
|
-
def date_time?(value)
|
99
|
-
date?(value) && time?(value)
|
100
|
-
end
|
101
|
-
|
102
|
-
def date_time_regex
|
103
|
-
/(?<date>\d{4}-\d{2}-\d{2})?(?<time>T\d{2}:\d{2}:\d{2}(\.\d*.\d{2}:\d{2})*)?/
|
104
|
-
end
|
105
44
|
end
|
data/lib/lhs/proxy.rb
CHANGED
data/lib/lhs/version.rb
CHANGED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'rails_helper'
|
2
|
+
|
3
|
+
describe LHS::Collection do
|
4
|
+
let(:items) { [{ name: 'Steve' }] }
|
5
|
+
let(:extra) { 'extra' }
|
6
|
+
let(:collection) { Record.where }
|
7
|
+
|
8
|
+
context 'accessors' do
|
9
|
+
let(:response_data) do
|
10
|
+
{
|
11
|
+
items: items,
|
12
|
+
extra: extra,
|
13
|
+
total: 1
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
before(:each) do
|
18
|
+
class Record < LHS::Record
|
19
|
+
endpoint 'http://datastore/records`'
|
20
|
+
end
|
21
|
+
stub_request(:get, %r{http://datastore/records})
|
22
|
+
.to_return(body: response_data.to_json)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'allows access to extra data passed with collection' do
|
26
|
+
expect(collection.extra).to eq(extra)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
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: 7.
|
4
|
+
version: 7.4.0
|
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: 2017-01-
|
11
|
+
date: 2017-01-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: lhc
|
@@ -202,6 +202,7 @@ files:
|
|
202
202
|
- lib/lhs/concerns/item/save.rb
|
203
203
|
- lib/lhs/concerns/item/update.rb
|
204
204
|
- lib/lhs/concerns/item/validation.rb
|
205
|
+
- lib/lhs/concerns/proxy/accessors.rb
|
205
206
|
- lib/lhs/concerns/proxy/create.rb
|
206
207
|
- lib/lhs/concerns/proxy/link.rb
|
207
208
|
- lib/lhs/concerns/record/all.rb
|
@@ -230,6 +231,7 @@ files:
|
|
230
231
|
- lib/lhs/version.rb
|
231
232
|
- script/ci/build.sh
|
232
233
|
- spec/.DS_Store
|
234
|
+
- spec/collection/accessors_spec.rb
|
233
235
|
- spec/collection/collection_items_spec.rb
|
234
236
|
- spec/collection/configurable_spec.rb
|
235
237
|
- spec/collection/delegate_spec.rb
|
@@ -378,11 +380,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
378
380
|
requirements:
|
379
381
|
- Ruby >= 2.0.0
|
380
382
|
rubyforge_project:
|
381
|
-
rubygems_version: 2.
|
383
|
+
rubygems_version: 2.5.2
|
382
384
|
signing_key:
|
383
385
|
specification_version: 4
|
384
386
|
summary: Rails gem providing an easy, active-record-like interface for http json services
|
385
387
|
test_files:
|
388
|
+
- spec/collection/accessors_spec.rb
|
386
389
|
- spec/collection/collection_items_spec.rb
|
387
390
|
- spec/collection/configurable_spec.rb
|
388
391
|
- spec/collection/delegate_spec.rb
|