Method: ROM::Relation::ClassInterface#view
- Defined in:
- lib/rom/relation/class_interface.rb
#view(name, schema, &block) ⇒ Symbol #view(name, &block) ⇒ Symbol
Define a relation view with a specific schema
This method should only be used in cases where a given adapter doesn’t support automatic schema projection at run-time.
**It’s not needed in rom-sql**
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/rom/relation/class_interface.rb', line 187 def view(*args, &block) if args.size == 1 && block.arity > 0 raise ArgumentError, "schema attribute names must be provided as the second argument" end name, new_schema_fn, relation_block = if args.size == 1 ViewDSL.new(*args, schema, &block).call else [*args, block] end schemas[name] = if args.size == 2 -> _ { schema.project(*args[1]) } else new_schema_fn end if relation_block.arity > 0 auto_curry_guard do define_method(name, &relation_block) auto_curry(name) do schemas[name].(self) end end else define_method(name) do schemas[name].(instance_exec(&relation_block)) end end name end |