Class: Jbuilder::Schema::Template
- Inherits:
-
JbuilderTemplate
- Object
- JbuilderTemplate
- Jbuilder::Schema::Template
- Defined in:
- lib/jbuilder/schema/template.rb
Defined Under Namespace
Classes: Handler, ModelScope
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#model_scope ⇒ Object
readonly
Returns the value of attribute model_scope.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #array!(collection = [], *args, schema: {}, **options, &block) ⇒ Object
- #cache!(key = nil, **options) ⇒ Object
- #extract!(object, *attributes, schema: {}) ⇒ Object
-
#initialize(context, **options) ⇒ Template
constructor
A new instance of Template.
- #merge!(object) ⇒ Object
-
#method_missing(*args, **options, &block) ⇒ Object
standard:disable Style/MissingRespondToMissing.
- #partial!(*args) ⇒ Object
- #schema! ⇒ Object
- #set!(key, value = BLANK, *args, schema: {}, **options, &block) ⇒ Object
Constructor Details
#initialize(context, **options) ⇒ Template
Returns a new instance of Template.
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/jbuilder/schema/template.rb', line 40 def initialize(context, **) @type = :object @inline_array = false @collection = false @model_scope = ModelScope.new(**) super(context) @ignore_nil = false end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args, **options, &block) ⇒ Object
standard:disable Style/MissingRespondToMissing
171 172 173 174 |
# File 'lib/jbuilder/schema/template.rb', line 171 def method_missing(*args, **, &block) # standard:disable Style/MissingRespondToMissing # TODO: Remove once Jbuilder passes keyword arguments along to `set!` in its `method_missing`. set!(*args, **, &block) end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
8 9 10 |
# File 'lib/jbuilder/schema/template.rb', line 8 def attributes @attributes end |
#model_scope ⇒ Object (readonly)
Returns the value of attribute model_scope.
9 10 11 |
# File 'lib/jbuilder/schema/template.rb', line 9 def model_scope @model_scope end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
8 9 10 |
# File 'lib/jbuilder/schema/template.rb', line 8 def type @type end |
Instance Method Details
#array!(collection = [], *args, schema: {}, **options, &block) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/jbuilder/schema/template.rb', line 118 def array!(collection = [], *args, schema: {}, **, &block) if () @collection = true _set_ref([:partial].split("/").last) else array = _make_array(collection, *args, schema: schema, &block) if @inline_array @attributes = {} _set_value(:type, :array) _set_value(:items, array) elsif _is_collection_array?(array) @attributes = {} @inline_array = true @collection = true array! array, *array.first&.attribute_names(&:to_sym) else @type = :array @attributes = {} _set_value(:items, array) end end end |
#cache!(key = nil, **options) ⇒ Object
167 168 169 |
# File 'lib/jbuilder/schema/template.rb', line 167 def cache!(key = nil, **) yield # TODO: Our schema generation breaks Jbuilder's fragment caching. end |
#extract!(object, *attributes, schema: {}) ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/jbuilder/schema/template.rb', line 110 def extract!(object, *attributes, schema: {}) if ::Hash === object _extract_hash_values(object, attributes, schema: schema) else _extract_method_values(object, attributes, schema: schema) end end |
#merge!(object) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/jbuilder/schema/template.rb', line 154 def merge!(object) hash_or_array = ::Jbuilder === object ? object.attributes! : object hash_or_array = _format_keys(hash_or_array) if hash_or_array.is_a?(::Hash) hash_or_array = hash_or_array.each_with_object({}) do |(key, value), a| result = _schema(key, value) result = _set_description(key, result) if model_scope.model a[key] = result end end @attributes = _merge_values(@attributes, hash_or_array) end |
#partial!(*args) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/jbuilder/schema/template.rb', line 142 def partial!(*args) if args.one? && _is_active_model?(args.first) # TODO: Find where it is being used _render_active_model_partial args.first elsif args.first.is_a?(::Hash) _set_ref(args.first[:partial].split("/").last) else @collection = true if args[1].key?(:collection) _set_ref(args.first&.split("/")&.last) end end |
#schema! ⇒ Object
52 53 54 |
# File 'lib/jbuilder/schema/template.rb', line 52 def schema! {type: type}.merge(type == :object ? _object(**attributes.merge) : attributes) end |
#set!(key, value = BLANK, *args, schema: {}, **options, &block) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/jbuilder/schema/template.rb', line 56 def set!(key, value = BLANK, *args, schema: {}, **, &block) result = if block if !_blank?(value) # OBJECTS ARRAY: # json.comments @article.comments { |comment| ... } # { "comments": [ { ... }, { ... } ] } _scope { array! value, &block } else # BLOCK: # json.comments { ... } # { "comments": ... } @inline_array = true _with_model_scope(**schema) do _merge_block(key) { yield self } end end elsif args.empty? if ::Jbuilder === value # ATTRIBUTE1: # json.age 32 # json.person another_jbuilder # { "age": 32, "person": { ... } _schema(key, _format_keys(value.attributes!), **schema) elsif _is_collection_array?(value) # ATTRIBUTE2: _scope { array! value } # json.articles @articles else # json.age 32 # { "age": 32 } _schema(key, _format_keys(value), **schema) end elsif _is_collection?(value) # COLLECTION: # json.comments @article.comments, :content, :created_at # { "comments": [ { "content": "hello", "created_at": "..." }, { "content": "world", "created_at": "..." } ] } @inline_array = true @collection = true _scope { array! value, *args } else # EXTRACT!: # json.author @article.creator, :name, :email_address # { "author": { "name": "David", "email_address": "[email protected]" } } _with_model_scope(**schema) do _merge_block(key) { extract! value, *args, schema: schema } end end result = _set_description key, result if model_scope.model _set_value key, result end |