Class: MediaTypes::Serialization::SerializationDSL
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- MediaTypes::Serialization::SerializationDSL
- Defined in:
- lib/media_types/serialization/serialization_dsl.rb
Overview
Provides the serialization convenience methods
Instance Attribute Summary collapse
-
#serialization_custom_render ⇒ Object
Returns the value of attribute serialization_custom_render.
-
#serialization_dsl_result ⇒ Object
Returns the value of attribute serialization_dsl_result.
Instance Method Summary collapse
- #attribute(key, value = {}, &block) ⇒ Object
- #collection(array, serializer = __getobj__, version:, view: nil, &block) ⇒ Object
- #emit ⇒ Object
- #hidden(&block) ⇒ Object
- #index(array, serializer = __getobj__, version:, view: nil) ⇒ Object
-
#initialize(serializer, links = [], vary = ['Accept'], value = {}, context: nil) ⇒ SerializationDSL
constructor
A new instance of SerializationDSL.
- #link(rel, href:, emit_header: true, **attributes) ⇒ Object
- #object(&block) ⇒ Object
- #redirect_to(url, context, **options) ⇒ Object
- #render_view(name, context:, **args) ⇒ Object
- #suppress_render(&block) ⇒ Object
Constructor Details
#initialize(serializer, links = [], vary = ['Accept'], value = {}, context: nil) ⇒ SerializationDSL
Returns a new instance of SerializationDSL.
9 10 11 12 13 14 15 16 |
# File 'lib/media_types/serialization/serialization_dsl.rb', line 9 def initialize(serializer, links = [], vary = ['Accept'], value = {}, context: nil) self.serialization_dsl_result = value @serialization_links = links @serialization_context = context @serialization_vary = vary @serialization_custom_render = nil super(serializer) end |
Instance Attribute Details
#serialization_custom_render ⇒ Object
Returns the value of attribute serialization_custom_render.
18 19 20 |
# File 'lib/media_types/serialization/serialization_dsl.rb', line 18 def serialization_custom_render @serialization_custom_render end |
#serialization_dsl_result ⇒ Object
Returns the value of attribute serialization_dsl_result.
18 19 20 |
# File 'lib/media_types/serialization/serialization_dsl.rb', line 18 def serialization_dsl_result @serialization_dsl_result end |
Instance Method Details
#attribute(key, value = {}, &block) ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/media_types/serialization/serialization_dsl.rb', line 20 def attribute(key, value = {}, &block) unless block.nil? subcontext = SerializationDSL.new(__getobj__, @serialization_links, @serialization_vary, value, context: @serialization_context) value = subcontext.instance_exec(&block) end serialization_dsl_result[key] = value serialization_dsl_result end |
#collection(array, serializer = __getobj__, version:, view: nil, &block) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/media_types/serialization/serialization_dsl.rb', line 74 def collection(array, serializer = __getobj__, version:, view: nil, &block) raise CollectionTypeError, array.class.name unless array.is_a? Array identifier = serializer.serializer_validator.view(view).version(version).identifier rendered = [] array.each do |e| context = SerializationDSL.new(__getobj__, [], @serialization_vary, context: @serialization_context) result = serializer.serialize(e, identifier, context: @serialization_context, dsl: context, raw: true) result = block.call(result) unless block.nil? rendered.append(result) end serialization_dsl_result[:_embedded] = rendered serialization_dsl_result end |
#emit ⇒ Object
106 107 108 |
# File 'lib/media_types/serialization/serialization_dsl.rb', line 106 def emit serialization_dsl_result end |
#hidden(&block) ⇒ Object
95 96 97 98 99 100 |
# File 'lib/media_types/serialization/serialization_dsl.rb', line 95 def hidden(&block) context = SerializationDSL.new(__getobj__, @serialization_links, context: @serialization_context) context.instance_exec(&block) serialization_dsl_result end |
#index(array, serializer = __getobj__, version:, view: nil) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/media_types/serialization/serialization_dsl.rb', line 51 def index(array, serializer = __getobj__, version:, view: nil) raise CollectionTypeError, array.class.name unless array.is_a? Array links = [] identifier = serializer.serializer_validator.view(view).version(version).identifier array.each do |e| child_links = [] context = SerializationDSL.new(__getobj__, child_links, context: @serialization_context) serializer.serialize(e, identifier, context: @serialization_context, dsl: context) self_links = child_links.select { |l| l[:rel] == :self } raise NoSelfLinkProvidedError, identifier unless self_links.any? raise MultipleSelfLinksProvidedError, identifier if self_links.length > 1 links.append(self_links.first.reject { |k, _| k == :rel } ) end serialization_dsl_result[:_index] = links serialization_dsl_result end |
#link(rel, href:, emit_header: true, **attributes) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/media_types/serialization/serialization_dsl.rb', line 31 def link(rel, href:, emit_header: true, **attributes) serialization_dsl_result[:_links] = {} unless serialization_dsl_result.has_key? :_links link = { href: href, rel: rel, } link = link.merge(attributes) json = { href: href, } json = json.merge(attributes) @serialization_links.append(link) if emit_header serialization_dsl_result[:_links][rel] = json serialization_dsl_result end |
#object(&block) ⇒ Object
110 111 112 113 114 115 |
# File 'lib/media_types/serialization/serialization_dsl.rb', line 110 def object(&block) context = SerializationDSL.new(__getobj__, @serialization_links, @serialization_vary, context: @serialization_context) context.instance_exec(&block) context.serialization_dsl_result end |
#redirect_to(url, context, **options) ⇒ Object
117 118 119 120 121 122 123 124 125 126 |
# File 'lib/media_types/serialization/serialization_dsl.rb', line 117 def redirect_to(url, context, **) suppress_render do |result| context.redirect_to( url, ** ) end "Redirecting to: #{url}" end |
#render_view(name, context:, **args) ⇒ Object
102 103 104 |
# File 'lib/media_types/serialization/serialization_dsl.rb', line 102 def render_view(name, context:, **args) context.render_to_string(name, **args) end |
#suppress_render(&block) ⇒ Object
128 129 130 131 132 |
# File 'lib/media_types/serialization/serialization_dsl.rb', line 128 def suppress_render(&block) @serialization_custom_render = block || lambda { |result| } serialization_dsl_result end |