Class: SoberSwag::OutputObject::View
- Inherits:
-
Serializer::Base
- Object
- Serializer::Base
- SoberSwag::OutputObject::View
- Includes:
- FieldSyntax
- Defined in:
- lib/sober_swag/output_object/view.rb
Overview
DSL for defining a view.
Used in view
blocks within define.
Views are "variants" of SoberSwag::OutputObjects that contain different fields.
Defined Under Namespace
Classes: NestingError
Instance Attribute Summary collapse
-
#fields ⇒ Array<SoberSwag::OutputObject::Fields>
readonly
The fields defined in this view.
-
#name ⇒ Symbol
readonly
The name of this view.
Class Method Summary collapse
-
.define(name, base_fields, &block) ⇒ SoberSwag::OutputObject::View
Define a new view with the given base fields.
Instance Method Summary collapse
-
#add_field!(field) ⇒ nil
Adds a field do this view.
-
#except!(name) ⇒ nil
Excludes a field with the given name from this view.
-
#initialize(name, base_fields = []) ⇒ View
constructor
A new instance of View.
-
#serialize(obj, opts = {}) ⇒ Hash
Serialize an object according to this view.
-
#serializer ⇒ SoberSwag::Serializer::FieldList
Get the serializer defined by this view.
-
#to_s ⇒ String
Pretty show for humans.
-
#type ⇒ Class
Get the type of this view.
-
#view ⇒ Object
Always raises NestingError.
Methods included from FieldSyntax
#field, #merge, #multi, #primitive
Methods inherited from Serializer::Base
#array, #finalize_lazy_type!, #identifier, #lazy_type, #lazy_type?, #meta, #optional, #via_map
Constructor Details
#initialize(name, base_fields = []) ⇒ View
Returns a new instance of View.
32 33 34 35 |
# File 'lib/sober_swag/output_object/view.rb', line 32 def initialize(name, base_fields = []) @name = name @fields = base_fields.dup end |
Instance Attribute Details
#fields ⇒ Array<SoberSwag::OutputObject::Fields> (readonly)
Returns the fields defined in this view.
43 44 45 |
# File 'lib/sober_swag/output_object/view.rb', line 43 def fields @fields end |
#name ⇒ Symbol (readonly)
Returns the name of this view.
39 40 41 |
# File 'lib/sober_swag/output_object/view.rb', line 39 def name @name end |
Class Method Details
.define(name, base_fields, &block) ⇒ SoberSwag::OutputObject::View
Define a new view with the given base fields.
17 18 19 20 21 |
# File 'lib/sober_swag/output_object/view.rb', line 17 def self.define(name, base_fields, &block) new(name, base_fields).tap do |view| view.instance_eval(&block) end end |
Instance Method Details
#add_field!(field) ⇒ nil
Adds a field do this view.
80 81 82 |
# File 'lib/sober_swag/output_object/view.rb', line 80 def add_field!(field) @fields << field end |
#except!(name) ⇒ nil
Excludes a field with the given name from this view.
65 66 67 |
# File 'lib/sober_swag/output_object/view.rb', line 65 def except!(name) @fields.reject! { |f| f.name == name } end |
#serialize(obj, opts = {}) ⇒ Hash
Serialize an object according to this view.
50 51 52 |
# File 'lib/sober_swag/output_object/view.rb', line 50 def serialize(obj, opts = {}) serializer.serialize(obj, opts) end |
#serializer ⇒ SoberSwag::Serializer::FieldList
Get the serializer defined by this view. WARNING: Don't add more fields after you call this.
96 97 98 99 |
# File 'lib/sober_swag/output_object/view.rb', line 96 def serializer @serializer ||= SoberSwag::Serializer::FieldList.new(fields).tap { |s| s.identifier(identifier) } end |
#to_s ⇒ String
Pretty show for humans
87 88 89 |
# File 'lib/sober_swag/output_object/view.rb', line 87 def to_s "<SoberSwag::OutputObject::View(#{identifier})>" end |
#type ⇒ Class
Get the type of this view.
57 58 59 |
# File 'lib/sober_swag/output_object/view.rb', line 57 def type serializer.type end |
#view ⇒ Object
Always raises NestingError
72 73 74 |
# File 'lib/sober_swag/output_object/view.rb', line 72 def view(*) raise NestingError, 'no views in views' end |