yaks-html 0.6.0.alpha → 0.6.0.alpha.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -525
  3. data/Rakefile +1 -41
  4. data/{yaks-html/lib → lib}/yaks-html.rb +0 -0
  5. data/{yaks-html/lib → lib}/yaks/format/html.rb +0 -0
  6. data/{yaks-html/lib → lib}/yaks/format/template.html +0 -0
  7. data/{yaks-html/spec → spec}/spec_helper.rb +0 -0
  8. data/{yaks-html/yaks-html.gemspec → yaks-html.gemspec} +1 -1
  9. metadata +11 -108
  10. data/.gitignore +0 -7
  11. data/.travis.yml +0 -36
  12. data/ADDING_FORMATS.md +0 -13
  13. data/CHANGELOG.md +0 -149
  14. data/Gemfile +0 -4
  15. data/IDENTIFIERS.md +0 -113
  16. data/LICENSE +0 -7
  17. data/bench/bench.rb +0 -16
  18. data/bench/bench_1000.rb +0 -60
  19. data/notes.org +0 -72
  20. data/shaved_yak.gif +0 -0
  21. data/yaks-html/README.md +0 -3
  22. data/yaks/README.md +0 -526
  23. data/yaks/lib/yaks.rb +0 -62
  24. data/yaks/lib/yaks/breaking_changes.rb +0 -49
  25. data/yaks/lib/yaks/collection_mapper.rb +0 -52
  26. data/yaks/lib/yaks/collection_resource.rb +0 -73
  27. data/yaks/lib/yaks/config.rb +0 -65
  28. data/yaks/lib/yaks/config/dsl.rb +0 -165
  29. data/yaks/lib/yaks/default_policy.rb +0 -100
  30. data/yaks/lib/yaks/format.rb +0 -87
  31. data/yaks/lib/yaks/format/collection_json.rb +0 -42
  32. data/yaks/lib/yaks/format/hal.rb +0 -82
  33. data/yaks/lib/yaks/format/json_api.rb +0 -86
  34. data/yaks/lib/yaks/fp.rb +0 -26
  35. data/yaks/lib/yaks/fp/callable.rb +0 -9
  36. data/yaks/lib/yaks/fp/hash_updatable.rb +0 -19
  37. data/yaks/lib/yaks/fp/updatable.rb +0 -17
  38. data/yaks/lib/yaks/mapper.rb +0 -75
  39. data/yaks/lib/yaks/mapper/association.rb +0 -43
  40. data/yaks/lib/yaks/mapper/association_mapper.rb +0 -42
  41. data/yaks/lib/yaks/mapper/attribute.rb +0 -17
  42. data/yaks/lib/yaks/mapper/class_methods.rb +0 -37
  43. data/yaks/lib/yaks/mapper/config.rb +0 -44
  44. data/yaks/lib/yaks/mapper/has_many.rb +0 -29
  45. data/yaks/lib/yaks/mapper/has_one.rb +0 -15
  46. data/yaks/lib/yaks/mapper/link.rb +0 -91
  47. data/yaks/lib/yaks/null_resource.rb +0 -40
  48. data/yaks/lib/yaks/primitivize.rb +0 -45
  49. data/yaks/lib/yaks/resource.rb +0 -51
  50. data/yaks/lib/yaks/resource/link.rb +0 -21
  51. data/yaks/lib/yaks/runner.rb +0 -101
  52. data/yaks/lib/yaks/util.rb +0 -56
  53. data/yaks/lib/yaks/version.rb +0 -3
  54. data/yaks/spec/acceptance/acceptance_spec.rb +0 -48
  55. data/yaks/spec/acceptance/json_shared_examples.rb +0 -8
  56. data/yaks/spec/acceptance/models.rb +0 -48
  57. data/yaks/spec/fixture_helpers.rb +0 -14
  58. data/yaks/spec/integration/map_to_resource_spec.rb +0 -28
  59. data/yaks/spec/json/confucius.collection.json +0 -28
  60. data/yaks/spec/json/confucius.hal.json +0 -59
  61. data/yaks/spec/json/confucius.json_api.json +0 -51
  62. data/yaks/spec/json/john.hal.json +0 -29
  63. data/yaks/spec/json/plant_collection.collection.json +0 -32
  64. data/yaks/spec/json/plant_collection.hal.json +0 -34
  65. data/yaks/spec/json/youtypeitwepostit.collection.json +0 -45
  66. data/yaks/spec/spec_helper.rb +0 -30
  67. data/yaks/spec/support/classes_for_policy_testing.rb +0 -36
  68. data/yaks/spec/support/deep_eql.rb +0 -123
  69. data/yaks/spec/support/fixtures.rb +0 -6
  70. data/yaks/spec/support/friends_mapper.rb +0 -29
  71. data/yaks/spec/support/models.rb +0 -23
  72. data/yaks/spec/support/pet_mapper.rb +0 -3
  73. data/yaks/spec/support/pet_peeve_mapper.rb +0 -3
  74. data/yaks/spec/support/shared_contexts.rb +0 -57
  75. data/yaks/spec/support/youtypeit_models_mappers.rb +0 -20
  76. data/yaks/spec/unit/yaks/collection_mapper_spec.rb +0 -165
  77. data/yaks/spec/unit/yaks/collection_resource_spec.rb +0 -77
  78. data/yaks/spec/unit/yaks/config/dsl_spec.rb +0 -87
  79. data/yaks/spec/unit/yaks/config_spec.rb +0 -74
  80. data/yaks/spec/unit/yaks/default_policy/derive_mapper_from_object_spec.rb +0 -80
  81. data/yaks/spec/unit/yaks/default_policy_spec.rb +0 -70
  82. data/yaks/spec/unit/yaks/format/collection_json_spec.rb +0 -41
  83. data/yaks/spec/unit/yaks/format/hal_spec.rb +0 -44
  84. data/yaks/spec/unit/yaks/format/json_api_spec.rb +0 -42
  85. data/yaks/spec/unit/yaks/format_spec.rb +0 -37
  86. data/yaks/spec/unit/yaks/fp/callable_spec.rb +0 -13
  87. data/yaks/spec/unit/yaks/fp/hash_updatable_spec.rb +0 -22
  88. data/yaks/spec/unit/yaks/fp/updatable_spec.rb +0 -22
  89. data/yaks/spec/unit/yaks/fp_spec.rb +0 -31
  90. data/yaks/spec/unit/yaks/mapper/association_mapper_spec.rb +0 -60
  91. data/yaks/spec/unit/yaks/mapper/association_spec.rb +0 -135
  92. data/yaks/spec/unit/yaks/mapper/attribute_spec.rb +0 -20
  93. data/yaks/spec/unit/yaks/mapper/class_methods_spec.rb +0 -67
  94. data/yaks/spec/unit/yaks/mapper/config_spec.rb +0 -166
  95. data/yaks/spec/unit/yaks/mapper/has_many_spec.rb +0 -89
  96. data/yaks/spec/unit/yaks/mapper/has_one_spec.rb +0 -51
  97. data/yaks/spec/unit/yaks/mapper/link_spec.rb +0 -196
  98. data/yaks/spec/unit/yaks/mapper_spec.rb +0 -303
  99. data/yaks/spec/unit/yaks/null_resource_spec.rb +0 -32
  100. data/yaks/spec/unit/yaks/primitivize_spec.rb +0 -77
  101. data/yaks/spec/unit/yaks/resource/link_spec.rb +0 -20
  102. data/yaks/spec/unit/yaks/resource_spec.rb +0 -108
  103. data/yaks/spec/unit/yaks/runner_spec.rb +0 -260
  104. data/yaks/spec/unit/yaks/util_spec.rb +0 -49
  105. data/yaks/spec/yaml/confucius.yaml +0 -28
  106. data/yaks/spec/yaml/youtypeitwepostit.yaml +0 -9
  107. data/yaks/yaks.gemspec +0 -41
