yaks 0.0.0 → 0.1.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.
Files changed (67) hide show
  1. checksums.yaml +13 -5
  2. data/.gitignore +1 -0
  3. data/.travis.yml +23 -0
  4. data/Gemfile +1 -1
  5. data/Gemfile.lock +32 -6
  6. data/README.md +182 -80
  7. data/examples/hal01.rb +126 -0
  8. data/examples/jsonapi01.rb +68 -0
  9. data/examples/jsonapi02.rb +62 -0
  10. data/examples/jsonapi03.rb +86 -0
  11. data/lib/yaks.rb +55 -25
  12. data/lib/yaks/collection_mapper.rb +33 -0
  13. data/lib/yaks/collection_resource.rb +65 -0
  14. data/lib/yaks/default_policy.rb +13 -0
  15. data/lib/yaks/hal_serializer.rb +59 -0
  16. data/lib/yaks/json_api_serializer.rb +59 -0
  17. data/lib/yaks/link_lookup.rb +23 -0
  18. data/lib/yaks/mapper.rb +59 -0
  19. data/lib/yaks/mapper/association.rb +43 -0
  20. data/lib/yaks/mapper/class_methods.rb +36 -0
  21. data/lib/yaks/mapper/config.rb +79 -0
  22. data/lib/yaks/mapper/has_many.rb +15 -0
  23. data/lib/yaks/mapper/has_one.rb +10 -0
  24. data/lib/yaks/mapper/link.rb +74 -0
  25. data/lib/yaks/mapper/lookup.rb +19 -0
  26. data/lib/yaks/mapper/map_links.rb +17 -0
  27. data/lib/yaks/null_resource.rb +28 -0
  28. data/lib/yaks/primitivize.rb +34 -15
  29. data/lib/yaks/profile_registry.rb +60 -0
  30. data/lib/yaks/rel_registry.rb +20 -0
  31. data/lib/yaks/resource.rb +28 -0
  32. data/lib/yaks/resource/link.rb +21 -0
  33. data/lib/yaks/serializer.rb +11 -53
  34. data/lib/yaks/shared_options.rb +15 -0
  35. data/lib/yaks/util.rb +76 -5
  36. data/lib/yaks/version.rb +1 -1
  37. data/spec/integration/map_to_resource_spec.rb +30 -0
  38. data/spec/json/hal_plant_collection.json +34 -0
  39. data/spec/spec_helper.rb +10 -1
  40. data/spec/support/friends_mapper.rb +29 -0
  41. data/spec/support/pet_mapper.rb +5 -0
  42. data/spec/support/pet_peeve_mapper.rb +3 -0
  43. data/spec/support/serializers.rb +11 -11
  44. data/spec/support/shared_contexts.rb +47 -0
  45. data/spec/support/shorthands.rb +22 -0
  46. data/spec/yaks/collection_resource_spec.rb +9 -0
  47. data/spec/yaks/hal_serializer_spec.rb +9 -0
  48. data/spec/yaks/mapper/association_spec.rb +21 -0
  49. data/spec/yaks/mapper/class_methods_spec.rb +28 -0
  50. data/spec/yaks/mapper/config_spec.rb +77 -0
  51. data/spec/yaks/mapper/has_one_spec.rb +16 -0
  52. data/spec/yaks/mapper/link_spec.rb +42 -0
  53. data/spec/yaks/mapper/map_links_spec.rb +46 -0
  54. data/spec/yaks/mapper_spec.rb +47 -0
  55. data/spec/yaks/resource_spec.rb +23 -0
  56. data/yaks.gemspec +6 -3
  57. metadata +115 -27
  58. data/lib/yaks/dumper.rb +0 -23
  59. data/lib/yaks/fold_ams_compat.rb +0 -33
  60. data/lib/yaks/fold_json_api.rb +0 -61
  61. data/lib/yaks/serializable_association.rb +0 -21
  62. data/lib/yaks/serializable_collection.rb +0 -10
  63. data/lib/yaks/serializable_object.rb +0 -18
  64. data/lib/yaks/serializer/class_methods.rb +0 -76
  65. data/spec/integration_spec.rb +0 -57
  66. data/spec/yaks/json_api_folder_spec.rb +0 -63
  67. data/spec/yaks/serializer_spec.rb +0 -12
