yaks 0.7.7 → 0.8.0.alpha

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec +1 -0
  3. data/Rakefile +1 -1
  4. data/lib/yaks/attributes.rb +17 -11
  5. data/lib/yaks/builder.rb +43 -0
  6. data/lib/yaks/changelog.rb +2 -2
  7. data/lib/yaks/collection_mapper.rb +0 -2
  8. data/lib/yaks/config.rb +52 -32
  9. data/lib/yaks/configurable.rb +43 -14
  10. data/lib/yaks/errors.rb +1 -0
  11. data/lib/yaks/format/collection_json.rb +37 -5
  12. data/lib/yaks/format/hal.rb +8 -2
  13. data/lib/yaks/format/halo.rb +16 -9
  14. data/lib/yaks/format/json_api.rb +21 -2
  15. data/lib/yaks/format.rb +21 -15
  16. data/lib/yaks/html5_forms.rb +3 -2
  17. data/lib/yaks/identifier/link_relation.rb +17 -0
  18. data/lib/yaks/mapper/attribute.rb +2 -2
  19. data/lib/yaks/mapper/config.rb +1 -15
  20. data/lib/yaks/mapper/form/config.rb +21 -0
  21. data/lib/yaks/mapper/form/field/option.rb +20 -0
  22. data/lib/yaks/mapper/form/field.rb +13 -26
  23. data/lib/yaks/mapper/form/fieldset.rb +43 -0
  24. data/lib/yaks/mapper/form.rb +41 -16
  25. data/lib/yaks/mapper/link.rb +9 -7
  26. data/lib/yaks/mapper.rb +15 -7
  27. data/lib/yaks/null_resource.rb +2 -1
  28. data/lib/yaks/pipeline.rb +42 -0
  29. data/lib/yaks/primitivize.rb +0 -1
  30. data/lib/yaks/reader/hal.rb +63 -0
  31. data/lib/yaks/resource/form/field.rb +19 -0
  32. data/lib/yaks/resource/form/fieldset.rb +13 -0
  33. data/lib/yaks/resource/form.rb +6 -10
  34. data/lib/yaks/resource.rb +19 -2
  35. data/lib/yaks/runner.rb +13 -38
  36. data/lib/yaks/serializer.rb +34 -1
  37. data/lib/yaks/util.rb +8 -2
  38. data/lib/yaks/version.rb +1 -1
  39. data/lib/yaks.rb +37 -10
  40. data/resources/iana-link-relations.csv +152 -0
  41. data/spec/acceptance/acceptance_spec.rb +4 -3
  42. data/spec/acceptance/json_shared_examples.rb +11 -0
  43. data/spec/acceptance/models.rb +1 -1
  44. data/spec/integration/dynamic_form_fields_spec.rb +36 -0
  45. data/spec/integration/fieldset_spec.rb +62 -0
  46. data/spec/integration/map_to_resource_spec.rb +0 -2
  47. data/spec/json/confucius.halo.json +0 -1
  48. data/spec/sanity_spec.rb +0 -2
  49. data/spec/spec_helper.rb +3 -3
  50. data/spec/unit/yaks/attributes_spec.rb +3 -5
  51. data/spec/unit/yaks/{stateful_builder_spec.rb → builder_spec.rb} +8 -10
  52. data/spec/unit/yaks/collection_mapper_spec.rb +0 -2
  53. data/spec/unit/yaks/collection_resource_spec.rb +0 -2
  54. data/spec/unit/yaks/config_spec.rb +9 -15
  55. data/spec/unit/yaks/default_policy/derive_mapper_from_object_spec.rb +0 -2
  56. data/spec/unit/yaks/default_policy_spec.rb +0 -2
  57. data/spec/unit/yaks/format/collection_json_spec.rb +195 -3
  58. data/spec/unit/yaks/format/hal_spec.rb +0 -2
  59. data/spec/unit/yaks/format/halo_spec.rb +0 -1
  60. data/spec/unit/yaks/format/html_spec.rb +0 -2
  61. data/spec/unit/yaks/format/json_api_spec.rb +0 -2
  62. data/spec/unit/yaks/format_spec.rb +4 -6
  63. data/spec/unit/yaks/fp/callable_spec.rb +0 -2
  64. data/spec/unit/yaks/fp_spec.rb +0 -2
  65. data/spec/unit/yaks/mapper/association_mapper_spec.rb +0 -2
  66. data/spec/unit/yaks/mapper/association_spec.rb +0 -2
  67. data/spec/unit/yaks/mapper/attribute_spec.rb +0 -2
  68. data/spec/unit/yaks/mapper/config_spec.rb +6 -167
  69. data/spec/unit/yaks/mapper/form/field_spec.rb +0 -2
  70. data/spec/unit/yaks/mapper/form_spec.rb +2 -26
  71. data/spec/unit/yaks/mapper/has_many_spec.rb +0 -2
  72. data/spec/unit/yaks/mapper/has_one_spec.rb +0 -2
  73. data/spec/unit/yaks/mapper/link_spec.rb +13 -2
  74. data/spec/unit/yaks/mapper_spec.rb +2 -10
  75. data/spec/unit/yaks/null_resource_spec.rb +2 -4
  76. data/spec/unit/yaks/pipeline_spec.rb +140 -0
  77. data/spec/unit/yaks/primitivize_spec.rb +0 -2
  78. data/spec/unit/yaks/resource/link_spec.rb +0 -2
  79. data/spec/unit/yaks/resource_spec.rb +2 -4
  80. data/spec/unit/yaks/runner_spec.rb +52 -92
  81. data/spec/unit/yaks/serializer_spec.rb +0 -2
  82. data/spec/unit/yaks/util_spec.rb +12 -2
  83. metadata +58 -23
  84. data/lib/yaks/config/dsl.rb +0 -174
  85. data/lib/yaks/mapper/class_methods.rb +0 -47
  86. data/lib/yaks/stateful_builder.rb +0 -63
  87. data/spec/unit/yaks/config/dsl_spec.rb +0 -92
  88. data/spec/unit/yaks/configurable_spec.rb +0 -55
  89. data/spec/unit/yaks/mapper/class_methods_spec.rb +0 -83
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 207388cedc68055dcef4f732bd82ce81180871c8
4
- data.tar.gz: 97c121f6e231e8a409d1b2bca7515d75f42d261a
3
+ metadata.gz: ae9407b2b6c0f2f15c86276063e02ea2d7f9a4f7
4
+ data.tar.gz: 776d6202349cbf8854506a3d89f06134f2bee12f
5
5
  SHA512:
