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,17 +0,0 @@
1
- module Yaks
2
- module FP
3
-
4
- class Updatable < Module
5
- # @param [Array] attributes
6
- # @return [Symbol]
7
- def initialize(*attributes)
8
- define_method :update do |updates|
9
- self.class.new(
10
- *attributes.map {|attr| updates.fetch(attr) { send(attr) }}
11
- )
12
- end
13
- end
14
- end
15
-
16
- end
17
- end
@@ -1,75 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- module Yaks
4
- class Mapper
5
- extend ClassMethods, Forwardable
6
- include Util, FP
7
-
8
- def_delegators 'self.class', :config
9
- def_delegators :config, :attributes, :links, :associations
10
-
11
- attr_reader :object, :context
12
-
13
- def initialize(context)
14
- @context = context
15
- end
16
-
17
- def policy
18
- context.fetch(:policy)
19
- end
20
-
21
- def env
22
- context.fetch(:env)
23
- end
24
-
25
- def mapper_stack
26
- context.fetch(:mapper_stack)
27
- end
28
-
29
- def self.mapper_name(policy)
30
- config.type || policy.derive_type_from_mapper_class(self)
31
- end
32
-
33
- def mapper_name
34
- self.class.mapper_name(policy)
35
- end
36
-
37
- def call(object)
38
- @object = object
39
-
40
- return NullResource.new if object.nil?
41
-
42
- [ :map_attributes,
43
- :map_links,
44
- :map_subresources
45
- ].inject(Resource.new(type: mapper_name)) do |resource, method|
46
- send(method, resource)
47
- end
48
- end
49
-
50
- def load_attribute(name)
51
- respond_to?(name) ? public_send(name) : object.public_send(name)
52
- end
53
- alias load_association load_attribute
54
-
55
- private
56
-
57
- def map_attributes(resource)
58
- attributes.inject(resource) do |res, attribute|
59
- attribute.add_to_resource(res, self, context)
60
- end
61
- end
62
-
63
- def map_links(resource)
64
- links.inject(resource) do |res, mapper_link|
65
- mapper_link.add_to_resource(res, self, context)
66
- end
67
- end
68
-
69
- def map_subresources(resource)
70
- associations.inject(resource) do |res, association|
71
- association.add_to_resource(res, self, context)
72
- end
73
- end
74
- end
75
- end
@@ -1,43 +0,0 @@
1
- module Yaks
2
- class Mapper
3
- class Association
4
- include Equalizer.new(:name, :child_mapper, :rel, :href, :link_if)
5
- include Util
6
-
7
- attr_reader :name, :child_mapper, :rel, :href, :link_if
8
-
9
- def initialize(options)
10
- @name = options.fetch(:name)
11
- @child_mapper = options.fetch(:mapper, Undefined)
12
- @rel = options.fetch(:rel, Undefined)
13
-
14
- @href = options.fetch(:href, Undefined)
15
- @link_if = options.fetch(:link_if, Undefined)
16
- end
17
-
18
- def add_to_resource(resource, parent_mapper, context)
19
- AssociationMapper.new(parent_mapper, self, context).call(resource)
20
- end
21
-
22
- def render_as_link?(parent_mapper)
23
- href != Undefined && link_if != Undefined && Resolve(link_if, parent_mapper)
24
- end
25
-
26
- def map_rel(policy)
27
- return rel unless rel.equal?(Undefined)
28
- policy.derive_rel_from_association(self)
29
- end
30
-
31
- # @abstract
32
- def map_resource(_object, _context)
33
- end
34
-
35
- # support for HasOne and HasMany
36
- def resolve_association_mapper(policy)
37
- return child_mapper unless child_mapper.equal?(Undefined)
38
- policy.derive_mapper_from_association(self)
39
- end
40
-
41
- end
42
- end
43
- end
@@ -1,42 +0,0 @@
1
- module Yaks
2
- class Mapper
3
- class AssociationMapper
4
- attr_reader :parent_mapper, :context, :rel, :association
5
-
6
- def initialize(parent_mapper, association, context)
7
- @parent_mapper = parent_mapper
8
- @association = association
9
- @context = context.merge(
10
- mapper_stack: context[:mapper_stack] + [parent_mapper]
11
- )
12
- @rel = association.map_rel(policy)
13
- end
14
-
15
- def policy
16
- context.fetch(:policy)
17
- end
18
-
19
- def call(resource)
20
- if association.render_as_link?(parent_mapper)
21
- add_link(resource)
22
- else
23
- add_subresource(resource)
24
- end
25
- end
26
-
27
- def add_link(resource)
28
- Link.new(rel, association.href, {})
29
- .add_to_resource(resource, parent_mapper, nil)
30
- # Yaks::Mapper::Link doesn't do anything with the context, making it
31
- # hard to test that we pass it a context. Passing nil for now, until
32
- # this is actually needed and can be tested.
33
- end
34
-
35
- def add_subresource(resource)
36
- object = parent_mapper.load_association(association.name)
37
- subresource = association.map_resource(object, context)
38
- resource.add_subresource(rel, subresource)
39
- end
40
- end
41
- end
42
- end
@@ -1,17 +0,0 @@
1
- module Yaks
2
- class Mapper
3
- class Attribute
4
- include Equalizer.new(:name)
5
-
6
- attr_reader :name
7
-
8
- def initialize(name)
9
- @name = name
10
- end
11
-
12
- def add_to_resource(resource, mapper, _context)
13
- resource.update_attributes(name => mapper.load_attribute(name))
14
- end
15
- end
16
- end
17
- end
@@ -1,37 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- module Yaks
4
- class Mapper
5
- module ClassMethods
6
- include Forwardable
7
- include Util
8
- include FP
9
-
10
- CONFIG_METHODS = [
11
- :type,
12
- :attributes,
13
- :link,
14
- :profile,
15
- :has_one,
16
- :has_many
17
- ]
18
-
19
- def config
20
- @config ||= Config.new nil, [], [], []
21
- @config = yield(@config) if block_given?
22
- @config
23
- end
24
-
25
- def inherited(child)
26
- child.config { @config }
27
- end
28
-
29
- CONFIG_METHODS.each do |method_name|
30
- define_method method_name do |*args|
31
- config &send_with_args(method_name, *args)
32
- end
33
- end
34
-
35
- end
36
- end
37
- end
@@ -1,44 +0,0 @@
1
- module Yaks
2
- class Mapper
3
- class Config
4
- include Equalizer.new(:type, :attributes, :links, :associations)
5
- include FP::Updatable.new(:type, :attributes, :links, :associations)
6
-
7
- attr_reader :links, :associations
8
-
9
- def initialize(type, attributes, links, associations)
10
- @type = type
11
- @attributes = attributes
12
- @links = links
13
- @associations = associations
14
- end
15
-
16
- def type(type = Undefined)
17
- return @type if type.equal?(Undefined)
18
- update(type: type)
19
- end
20
-
21
- def attributes(*attrs)
22
- return @attributes if attrs.empty?
23
- update(attributes: @attributes + attrs.map(&Attribute.method(:new)))
24
- end
25
-
26
- def link(rel, template, options = {})
27
- update(links: @links + [Link.new(rel, template, options)])
28
- end
29
-
30
- def add_association(type, name, options)
31
- update(associations: @associations + [type.new(options.merge(name: name))])
32
- end
33
-
34
- def has_one(name, options = {})
35
- add_association(HasOne, name, options)
36
- end
37
-
38
- def has_many(name, options = {})
39
- add_association(HasMany, name, options)
40
- end
41
-
42
- end
43
- end
44
- end
@@ -1,29 +0,0 @@
1
- module Yaks
2
- class Mapper
3
- class HasMany < Association
4
- include Util
5
-
6
- def initialize(options)
7
- super
8
- @collection_mapper = options.fetch(:collection_mapper, Undefined)
9
- end
10
-
11
- def map_resource(collection, context)
12
- return NullResource.new(collection: true) if collection.nil?
13
- policy = context.fetch(:policy)
14
- item_mapper = resolve_association_mapper(policy)
15
- context = context.merge(item_mapper: item_mapper)
16
- collection_mapper(collection, policy).new(context).call(collection)
17
- end
18
-
19
- def collection_mapper(collection, policy)
20
- return @collection_mapper unless @collection_mapper.equal? Undefined
21
- policy.derive_mapper_from_object(collection)
22
- end
23
-
24
- def singular_name
25
- singularize(name.to_s)
26
- end
27
- end
28
- end
29
- end
@@ -1,15 +0,0 @@
1
- module Yaks
2
- class Mapper
3
- class HasOne < Association
4
- def map_resource(object, context)
5
- resolve_association_mapper(context.fetch(:policy))
6
- .new(context)
7
- .call(object)
8
- end
9
-
10
- def singular_name
11
- name.to_s
12
- end
13
- end
14
- end
15
- end
@@ -1,91 +0,0 @@
1
- module Yaks
2
- class Mapper
3
- # A Yaks::Mapper::Link is part of a mapper's configuration. It captures
4
- # what is set through the mapper's class level `#link` function, and is
5
- # capable of generating a `Yaks::Resource::Link` for a given mapper
6
- # instance (and hence subject).
7
- #
8
- # @example
9
- # link :self, 'http://api.foo.org/users/{id}', title: ->{ "User #{object.name}" }
10
- # link :profile, 'http://apidocs.foo.org/profiles/users'
11
- # link 'http://apidocs.foo.org/rels/friends', 'http://api.foo.org/users/{id}/friends?page={page}', expand: [:id]
12
- #
13
- # It takes a relationship identifier, a URI template and an options hash.
14
- #
15
- # @param rel [Symbol|String] Either a registered relationship type (Symbol)
16
- # or a relationship URI. See [RFC5988 Web Linking](http://tools.ietf.org/html/rfc5988)
17
- # @param template [String] A [RFC6570](http://tools.ietf.org/html/rfc6570) URI template
18
- # @param template [Symbol] A method name that generates the link. No more expansion is done afterwards
19
- # @option expand [Boolean] pass false to pass on the URI template in the response,
20
- # instead of expanding the variables
21
- # @option expand [Array[Symbol]] pass a list of variable names to only expand those,
22
- # and return a partially expanded URI template in the response
23
- # @option title [String] Give the link a title
24
- # @option title [#to_proc] Block that returns the title. If it takes an argument,
25
- # it will receive the mapper instance as argument. Otherwise it is evaluated in the mapper context
26
- class Link
27
- extend Forwardable
28
- include Concord.new(:rel, :template, :options)
29
- include Util
30
-
31
- def_delegators :uri_template, :expand_partial
32
-
33
- def add_to_resource(resource, mapper, _context)
34
- resource_link = map_to_resource_link(mapper)
35
- return resource unless resource_link
36
- resource.add_link(resource_link)
37
- end
38
-
39
- def rel?(rel)
40
- rel().eql? rel
41
- end
42
-
43
- # A link is templated if it does not expand, or only partially
44
- def templated?
45
- !options.fetch(:expand) { true }.equal? true
46
- end
47
-
48
- def template_variables
49
- case options.fetch(:expand) { true }
50
- when true
51
- uri_template.variables
52
- when false
53
- []
54
- else
55
- options[:expand]
56
- end.map(&:to_sym)
57
- end
58
-
59
- def expansion_mapping(lookup)
60
- template_variables.each_with_object({}) do |name, hsh|
61
- hsh[name] = lookup[name]
62
- end
63
- end
64
-
65
- def uri_template
66
- URITemplate.new(template)
67
- end
68
-
69
- def map_to_resource_link(mapper)
70
- uri = expand_with(mapper.method(:load_attribute))
71
- return if uri.nil?
72
-
73
- Resource::Link.new(rel, uri, resource_link_options(mapper))
74
- end
75
-
76
- def expand_with(lookup)
77
- return lookup[template] if template.instance_of? Symbol
78
-
79
- expand_partial(expansion_mapping(lookup)).to_s
80
- end
81
-
82
- def resource_link_options(mapper)
83
- options = options()
84
- options = options.merge(title: Resolve(options[:title], mapper)) if options.key?(:title)
85
- options = options.merge(templated: true) if templated?
86
- options.reject{|key| key.equal? :expand }
87
- end
88
-
89
- end
90
- end
91
- end
@@ -1,40 +0,0 @@
1
- module Yaks
2
- class NullResource
3
- include Equalizer.new(:collection?)
4
- include Enumerable
5
-
6
- def initialize(opts = {})
7
- @collection = opts.fetch(:collection) { false }
8
- end
9
-
10
- def each
11
- to_enum
12
- end
13
-
14
- def attributes
15
- {}
16
- end
17
-
18
- def links
19
- []
20
- end
21
-
22
- def subresources
23
- {}
24
- end
25
-
26
- def [](*)
27
- end
28
-
29
- def type
30
- end
31
-
32
- def collection?
33
- @collection
34
- end
35
-
36
- def null_resource?
37
- true
38
- end
39
- end
40
- end