@@ -1,21 +0,0 @@
1
- module Yaks
2
- class SerializableAssociation
3
- include Concord.new(:collection, :one)
4
- extend Forwardable
5
-
6
- def_delegator :collection, :root_key, :name
7
- def_delegators :collection, :identity_key, :map, :objects, :empty?
8
-
9
- def one?
10
- one
11
- end
12
-
13
- def identities
14
- map(&method(:identity))
15
- end
16
-
17
- def identity(object)
18
- object[identity_key]
19
- end
20
- end
21
- end
@@ -1,10 +0,0 @@
1
- module Yaks
2
- class SerializableCollection
3
- include Concord.new(:root_key, :identity_key, :objects)
4
- extend Forwardable
5
-
6
- public :root_key, :identity_key
7
-
8
- def_delegators :objects, :map, :flat_map, :empty?
9
- end
10
- end
@@ -1,18 +0,0 @@
1
- module Yaks
2
- class SerializableObject
3
- include Concord.new(:attributes, :associations)
4
- extend Forwardable
5
-
6
- public :attributes, :associations
7
-
8
- def_delegator :attributes, :[]
9
-
10
- def associated_objects(association_name)
11
- associations.detect {|association| association_name == association.name }.objects
12
- end
13
-
14
- def has_associated_objects?
15
- !associations.all?(&:empty?)
16
- end
17
- end
18
- end
@@ -1,76 +0,0 @@
1
- module Yaks
2
- class Serializer
3
- module ClassMethods
4
- include Util
5
-
6
- protected
7
-
8
- def inherited(desc)
9
- attributes
10
- has_one
11
- end
12
-
13
- def attributes(*attrs)
14
- _attributes.concat attrs
15
-
16
- attrs.each do |attr|
17
- unless method_defined?(attr)
18
- define_method attr do
19
- object.send attr
20
- end
21
- end
22
- end
23
- end
24
-
25
- def has_one(*attrs)
26
- _associations.concat(attrs.map {|a| [:has_one, a] })
27
- attrs.each do |attr|
28
- unless method_defined?(attr)
29
- define_method attr do
30
- object.send attr
31
- end
32
- end
33
- end
34
- end
35
-
36
- def has_many(*attrs)
37
- _associations.concat(attrs.map {|a| [:has_many, a] })
38
- attrs.each do |attr|
39
- unless method_defined?(attr)
40
- define_method attr do
41
- object.send attr
42
- end
43
- end
44
- end
45
- end
46
-
47
- def root_key(key)
48
- @root_key = key
49
- end
50
-
51
- def identity_key(id_key)
52
- @identity_key = id_key
53
- end
54
-
55
- public
56
-
57
- def _attributes
58
- @attributes ||= []
59
- end
60
-
61
- def _associations
62
- @associations ||= []
63
- end
64
-
65
- def _root_key
66
- @root_key ||
67
- pluralize(underscore(self.name.sub(/Serializer$/, '')))
68
- end
69
-
70
- def _identity_key
71
- @identity_key || :id
72
- end
73
-
74
- end
75
- end
76
- end
@@ -1,57 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module IntegrationRunner
4
- def run!
5
- specify do
6
- tests.each do |type, objects, result|
7
- expect(Yaks::Dumper.new(format: format).call(type, objects)).to eq result
8
- end
9
- end
10
- end
11
- end
12
-
13
- describe Yaks, 'integration tests' do
14
- include_context 'fixtures'
15
-
16
- context 'json api' do
17
- extend IntegrationRunner
18
-
19
- let(:format) { :json_api }
20
- let(:tests) do
21
- [
22
- ['friends', [john],
23
- {
24
- "friends" => [
25
- { "name" => "john",
26
- "id" => 1,
27
- "links" => {
28
- 'pets' => [2, 3],
29
- 'pet_peeve' => 4
30
- }
31
- }
32
- ],
33
- "linked" => {
34
- "pets" => [
35
- { "name" => "boingboing",
36
- "species" => "dog",
37
- "id" => 2
38
- },
39
- { "name" => "wassup",
40
- "species" => "cat",
41
- "id" => 3
42
- }
43
- ],
44
- "pet_peeve" => [
45
- { "id" => 4,
46
- "type" => "parsing with regexps"}
47
- ]
48
- }
49
- }
50
- ]
51
- ]
52
- end
53
-
54
- run!
55
- end
56
-
57
- end
@@ -1,63 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module Yaks
4
- describe FoldJsonApi do
5
- let(:collection) {
6
- SerializableCollection.new(
7
- 'friends',
8
- :id,
9
- Hamster.list(
10
- SerializableObject.new(
11
- Hamster.hash(
12
- id: '3',
13
- name: 'john'
14
- ),
15
- Hamster.list(
16
- SerializableAssociation.new(
17
- SerializableCollection.new(
18
- 'pets', :id,
19
- Hamster.list(
20
- SerializableObject.new(Hamster.hash(
21
- id: '3',
22
- pet_name: 'wabi'
23
- ),
24
- Hamster.list
25
- ),
26
- SerializableObject.new(Hamster.hash(
27
- id: '4',
28
- pet_name: 'sabi'
29
- ),
30
- Hamster.list
31
- )
32
- )
33
- )
34
- )
35
- )
36
- )
37
- )
38
- )
39
- }
40
-
41
- specify do
42
- expect( FoldJsonApi.new(collection).fold ).to eq(
43
- Hamster.hash(
44
- "friends" => Hamster.list(
45
- Hamster.hash(
46
- "id" => "3",
47
- "name" => "john",
48
- "links" => Hamster.hash(
49
- "pets" => Hamster.list("3", "4")
50
- ),
51
- )
52
- ),
53
- "linked" => Hamster.hash(
54
- "pets" => Hamster.set(
55
- Hamster.hash("pet_name" => "sabi", "id" => "4"),
56
- Hamster.hash("pet_name" => "wabi", "id" => "3")
57
- )
58
- )
59
- )
60
- )
61
- end
62
- end
63
- end
@@ -1,12 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Yaks::Serializer do
4
- include_context 'fixtures'
5
-
6
- let(:collection) { FriendSerializer.new.serializable_collection [john] }
7
-
8
- it do
9
- expect(Yaks::FoldJsonApi.new(collection).fold).to eq({})
10
- end
11
-
12
- end