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
@@ -1,100 +0,0 @@
1
- module Yaks
2
- class DefaultPolicy
3
- include Util
4
-
5
- # Default policy options.
6
- DEFAULTS = {
7
- rel_template: "rel:{rel}",
8
- namespace: Kernel
9
- }
10
-
11
- # @!attribute [r]
12
- # @return [Hash]
13
- attr_reader :options
14
-
15
- # @param options [Hash] options
16
- def initialize(options = {})
17
- @options = DEFAULTS.merge(options)
18
- end
19
-
20
- # @param model [Object]
21
- # @return [Class] A mapper, typically a subclass of Yaks::Mapper
22
- #
23
- # @raise [NameError] only occurs when the model is anything but a collection.
24
- def derive_mapper_from_object(model)
25
- if model.respond_to? :to_ary
26
- if m = model.first
27
- name = m.class.name.split('::').last + 'CollectionMapper'
28
- begin
29
- return @options[:namespace].const_get(name)
30
- rescue NameError
31
- end
32
- end
33
- begin
34
- return @options[:namespace].const_get(:CollectionMapper)
35
- rescue NameError
36
- end
37
- CollectionMapper
38
- else
39
- name = model.class.name.split('::').last
40
- @options[:namespace].const_get(name + 'Mapper')
41
- end
42
- end
43
-
44
- # Derive the a mapper type name
45
- #
46
- # This returns the 'system name' for a mapper,
47
- # e.g. ShowEventMapper => show_event.
48
- #
49
- # @param [Class] mapper_class
50
- #
51
- # @return [String]
52
- def derive_type_from_mapper_class(mapper_class)
53
- underscore(mapper_class.name.split('::').last.sub(/Mapper$/, ''))
54
- end
55
-
56
- # Derive the mapper type name from a collection
57
- #
58
- # This inspects the first element of the collection, so it
59
- # requires a non-empty collection. Will return nil if the
60
- # collection is empty.
61
- #
62
- # @param [#first] collection
63
- #
64
- # @return [String|nil]
65
- #
66
- # @raise [NameError]
67
- def derive_type_from_collection(collection)
68
- if collection.any?
69
- derive_type_from_mapper_class(
70
- derive_mapper_from_object(collection.first)
71
- )
72
- end
73
- end
74
-
75
- def derive_mapper_from_association(association)
76
- @options[:namespace].const_get("#{camelize(association.singular_name)}Mapper")
77
- end
78
-
79
- # @param association [Yaks::Mapper::Association]
80
- # @return [String]
81
- def derive_rel_from_association(association)
82
- expand_rel( association.name )
83
- end
84
-
85
- # @param relname [String]
86
- # @return [String]
87
- def expand_rel(relname)
88
- URITemplate.new(@options[:rel_template]).expand(rel: relname)
89
- end
90
-
91
- # @param format_class [Class]
92
- # @return [#call] format_class
93
- def serializer_for_format(format_class)
94
- {
95
- json: JSON.method(:pretty_generate),
96
- html: :to_html.to_proc
97
- }.fetch(format_class.serializer)
98
- end
99
- end
100
- end
@@ -1,87 +0,0 @@
1
- module Yaks
2
- class Format
3
- extend Forwardable
4
- include Util
5
- include FP::Callable
6
-
7
- # @!attribute [r] options
8
- # @return [Hash]
9
- attr_reader :options
10
-
11
- def_delegators :resource, :links, :attributes, :subresources
12
-
13
- protected :links, :attributes, :subresources, :options
14
-
15
- # @param [Hash] options
16
- # @return [Hash]
17
- def initialize(options = {})
18
- @options = options
19
- end
20
-
21
- # @param [Yaks::Resource] resource
22
- # @return [Hash]
23
- def call(resource)
24
- serialize_resource(resource)
25
- end
26
- alias serialize call
27
-
28
- class << self
29
- attr_reader :format_name, :serializer, :mime_type
30
-
31
- def all
32
- @formats ||= []
33
- end
34
-
35
- # @param [Constant] klass
36
- # @param [Symbol] format_name
37
- # @param [String] mime_type
38
- # @return [Array]
39
- def register(format_name, serializer, mime_type)
40
- @format_name = format_name
41
- @serializer = serializer
42
- @mime_type = mime_type
43
-
44
- Format.all << self
45
- end
46
-
47
- # @param [Symbol] format_name
48
- # @return [Constant]
49
- # @raise [KeyError]
50
- def by_name(format_name)
51
- find(:format_name, format_name)
52
- end
53
-
54
- # @param [Symbol] mime_type
55
- # @return [Constant]
56
- # @raise [KeyError]
57
- def by_mime_type(mime_type)
58
- find(:mime_type, mime_type)
59
- end
60
-
61
- def by_accept_header(accept_header)
62
- mime_type = Rack::Accept::Charset.new(accept_header).best_of(mime_types.values)
63
- if mime_type
64
- by_mime_type(mime_type)
65
- else
66
- yield if block_given?
67
- end
68
- end
69
-
70
- def mime_types
71
- Format.all.each_with_object({}) do
72
- |format, memo| memo[format.format_name] = format.mime_type
73
- end
74
- end
75
-
76
- def names
77
- mime_types.keys
78
- end
79
-
80
- private
81
-
82
- def find(key, cond)
83
- Format.all.detect {|format| format.send(key) == cond }
84
- end
85
- end
86
- end
87
- end
@@ -1,42 +0,0 @@
1
- module Yaks
2
- class Format
3
- class CollectionJson < self
4
- register :collection_json, :json, 'application/vnd.collection+json'
5
-
6
- include FP
7
-
8
- # @param [Yaks::Resource] resource
9
- # @return [Hash]
10
- def serialize_resource(resource)
11
- result = {
12
- version: "1.0",
13
- items: serialize_items(resource)
14
- }
15
- result[:href] = resource.self_link.uri if resource.self_link
16
- {collection: result}
17
- end
18
-
19
- # @param [Yaks::Resource] resource
20
- # @return [Array]
21
- def serialize_items(resource)
22
- resource.map do |item|
23
- attrs = item.attributes.map do |name, value|
24
- {
25
- name: name,
26
- value: value
27
- }
28
- end
29
- result = { data: attrs }
30
- result[:href] = item.self_link.uri if item.self_link
31
- item.links.each do |link|
32
- next if link.rel.equal? :self
33
- result[:links] = [] unless result.key?(:links)
34
- result[:links] << {rel: link.rel, href: link.uri}
35
- result[:links].last[:name] = link.title if link.title
36
- end
37
- result
38
- end
39
- end
40
- end
41
- end
42
- end
@@ -1,82 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- module Yaks
4
- class Format
5
- # Hypertext Application Language (http://stateless.co/hal_specification.html)
6
- #
7
- # A lightweight JSON Hypermedia message format.
8
- #
9
- # Options: +:plural_links+ In HAL, a single rel can correspond to
10
- # a single link, or to a list of links. Which rels are singular
11
- # and which are plural is application-dependant. Yaks assumes all
12
- # links are singular. If your resource might contain multiple
13
- # links for the same rel, then configure that rel to be plural. In
14
- # that case it will always be rendered as a collection, even when
15
- # the resource only contains a single link.
16
- #
17
- # @example
18
- #
19
- # yaks = Yaks.new do
20
- # format_options :hal, {plural_links: [:related_content]}
21
- # end
22
- #
23
- class Hal < self
24
- register :hal, :json, 'application/hal+json'
25
-
26
- protected
27
-
28
- # @param [Yaks::Resource] resource
29
- # @return [Hash]
30
- def serialize_resource(resource)
31
- # The HAL spec doesn't say explicitly how to deal missing values,
32
- # looking at client behavior (Hyperagent) it seems safer to return an empty
33
- # resource.
34
- #
35
- result = resource.attributes
36
- result = result.merge(:_links => serialize_links(resource.links)) unless resource.links.empty?
37
- result = result.merge(:_embedded => serialize_embedded(resource.subresources)) unless resource.subresources.empty?
38
- result
39
- end
40
-
41
- # @param [Array] links
42
- # @return [Hash]
43
- def serialize_links(links)
44
- links.reduce({}, &method(:serialize_link))
45
- end
46
-
47
- # @param [Hash] memo
48
- # @param [Yaks::Resource::Link]
49
- # @return [Hash]
50
- def serialize_link(memo, link)
51
- hal_link = {href: link.uri}
52
- hal_link.merge!(link.options)
53
-
54
- memo[link.rel] = if singular?(link.rel)
55
- hal_link
56
- else
57
- (memo[link.rel] || []) + [hal_link]
58
- end
59
- memo
60
- end
61
-
62
- # @param [String] rel
63
- # @return [Boolean]
64
- def singular?(rel)
65
- !options.fetch(:plural_links) { [] }.include?(rel)
66
- end
67
-
68
- # @param [Array] subresources
69
- # @return [Hash]
70
- def serialize_embedded(subresources)
71
- subresources.each_with_object({}) do |(rel, resources), memo|
72
- memo[rel] = if resources.collection?
73
- resources.map( &method(:serialize_resource) )
74
- else
75
- serialize_resource(resources) unless resources.null_resource?
76
- end
77
- end
78
- end
79
-
80
- end
81
- end
82
- end
@@ -1,86 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- module Yaks
4
- class Format
5
- class JsonAPI < self
6
- register :json_api, :json, 'application/vnd.api+json'
7
-
8
- include FP
9
-
10
- # @param [Yaks::Resource] resource
11
- # @return [Hash]
12
- def call(resource)
13
- main_collection = resource.map(&method(:serialize_resource))
14
-
15
- { pluralize(resource.type) => main_collection }.tap do |serialized|
16
- linked = resource.each_with_object({}) do |res, hsh|
17
- serialize_linked_subresources(res.subresources, hsh)
18
- end
19
- serialized.merge!(linked: linked) unless linked.empty?
20
- end
21
- end
22
-
23
- # @param [Yaks::Resource] resource
24
- # @return [Hash]
25
- def serialize_resource(resource)
26
- result = resource.attributes
27
-
28
- unless resource.subresources.empty?
29
- result[:links] = serialize_links(resource.subresources)
30
- end
31
-
32
- if resource.self_link && !result.key?(:href)
33
- result[:href] = resource.self_link.uri
34
- end
35
-
36
- result
37
- end
38
-
39
- # @param [Yaks::Resource] subresource
40
- # @return [Hash]
41
- def serialize_links(subresources)
42
- subresources.each_with_object({}) do |(_name, resource), hsh|
43
- next if resource.instance_of? NullResource
44
- key = resource.collection? ? pluralize(resource.type) : resource.type
45
- hsh[key] = serialize_link(resource)
46
- end
47
- end
48
-
49
- # @param [Yaks::Resource] resource
50
- # @return [Array, String]
51
- def serialize_link(resource)
52
- resource.collection? ? resource.map(&send_with_args(:[], :id)) : resource[:id]
53
- end
54
-
55
- # @param [Hash] subresources
56
- # @param [Hash] hsh
57
- # @return [Hash]
58
- def serialize_linked_subresources(subresources, hsh)
59
- subresources.values.each do |resources|
60
- serialize_linked_resources(resources, hsh)
61
- end
62
- end
63
-
64
- # @param [Array] resources
65
- # @param [Hash] linked
66
- # @return [Hash]
67
- def serialize_linked_resources(resources, linked)
68
- resources.each_with_object(linked) do |resource, memo|
69
- serialize_subresource(resource, memo)
70
- end
71
- end
72
-
73
- # {shows => [{id: 3, name: 'foo'}]}
74
- #
75
- # @param [Yaks::Resource] resource
76
- # @param [Hash] linked
77
- # @return [Hash]
78
- def serialize_subresource(resource, linked)
79
- key = pluralize(resource.type)
80
- set = linked.fetch(key) { Set.new }
81
- linked[key] = (set << serialize_resource(resource))
82
- serialize_linked_subresources(resource.subresources, linked)
83
- end
84
- end
85
- end
86
- end
data/yaks/lib/yaks/fp.rb DELETED
@@ -1,26 +0,0 @@
1
- module Yaks
2
- module FP
3
- extend self
4
-
5
- # @param [Symbol, String] name
6
- # @return [Proc]
7
- def curry_method(name)
8
- method(name).to_proc.curry
9
- end
10
-
11
-
12
- # @return [Proc]
13
- def identity_function
14
- ->(x) {x}
15
- end
16
- I = identity_function
17
-
18
- # @param [Symbol] symbol
19
- # @param [Array] args
20
- # @param [Proc] blk
21
- # @return [Proc]
22
- def send_with_args(symbol, *args, &blk)
23
- ->(obj) { obj.method(symbol).(*args, &blk) }
24
- end
25
- end
26
- end
@@ -1,9 +0,0 @@
1
- module Yaks
2
- module FP
3
- module Callable
4
- def to_proc
5
- method(:call).to_proc
6
- end
7
- end
8
- end
9
- end
@@ -1,19 +0,0 @@
1
- module Yaks
2
- module FP
3
-
4
- class HashUpdatable < Module
5
- # @param [Array] attributes
6
- # @return [Symbol]
7
- def initialize(*attributes)
8
- define_method :update do |updates|
9
- self.class.new(
10
- attributes.each_with_object({}) {|attr, hsh|
11
- hsh[attr] = updates.fetch(attr) { send(attr) }
12
- }
13
- )
14
- end
15
- end
16
- end
17
-
18
- end
19
- end