data/yaks/lib/yaks.rb DELETED
@@ -1,62 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- require 'forwardable'
4
- require 'set'
5
- require 'pathname'
6
-
7
- require 'anima'
8
- require 'concord'
9
- require 'inflection'
10
- require 'uri_template'
11
- require 'rack/accept'
12
-
13
- require 'yaks/util'
14
- require 'yaks/fp'
15
- require 'yaks/fp/updatable'
16
- require 'yaks/fp/hash_updatable'
17
- require 'yaks/fp/callable'
18
- require 'yaks/primitivize'
19
-
20
- require 'yaks/default_policy'
21
-
22
- module Yaks
23
- # A PORO
24
- Undefined = Object.new
25
- # Set the Root constant as the gems root path
26
- Root = Pathname(__FILE__).join('../..')
27
-
28
- class << self
29
- # @param [Proc] blk
30
- # @return [Yaks::Config]
31
- def new(&blk)
32
- Yaks::Config.new(&blk)
33
- end
34
- end
35
-
36
- end
37
-
38
-
39
- require 'yaks/resource'
40
- require 'yaks/null_resource'
41
- require 'yaks/resource/link'
42
- require 'yaks/collection_resource'
43
-
44
- require 'yaks/mapper/class_methods'
45
- require 'yaks/mapper'
46
- require 'yaks/mapper/attribute'
47
- require 'yaks/mapper/link'
48
- require 'yaks/mapper/association'
49
- require 'yaks/mapper/association_mapper'
50
- require 'yaks/mapper/has_one'
51
- require 'yaks/mapper/has_many'
52
- require 'yaks/mapper/config'
53
- require 'yaks/collection_mapper'
54
-
55
- require 'yaks/format'
56
- require 'yaks/format/hal'
57
- require 'yaks/format/json_api'
58
- require 'yaks/format/collection_json'
59
-
60
- require 'yaks/config/dsl'
61
- require 'yaks/config'
62
- require 'yaks/runner'
@@ -1,49 +0,0 @@
1
- module Yaks
2
-
3
- # These are displayed in a post-install message when installing the
4
- # gem to aid upgraiding
5
-
6
- BreakingChanges = {
7
-
8
- '0.5.0' => %q~
9
-
10
- Breaking Changes in Yaks 0.5.0
11
- ==============================
12
-
13
- Yaks now serializes its output, you no longer have to convert to JSON
14
- yourself. Use `skip :serialize' to get the old behavior, or
15
- `json_serializer` to use a different JSON implementation.
16
-
17
- The single `after' hook has been replaced with a set of `before',
18
- `after', `around' and `skip' hooks.
19
-
20
- If you've created your own subclass of `Yaks::Format' (previously:
21
- `Yaks::Serializer'), then you need to update the call to
22
- `Format.register'.
23
-
24
- These are potentially breaking changes. See the CHANGELOG and README
25
- for full documentation.
26
-
27
- ~,
28
-
29
- '0.4.3' => %q~
30
-
31
- Breaking Changes in Yaks 0.4.3
32
- ==============================
33
-
34
- Yaks::Mapper#filter was removed, if you override this method in your
35
- mappers to conditionally filter attributes or associations, you will
36
- have to override #attributes or #associations instead.
37
-
38
- When specifying a rel_template, now a single {rel} placeholder is
39
- expected instead of {src} and {dest}.
40
-
41
- There are other internal changes. See the CHANGELOG and README for full
42
- documentation.
43
-
44
- ~
45
- }
46
-
47
- BreakingChanges['0.4.4'] = BreakingChanges['0.4.3']
48
-
49
- end
@@ -1,52 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- module Yaks
4
- class CollectionMapper < Mapper
5
- alias collection object
6
-
7
- # @param [Array] collection
8
- # @return [Array]
9
- def call(collection)
10
- @object = collection
11
-
12
- attrs = {
13
- type: collection_type,
14
- members: collection().map do |obj|
15
- mapper_for_model(obj).new(context).call(obj)
16
- end
17
- }
18
-
19
- attrs[ :collection_rel ] = collection_rel
20
-
21
- map_attributes(
22
- map_links(
23
- CollectionResource.new(attrs)
24
- )
25
- )
26
- end
27
-
28
- private
29
-
30
- def collection_rel
31
- if collection_type
32
- policy.expand_rel( pluralize( collection_type ) )
33
- else
34
- 'collection'
35
- end
36
- end
37
-
38
- def collection_type
39
- if item_mapper = context[:item_mapper]
40
- item_mapper.config.type || policy.derive_type_from_mapper_class(item_mapper)
41
- else
42
- policy.derive_type_from_collection(collection)
43
- end
44
- end
45
-
46
- def mapper_for_model(model)
47
- context.fetch(:item_mapper) do
48
- policy.derive_mapper_from_object(model)
49
- end
50
- end
51
- end
52
- end
@@ -1,73 +0,0 @@
1
- module Yaks
2
- # A collection of Resource objects, it has members, and its own set of link
3
- # relations like self and profile describing the collection.
4
- #
5
- # A collection can be the top-level result of an API call, like all posts to
6
- # a blog, or a subresource collection, like the comments on a post result.
7
- #
8
- # Some formats treat everything like a collection, and a single resource as a
9
- # collection of one. Others treat every top level response as singular, e.g.
10
- # a single "collection of orders". Because of this Resource and
11
- # CollectionResource can both be iterated with #each, for the "everything is
12
- # a collection crowd", and they both respond to `links`, `attributes` and
13
- # `subresources`, so they can both be approached like a singular resource.
14
- #
15
- # In the second case a collection has a single "subresource", being its
16
- # members.
17
- class CollectionResource < Resource
18
- include Equalizer.new(:type, :links, :attributes, :members, :collection_rel)
19
- include FP::HashUpdatable.new(:type, :links, :attributes, :members, :collection_rel)
20
- include Enumerable
21
-
22
- extend Forwardable
23
-
24
- # @!attribute [r] type
25
- # @return [String]
26
- # @!attribute [r] links
27
- # @return [Array]
28
- # @!attribute [r] members
29
- # @return [Array]
30
- # @!attribute [r] collection_rel
31
- # @return [String]
32
- attr_reader :type, :links, :members, :collection_rel
33
-
34
- def_delegators :members, :each
35
-
36
- # @param [Hash] options
37
- # @return [CollectionResource]
38
- def initialize(options)
39
- super
40
- @members = options.fetch(:members, [])
41
- @collection_rel = options.fetch(:collection_rel, 'members')
42
- end
43
-
44
- # Make a CollectionResource quack like a resource.
45
- #
46
- # At the moment this is only for HAL, which always assumes
47
- # a singular resource at the top level, this way it can treat
48
- # whatever it gets as a single resource with links and subresources,
49
- # we just push the collection down one level.
50
- #
51
- # Once inside subresources the HAL format does check if a resource
52
- # is a collection, since there it does make a distinction, and because
53
- # in that case it will iterate with each/map rather than calling subresources,
54
- # this doesn't cause infinite recursion. Not very pretty, needs looking at.
55
- #
56
- # :(
57
- #
58
- # @return [Hash]
59
- def subresources
60
- if any?
61
- { collection_rel => self }
62
- else
63
- {}
64
- end
65
- end
66
-
67
- # @return [Boolean]
68
- def collection?
69
- true
70
- end
71
-
72
- end
73
- end
@@ -1,65 +0,0 @@
1
- module Yaks
2
- class Config
3
- include Yaks::FP::Callable
4
-
5
- # @!attribute [r] format_options
6
- # @return [Hash<Symbol,Hash>]
7
- attr_reader :format_options
8
-
9
- # @!attribute [rw] default_format
10
- # @return [Symbol]
11
- attr_accessor :default_format
12
-
13
- # @!attribute [rw] policy_class
14
- # @return [Class]
15
- attr_accessor :policy_class
16
-
17
- # @!attribute [r] policy_options
18
- # @return [Hash]
19
- attr_reader :policy_options
20
-
21
- # @!attribute [rw] primitivize
22
- # @return [Primitivize]
23
- attr_accessor :primitivize
24
-
25
- # @!attribute [r] serializers
26
- # @return [Hash<Symbol,#call>]
27
- attr_reader :serializers
28
-
29
- # @!attribute [r] hooks
30
- # @return [Array]
31
- attr_reader :hooks
32
-
33
- # @param blk [Proc] Configuration block
34
- def initialize(&blk)
35
- @format_options = Hash.new({})
36
- @default_format = :hal
37
- @policy_options = {}
38
- @primitivize = Primitivize.create
39
- @serializers = {}
40
- @hooks = []
41
-
42
- DSL.new(self, &blk)
43
- end
44
-
45
- # @return [Yaks::DefaultPolicy]
46
- def policy
47
- @policy ||= @policy_class.new(@policy_options)
48
- end
49
-
50
- # Main entry point into yaks
51
- #
52
- # @param object [Object] The object to serialize
53
- # @param options [Hash<Symbol,Object>] Serialization options
54
- #
55
- # @option env [Hash] The rack environment
56
- # @option format [Symbol] The target format, default :hal
57
- # @option mapper [Class] Mapper class to use
58
- # @option item_mapper [Class] Mapper class to use for items in a top-level collection
59
- #
60
- def call(object, options = {})
61
- Runner.new(config: self, object: object, options: options).call
62
- end
63
- alias serialize call
64
- end
65
- end
@@ -1,165 +0,0 @@
1
- module Yaks
2
- class Config
3
- class DSL
4
- # @!attribute [r] config
5
- # @return [Yaks::Config]
6
- attr_reader :config
7
-
8
- # @param [Yaks::Config] config
9
- # @param [Proc] block
10
- def initialize(config, &block)
11
- @config = config
12
- @policy_class = Class.new(DefaultPolicy)
13
- @policies = []
14
- instance_eval(&block) if block
15
- @policies.each do |policy_block|
16
- @policy_class.class_eval &policy_block
17
- end
18
- config.policy_class = @policy_class
19
- end
20
-
21
- # Set the options for a format
22
- #
23
- # @param [Symbol] format
24
- # @param [Hash] options
25
- #
26
- # @example
27
- #
28
- # yaks = Yaks.new do
29
- # format_options :hal, {plural_links: [:related_content]}
30
- # end
31
- #
32
- def format_options(format, options)
33
- config.format_options[format] = options
34
- end
35
-
36
- # Set the default format
37
- #
38
- # Defaults to +:hal+
39
- #
40
- # @param [Symbol] format
41
- # Format identifier, one of +Yaks::Format.names+
42
- #
43
- # @example
44
- #
45
- # yaks = Yaks.new do
46
- # default_fomat :json_api
47
- # end
48
- #
49
- def default_format(format)
50
- config.default_format = format
51
- end
52
-
53
- # Configure JSON serializer
54
- #
55
- # Defaults to JSON.pretty_generate
56
- #
57
- # @example
58
- #
59
- # yaks = Yaks.new do
60
- # json_serializer &Oj.method(:dump)
61
- # end
62
- #
63
- # @param [Proc] block
64
- # Serialization procedure
65
- #
66
- def json_serializer(&block)
67
- config.serializers[:json] = block
68
- end
69
-
70
- %w[before after around skip].map(&:intern).each do |hook_type|
71
- define_method hook_type do |step, name = :"#{hook_type}_#{step}", &block|
72
- config.hooks << [hook_type, step, name, block]
73
- end
74
- end
75
-
76
- # Set a different policy implementation
77
- #
78
- # By default Yaks uses +Yaks::DefaultPolicy+ to derive missing
79
- # information. You can swap in a class with a compatible
80
- # interface to change the default behavior
81
- #
82
- # To override a single policy method, simply call a method with
83
- # the same name as part of your Yaks configuration, passing a
84
- # block to define the new behavior.
85
- #
86
- # @example
87
- #
88
- # yaks = Yaks.new do
89
- # derive_type_from_mapper_class do |mapper_class|
90
- # mapper_class.name.sub(/Mapper^/,'')
91
- # end
92
- # end
93
- #
94
- # @param [Class] klass
95
- # Policy class
96
- #
97
- def policy(klass)
98
- @policy_class = klass
99
- end
100
-
101
- # Set the template for deriving rels
102
- #
103
- # Used to derive rels for links and subresources.
104
- #
105
- # @example
106
- #
107
- # yaks = Yaks.new do
108
- # rel_template 'http://api.example.com/rels/{rel}'
109
- # end
110
- #
111
- # @param [String] template
112
- # A valid URI template containing +{rel}+
113
- #
114
- def rel_template(template)
115
- config.policy_options[:rel_template] = template
116
- end
117
-
118
- # Set the namespace (Ruby module) that contains your mappers
119
- #
120
- # When your mappers don't live at the top-level, then set this
121
- # so Yaks can correctly infer the mapper class from the model
122
- # class.
123
- #
124
- # @example
125
- #
126
- # yaks = Yaks.new do
127
- # mapper_namespace API::Mappers
128
- # end
129
- #
130
- # module API::Mappers
131
- # class FruitMapper < Yaks::Mapper
132
- # ...
133
- # end
134
- # end
135
- #
136
- # class Fruit < BaseModel
137
- # ...
138
- # end
139
- #
140
- # @param [Module] namespace
141
- #
142
- def mapper_namespace(namespace)
143
- config.policy_options[:namespace] = namespace
144
- end
145
- alias namespace mapper_namespace
146
-
147
- # @param [Array] args
148
- # @param [Proc] block
149
- def map_to_primitive(*args, &block)
150
- config.primitivize.map(*args, &block)
151
- end
152
-
153
- # Will define each method available in the DefaultPolicy upon the DSL
154
- # and then make it available to apply to any Class taking on the
155
- # `@policies` Array.
156
- DefaultPolicy.public_instance_methods(false).each do |method|
157
- define_method method do |&block|
158
- @policies << proc {
159
- define_method method, &block
160
- }
161
- end
162
- end
163
- end
164
- end
165
- end