6
- metadata.gz: fd61481333faaccde1120268996ab50462dcfd2c04f0b08e9340300ad6567a7bf47d59eae2b36471d9661cb9c020304566df8267c3c19f262a77346b7924504e
7
- data.tar.gz: 3acfcdfa41752e295833ca61052fbad706e51bae6132eb7f72350bbfd5db1e0d6fa42d1338059d6fa758396d48d417f2c699a8fcfc40888074afde7189c03dfb
6
+ metadata.gz: 23ec9ff975f4a4da763720f8ce5d11d8346c1e23f592070e9517dc340605d5221adad9e9a9c64db38158fc88af4b03d21717fc478137dbf4a2f85836a99dd417
7
+ data.tar.gz: 64f8c4e3078416d4ccffdd572c8eadd0606d5e11c950085f16d5fede07aea162e3338c038284d92c2bbc1974c0a32e98fda97aa4a8d17d1a0d40755c17ba49ad
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ -r spec_helper
data/Rakefile CHANGED
@@ -33,13 +33,13 @@ task :mutant_chunked do
33
33
  # Yaks::Attributes::InstanceMethods,
34
34
  # Yaks::Configurable,
35
35
  # Yaks::NullResource,
36
+ # Yaks::Runner,
36
37
 
37
38
  [
38
39
  # Yaks::Resource::Form, # no explicit coverage
39
40
  # Yaks::Resource::Form::Field, # no explicit coverage
40
41
  # Yaks::Mapper::Form::Field::Option, # no explicit coverage
41
42
  # Yaks::Format::HTML, # no explicit coverage
42
- Yaks::Runner, # 56/509 # 89.55%
43
43
  Yaks::Resource, # 35/206 # 83.01%
44
44
  Yaks::StatefulBuilder, # 25/192 # 86.98%
45
45
  Yaks::Format::Halo, # 24/232 # 89.66%
@@ -12,19 +12,21 @@ module Yaks
12
12
  self.class.new(*[*(names+attrs), @defaults.merge(defaults)])
13
13
  end
14
14
 
15
+ def remove(*attrs)
16
+ self.class.new(*[*(names-attrs), @defaults.reject {|k,v| attrs.include?(k) }])
17
+ end
18
+
15
19
  def included(descendant)
16
20
  descendant.module_exec(self) do |this|
17
21
  include InstanceMethods,
18
22
  Anima.new(*this.names),
19
23
  Anima::Update
20
24
 
25
+ alias with update
26
+
21
27
  this.names.each do |attr|
22
- define_method attr do |value = Undefined|
23
- if value.equal? Undefined
24
- instance_variable_get("@#{attr}")
25
- else
26
- update(attr => value)
27
- end
28
+ define_method "with_#{attr}" do |value|
29
+ with(attr => value)
28
30
  end
29
31
  end
30
32
 
@@ -38,17 +40,21 @@ module Yaks
38
40
  end
39
41
 
40
42
  def append_to(type, *objects)
41
- update(type => instance_variable_get("@#{type}") + objects)
43
+ with(type => instance_variable_get("@#{type}") + objects)
44
+ end
45
+
46
+ def to_h_compact
47
+ defaults = self.class.attributes.defaults
48
+ to_h.reject do |attr, value|
49
+ value.equal?(defaults[attr])
50
+ end
42
51
  end
43
52
 
44
53
  def pp
45
54
  indent = ->(str) { str.lines.map {|l| " #{l}"}.join }
46
55
  format = ->(val) { val.respond_to?(:pp) ? val.pp : val.inspect }
47
56
 
48
- defaults = self.class.attributes.defaults
49
- values = to_h.reject do |attr, value|
50
- value.equal?(defaults[attr])
51
- end
57
+ values = to_h_compact
52
58
 
53
59
  fmt_attrs = values.map do |attr, value|
54
60
  fmt_val = case value
@@ -0,0 +1,43 @@
1
+ module Yaks
2
+ # State monad-ish thing.
3
+ #
4
+ # Generate a DSL syntax for immutable classes.
5
+ #
6
+ # @example
7
+ #
8
+ # # This code
9
+ # Form.create(:search)
10
+ # .method("POST")
11
+ # .action("/search")
12
+ #
13
+ # # Can be written as
14
+ # Builder.new(Form, [:method, :action]).create(:search) do
15
+ # method "POST"
16
+ # action "/search"
17
+ # end
18
+ #
19
+ class Builder
20
+ include Configurable
21
+
22
+ def create(*args, &block)
23
+ build(@klass.create(*args), &block)
24
+ end
25
+
26
+ def build(init_state, *extra_args, &block)
27
+ @config = init_state
28
+ instance_exec(*extra_args, &block) if block
29
+ @config
30
+ end
31
+
32
+ def initialize(klass, methods = [], &block)
33
+ @klass = klass
34
+ @methods = methods
35
+ def_forward *methods if methods.any?
36
+ instance_eval(&block) if block
37
+ end
38
+
39
+ def inspect
40
+ "#<Builder #{@klass} #{@methods.inspect}>"
41
+ end
42
+ end
43
+ end
@@ -7,8 +7,8 @@ module Yaks
7
7
  end
8
8
 
9
9
  def versions
10
- markdown.split(/(?=###\s*[\d\.]+\n)/).each_with_object({}) do |section, hsh|
11
- version = section.each_line.first[/[\d\.]+/]
10
+ markdown.split(/(?=###\s*[\d\w\.]+\n)/).each_with_object({}) do |section, hsh|
11
+ version = section.each_line.first[/[\d\w\.]+/]
12
12
  log = section.each_line.drop(1).join.strip
13
13
  hsh[version] = log
14
14
  end
@@ -1,5 +1,3 @@
1
- # -*- coding: utf-8 -*-
2
-
3
1
  module Yaks
4
2
  class CollectionMapper < Mapper
5
3
  alias collection object
data/lib/yaks/config.rb CHANGED
@@ -1,45 +1,61 @@
1
1
  module Yaks
2
2
  class Config
3
- include Yaks::FP::Callable
3
+ extend Yaks::Util::Deprecated
4
+ include Yaks::FP::Callable,
5
+ Attributes.new(
6
+ format_options_hash: Hash.new({}),
7
+ default_format: :hal,
8
+ policy_options: {},
9
+ policy_class: DefaultPolicy,
10
+ primitivize: Primitivize.create,
11
+ serializers: Serializer.all,
12
+ hooks: []
13
+ )
4
14
 
5
- # @!attribute [r] format_options
6
- # @return [Hash<Symbol,Hash>]
7
- attr_reader :format_options
15
+ class << self
16
+ alias create new
17
+ end
8
18
 
9
- # @!attribute [rw] default_format
10
- # @return [Symbol]
11
- attr_accessor :default_format
19
+ deprecated_alias :namespace, :mapper_namespace
12
20
 
13
- # @!attribute [rw] policy_class
14
- # @return [Class]
15
- attr_accessor :policy_class
21
+ def format_options(format = Undefined, options = Undefined)
22
+ with(format_options_hash: format_options_hash.merge(format => options))
23
+ end
16
24
 
17
- # @!attribute [r] policy_options
18
- # @return [Hash]
19
- attr_reader :policy_options
25
+ def serializer(type, &serializer)
26
+ with(serializers: serializers.merge(type => serializer))
27
+ end
20
28
 
21
- # @!attribute [rw] primitivize
22
- # @return [Primitivize]
23
- attr_accessor :primitivize
29
+ def json_serializer(&serializer)
30
+ with(serializer: :json, &serializer)
31
+ end
24
32
 
25
- # @!attribute [r] serializers
26
- # @return [Hash<Symbol,#call>]
27
- attr_reader :serializers
33
+ %w[before after around skip].map(&:intern).each do |hook_type|
34
+ define_method hook_type do |step, name = :"#{hook_type}_#{step}", &block|
35
+ append_to(:hooks, [hook_type, step, name, block])
36
+ end
37
+ end
28
38
 
29
- # @!attribute [r] hooks
30
- # @return [Array]
31
- attr_reader :hooks
39
+ def rel_template(template)
40
+ with(policy_options: policy_options.merge(:rel_template => template))
41
+ end
32
42
 
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 = Serializer.all.dup
40
- @hooks = []
43
+ def mapper_namespace(namespace)
44
+ with(policy_options: policy_options.merge(:namespace => namespace))
45
+ end
46
+
47
+ def map_to_primitive(*args, &block)
48
+ with(primitivize: primitivize.dup.tap { |prim| prim.map(*args, &block) })
49
+ end
41
50
 
42
- DSL.new(self, &blk)
51
+ DefaultPolicy.public_instance_methods(false).each do |method|
52
+ define_method method do |&block|
53
+ with(
54
+ policy_class: Class.new(policy_class) do
55
+ define_method method, &block
56
+ end
57
+ )
58
+ end
43
59
  end
44
60
 
45
61
  # @return [Yaks::DefaultPolicy]
@@ -67,7 +83,11 @@ module Yaks
67
83
  alias serialize call
68
84
 
69
85
  def map(object, options = {})
70
- runner(object, options).map(object)
86
+ runner(object, options).map
87
+ end
88
+
89
+ def read(data, options = {})
90
+ runner(data, options).read
71
91
  end
72
92
  end
73
93
  end
@@ -1,23 +1,52 @@
1
1
  module Yaks
2
2
  module Configurable
3
- def config_method(name, options)
4
- define_method name do |*args, &block|
3
+ attr_accessor :config
4
+
5
+ def self.extended(child)
6
+ child.config = child::Config.new
7
+ end
8
+
9
+ def inherited(child)
10
+ child.config = config
11
+ end
12
+
13
+ def def_set(*method_names)
14
+ method_names.each do |method_name|
15
+ define_singleton_method method_name do |arg|
16
+ self.config = config.update(method_name => arg)
17
+ end
18
+ end
19
+ end
20
+
21
+ def def_forward(method_names, *args)
22
+ unless method_names.is_a? Hash
23
+ def_forward([method_names, *args].map{|name| {name => name}}.inject(:merge))
24
+ return
25
+ end
26
+ method_names.each do |method_name, target|
27
+ define_singleton_method method_name do |*args, &block|
28
+ self.config = config.public_send(target, *args, &block)
29
+ end
30
+ end
31
+ end
32
+
33
+ def def_add(name, options)
34
+ define_singleton_method name do |*args, &block|
5
35
  defaults = options.fetch(:defaults, {})
6
36
  klass = options.fetch(:create)
7
37
 
8
- instance = if args.length.equal?(1) && args.first.instance_of?(klass)
9
- args.first
10
- else
11
- if args.last.instance_of?(Hash)
12
- args[-1] = defaults.merge(args[-1])
13
- else
14
- args << defaults
15
- end
16
- klass.create(*args, &block)
17
- end
18
-
19
- append_to(options.fetch(:append_to), instance)
38
+ if args.last.instance_of?(Hash)
39
+ args[-1] = defaults.merge(args[-1])
40
+ else
41
+ args << defaults
42
+ end
43
+
44
+ self.config = config.append_to(
45
+ options.fetch(:append_to),
46
+ klass.create(*args, &block)
47
+ )
20
48
  end
21
49
  end
50
+
22
51
  end
23
52
  end
data/lib/yaks/errors.rb CHANGED
@@ -2,6 +2,7 @@ module Yaks
2
2
  Error = Class.new(StandardError)
3
3
 
4
4
  IllegalStateError = Class.new(Error)
5
+ RuntimeError = Class.new(Error)
5
6
  UnsupportedOperationError = Class.new(Error)
6
7
  PrimitivizeError = Class.new(Error)
7
8
  end
@@ -13,7 +13,8 @@ module Yaks
13
13
  items: serialize_items(resource)
14
14
  }
15
15
  result[:href] = resource.self_link.uri if resource.self_link
16
- result[:links] = serialize_links(resource) if resource.collection? && resource.links.any?
16
+ result[:links] = serialize_links(resource) if links? resource
17
+ result[:queries] = serialize_queries(resource) if queries? resource
17
18
  {collection: result}
18
19
  end
19
20
 
@@ -40,11 +41,42 @@ module Yaks
40
41
  end
41
42
 
42
43
  def serialize_links(resource)
43
- result = []
44
- resource.links.each do |link|
45
- result << {href: link.uri, rel: link.rel}
44
+ resource.links.each_with_object([]) do |link, result|
45
+ result << { href: link.uri, rel: link.rel }
46
46
  end
47
- result
47
+ end
48
+
49
+ def serialize_queries(resource)
50
+ resource.forms.each_with_object([]) do |form, result|
51
+ next unless form_is_query? form
52
+
53
+ result << { rel: form.name, href: form.action }
54
+ result.last[:prompt] = form.title if form.title
55
+
56
+ form.fields_flat.each do |field|
57
+ result.last[:data] = [] unless result.last.key? :data
58
+ result.last[:data] << { name: field.name, value: nil.to_s }
59
+ result.last[:data].last[:prompt] = field.label if field.label
60
+ end
61
+ end
62
+ end
63
+
64
+ def queries?(resource)
65
+ resource.forms.any? { |f| form_is_query? f }
66
+ end
67
+
68
+ def links?(resource)
69
+ resource.collection? && resource.links.any?
70
+ end
71
+
72
+ protected
73
+
74
+ def form_is_query?(form)
75
+ method_is_get?(form.method) && !form.action.nil?
76
+ end
77
+
78
+ def method_is_get?(method)
79
+ method.downcase.to_sym === :get
48
80
  end
49
81
  end
50
82
  end
@@ -1,5 +1,3 @@
1
- # -*- coding: utf-8 -*-
2
-
3
1
  module Yaks
4
2
  class Format
5
3
  # Hypertext Application Language (http://stateless.co/hal_specification.html)
@@ -23,6 +21,14 @@ module Yaks
23
21
  class Hal < self
24
22
  register :hal, :json, 'application/hal+json'
25
23
 
24
+ def transitive?
25
+ true
26
+ end
27
+
28
+ def inverse
29
+ Yaks::Reader::Hal.new
30
+ end
31
+
26
32
  protected
27
33
 
28
34
  # @param [Yaks::Resource] resource
@@ -1,5 +1,3 @@
1
- # -*- coding: utf-8 -*-
2
-
3
1
  module Yaks
4
2
  class Format
5
3
  # Extension of Hal loosely based on the example by Mike Kelly given at
@@ -22,20 +20,29 @@ module Yaks
22
20
  end
23
21
 
24
22
  def serialize_form(form)
25
- raw = form.to_h
26
- raw[:href] = raw.delete(:action)
27
- raw[:fields] = form.fields.map do |field|
28
- field.to_h.each_with_object({}) do |(attr,value), hsh|
29
- if attr == :options
23
+ raw = form.to_h_compact
24
+ raw[:href] = raw.delete(:action) if raw[:action]
25
+ raw[:fields] = form.fields.map(&method(:serialize_form_field))
26
+ raw
27
+ end
28
+
29
+ def serialize_form_field(field)
30
+ if field.type == :fieldset
31
+ {
32
+ type: :fieldset,
33
+ fields: field.fields.map(&method(:serialize_form_field))
34
+ }
35
+ else
36
+ field.to_h_compact.each_with_object({}) do |(attr,value), hsh|
37
+ if attr == :options # <option>s of a <select>
30
38
  if !value.empty?
31
39
  hsh[:options] = value.map(&:to_h)
32
40
  end
33
- elsif HTML5Forms::FIELD_OPTIONS[attr] != value
41
+ else
34
42
  hsh[attr] = value
35
43
  end
36
44
  end
37
45
  end
38
- raw
39
46
  end
40
47
  end
41
48
  end
@@ -1,5 +1,3 @@
1
- # -*- coding: utf-8 -*-
2
-
3
1
  module Yaks
4
2
  class Format
5
3
  class JsonAPI < self
@@ -81,6 +79,27 @@ module Yaks
81
79
  linked[key] = (set << serialize_resource(resource))
82
80
  serialize_linked_subresources(resource.subresources, linked)
83
81
  end
82
+
83
+ def inverse
84
+ JsonApi::Reader.new
85
+ end
86
+ end
87
+
88
+ class Reader
89
+ def call(data, env)
90
+ type = data.detect do |key, value|
91
+ key unless key == "links"
92
+ end
93
+
94
+ CollectionResource.new(
95
+ type: type,
96
+ members: map_to_resource(data[type], )
97
+ )
98
+ end
99
+
100
+ def inverse
101
+ JsonApi.new
102
+ end
84
103
  end
85
104
  end
86
105
  end
data/lib/yaks/format.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  module Yaks
2
2
  class Format
3
3
  extend Forwardable
4
- include Util
5
- include FP::Callable
4
+ include Util,
5
+ FP::Callable
6
6
 
7
7
  # @!attribute [r] options
8
8
  # @return [Hash]
@@ -26,7 +26,11 @@ module Yaks
26
26
  alias serialize call
27
27
 
28
28
  class << self
29
- attr_reader :format_name, :serializer, :mime_type
29
+ extend Util::Deprecated
30
+
31
+ attr_reader :format_name, :serializer, :media_type
32
+
33
+ deprecated_alias :mime_type, :media_type
30
34
 
31
35
  def all
32
36
  @formats ||= []
@@ -34,12 +38,12 @@ module Yaks
34
38
 
35
39
  # @param [Constant] klass
36
40
  # @param [Symbol] format_name
37
- # @param [String] mime_type
41
+ # @param [String] media_type
38
42
  # @return [Array]
39
- def register(format_name, serializer, mime_type)
43
+ def register(format_name, serializer, media_type)
40
44
  @format_name = format_name
41
45
  @serializer = serializer
42
- @mime_type = mime_type
46
+ @media_type = media_type
43
47
 
44
48
  Format.all << self
45
49
  end
@@ -51,30 +55,32 @@ module Yaks
51
55
  find(:format_name, format_name)
52
56
  end
53
57
 
54
- # @param [Symbol] mime_type
58
+ # @param [Symbol] media_type
55
59
  # @return [Constant]
56
60
  # @raise [KeyError]
57
- def by_mime_type(mime_type)
58
- find(:mime_type, mime_type)
61
+ def by_media_type(media_type)
62
+ find(:media_type, media_type)
59
63
  end
64
+ deprecated_alias :by_mime_type, :by_media_type
60
65
 
61
66
  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)
67
+ media_type = Rack::Accept::Charset.new(accept_header).best_of(media_types.values)
68
+ if media_type
69
+ by_media_type(media_type)
65
70
  else
66
71
  yield if block_given?
67
72
  end
68
73
  end
69
74
 
70
- def mime_types
75
+ def media_types
71
76
  Format.all.each_with_object({}) do
72
- |format, memo| memo[format.format_name] = format.mime_type
77
+ |format, memo| memo[format.format_name] = format.media_type
73
78
  end
74
79
  end
80
+ deprecated_alias :mime_types, :media_types
75
81
 
76
82
  def names
77
- mime_types.keys
83
+ media_types.keys
78
84
  end
79
85
 
80
86
  private
@@ -30,13 +30,14 @@ module Yaks
30
30
 
31
31
  :select,
32
32
  :textarea,
33
- :datalist
33
+ :datalist,
34
+ :legend
34
35
  ]
35
36
 
36
37
  FIELD_OPTIONS = {
38
+ type: nil,
37
39
  required: false,
38
40
  rows: nil,
39
- type: nil,
40
41
  value: nil,
41
42
  pattern: nil,
42
43
  maxlength: nil,
@@ -0,0 +1,17 @@
1
+ module Yaks
2
+ module Identifier
3
+ module LinkRelation
4
+ def self.iana_registry
5
+ @iana_registry ||= CSV.read(Yaks::Root.join('resources/iana-link-relations.csv'))
6
+ end
7
+
8
+ def self.registered_names
9
+ @registered_names = iana_registry.drop(1).map(&:first)
10
+ end
11
+
12
+ def self.iana?(name)
13
+ registered_names.include?(name.to_s)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -4,11 +4,11 @@ module Yaks
4
4
  include Attributes.new(:name)
5
5
 
6
6
  def initialize(name)
7
- @name = name
7
+ super(name: name)
8
8
  end
9
9
 
10
10
  def add_to_resource(resource, mapper, _context)
11
- resource.update_attributes(name => mapper.load_attribute(name))
11
+ resource.merge_attributes(name => mapper.load_attribute(name))
12
12
  end
13
13
  end
14
14
  end
@@ -1,27 +1,13 @@
1
1
  module Yaks
2
2
  class Mapper
3
3
  class Config
4
- extend Configurable
5
-
6
4
  include Attributes.new(
7
5
  type: nil, attributes: [], links: [], associations: [], forms: []
8
6
  )
9
7
 
10
- def type(type = Undefined)
11
- return @type if type.equal?(Undefined)
12
- update(type: type)
13
- end
14
-
15
- def attributes(*attrs)
16
- return @attributes if attrs.empty?
8
+ def add_attributes(*attrs)
17
9
  append_to(:attributes, *attrs.map(&Attribute.method(:new)))
18
10
  end
19
-
20
- config_method :link, create: Link, append_to: :links
21
- config_method :has_one, create: HasOne, append_to: :associations
22
- config_method :has_many, create: HasMany, append_to: :associations
23
- config_method :attribute, create: Attribute, append_to: :attributes
24
- config_method :form, create: Form, append_to: :forms
25
11
  end
26
12
  end
27
13
  end
@@ -0,0 +1,21 @@
1
+ module Yaks
2
+ class Mapper
3
+ class Form
4
+ class Config
5
+ include Attributes.new(
6
+ name: nil,
7
+ action: nil,
8
+ title: nil,
9
+ method: nil,
10
+ media_type: nil,
11
+ fields: [],
12
+ dynamic_blocks: []
13
+ )
14
+
15
+ def dynamic(&blk)
16
+ append_to(:dynamic_blocks, blk)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end