collection-json 0.1.2 → 0.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.
- data/.gitignore +1 -0
- data/lib/collection-json/attribute.rb +12 -0
- data/lib/collection-json/attributes/collection.rb +4 -2
- data/lib/collection-json/attributes/item.rb +6 -4
- data/lib/collection-json/attributes/query.rb +3 -2
- data/lib/collection-json/attributes/template.rb +3 -2
- data/lib/collection-json/version.rb +1 -1
- data/spec/collection-json_spec.rb +3 -0
- metadata +4 -4
data/.gitignore
CHANGED
@@ -27,6 +27,7 @@ module CollectionJSON
|
|
27
27
|
end
|
28
28
|
end
|
29
29
|
end
|
30
|
+
add_find_method(name, opts[:find_method]) if opts.has_key?(:find_method)
|
30
31
|
end
|
31
32
|
|
32
33
|
def self.root_node(value = nil)
|
@@ -55,6 +56,17 @@ module CollectionJSON
|
|
55
56
|
end
|
56
57
|
|
57
58
|
private
|
59
|
+
def self.add_find_method(name, opts)
|
60
|
+
opts = {method_name: opts} unless opts.is_a?(Hash)
|
61
|
+
opts[:key] ||= 'rel'
|
62
|
+
define_method(opts[:method_name]) do |*args|
|
63
|
+
send(name).select do |element|
|
64
|
+
value = element.send opts[:key]
|
65
|
+
args.include? value
|
66
|
+
end.first
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
58
70
|
def skip_value?(value)
|
59
71
|
value.nil? || value.respond_to?(:length) && value.length == 0
|
60
72
|
end
|
@@ -12,13 +12,15 @@ module CollectionJSON
|
|
12
12
|
attribute :version
|
13
13
|
attribute :links,
|
14
14
|
transform: lambda { |links| links.each.map { |l| Link.from_hash(l) }},
|
15
|
-
default: []
|
15
|
+
default: [],
|
16
|
+
find_method: :link
|
16
17
|
attribute :items,
|
17
18
|
transform: lambda { |items| items.each.map { |i| Item.from_hash(i) }},
|
18
19
|
default: []
|
19
20
|
attribute :queries,
|
20
21
|
transform: lambda { |queries| queries.each.map { |q| Query.from_hash(q) }},
|
21
|
-
default: []
|
22
|
+
default: [],
|
23
|
+
find_method: :query
|
22
24
|
attribute :template, transform: lambda { |template| Template.from_hash(template) }
|
23
25
|
attribute :error, transform: lambda { |error| Error.from_hash(error) }
|
24
26
|
end
|
@@ -6,10 +6,12 @@ module CollectionJSON
|
|
6
6
|
class Item < Attribute
|
7
7
|
attribute :href, transform: URI
|
8
8
|
attribute :data,
|
9
|
-
transform:
|
10
|
-
default:
|
9
|
+
transform: lambda { |data| data.each.map{ |d| Data.from_hash(d) }},
|
10
|
+
default: [],
|
11
|
+
find_method: {method_name: :datum, key: 'name'}
|
11
12
|
attribute :links,
|
12
|
-
transform:
|
13
|
-
default:
|
13
|
+
transform: lambda { |links| links.each.map { |l| Link.from_hash(l) }},
|
14
|
+
default: [],
|
15
|
+
find_method: :link
|
14
16
|
end
|
15
17
|
end
|
@@ -8,8 +8,9 @@ module CollectionJSON
|
|
8
8
|
attribute :name
|
9
9
|
attribute :prompt
|
10
10
|
attribute :data,
|
11
|
-
transform:
|
12
|
-
default:
|
11
|
+
transform: lambda { |data| data.each.map { |d| Data.from_hash(d) }},
|
12
|
+
default: [],
|
13
|
+
find_method: {method_name: :datum, key: 'name'}
|
13
14
|
|
14
15
|
def build(params = {})
|
15
16
|
URI(href).tap do |uri|
|
@@ -3,8 +3,9 @@ require_relative 'data'
|
|
3
3
|
module CollectionJSON
|
4
4
|
class Template < Attribute
|
5
5
|
attribute :data,
|
6
|
-
transform:
|
7
|
-
default:
|
6
|
+
transform: lambda { |data| data.each.map { |d| Data.from_hash(d) }},
|
7
|
+
default: [],
|
8
|
+
find_method: {method_name: :datum, key: 'name'}
|
8
9
|
|
9
10
|
def build(params = {})
|
10
11
|
{'template' => Hash.new}.tap do |hash|
|
@@ -47,8 +47,10 @@ describe CollectionJSON do
|
|
47
47
|
|
48
48
|
response.href.should eq('/friends/')
|
49
49
|
response.links.first.href.should eq("/friends/rss")
|
50
|
+
response.link('feed').href.should eq("/friends/rss")
|
50
51
|
response.items.length.should eq(3)
|
51
52
|
response.items.first.data.length.should eq(2)
|
53
|
+
response.items.first.datum('full-name').value.should eq("J. Doe")
|
52
54
|
response.items.first.links.length.should eq(2)
|
53
55
|
response.items.first.href.class.should eq(String)
|
54
56
|
response.template.data.length.should eq(4)
|
@@ -56,6 +58,7 @@ describe CollectionJSON do
|
|
56
58
|
response.queries.first.href.should eq("/friends/search")
|
57
59
|
response.queries.first.data.length.should eq(1)
|
58
60
|
response.queries.first.data.first.name.should eq('search')
|
61
|
+
response.query('search').prompt.should eq('Search')
|
59
62
|
end
|
60
63
|
end
|
61
64
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: collection-json
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-04-
|
12
|
+
date: 2012-04-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &70202255799500 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70202255799500
|
25
25
|
description: Lightweight gem for building Collection+JSON responses.
|
26
26
|
email:
|
27
27
|
- sebastian@uprise.co.nz
|