Class: ROM::Schema::DSL
Overview
Schema DSL exposed as schema { .. }
in relation classes
Constant Summary collapse
- KERNEL_METHODS =
%i[extend method].freeze
Instance Attribute Summary collapse
-
#adapter ⇒ Symbol
readonly
The adapter identifier used in gateways.
- #associations_dsl ⇒ Object readonly
-
#attr_class ⇒ Class
readonly
Attribute class that should be used.
- #attributes ⇒ Object readonly
- #definition ⇒ Object readonly
-
#inferrer ⇒ Inferrer
readonly
Optional attribute inferrer.
- #plugins ⇒ Object readonly
-
#relation ⇒ Relation::Name
readonly
The name of the schema's relation.
-
#schema_class ⇒ Class
readonly
Schema class that should be instantiated.
Instance Method Summary collapse
- #app_plugin(plugin, options = ::ROM::EMPTY_HASH) ⇒ Object private
-
#associations(&block) ⇒ AssociationDSL
Define associations for a relation.
-
#attribute(name, type_or_options, options = EMPTY_HASH) ⇒ Object
Defines a relation attribute with its type and options.
-
#build_attribute_info(name, type_or_options, options = EMPTY_HASH) ⇒ Hash
private
Builds a representation of the information needed to create an attribute.
-
#build_type(type, options = EMPTY_HASH) ⇒ Dry::Types::Type
private
Builds a type instance from base type and meta options.
- #call(&block) ⇒ Object private
-
#initialize(&block) ⇒ DSL
constructor
private
A new instance of DSL.
- #plugin_options(plugin) ⇒ Object private
-
#primary_key(*names) ⇒ Object
Specify which key(s) should be the primary key.
-
#use(plugin_name, options = ::ROM::EMPTY_HASH) ⇒ Object
Enables for the schema.
Constructor Details
#initialize(&block) ⇒ DSL
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of DSL.
58 59 60 61 62 63 64 65 |
# File 'core/lib/rom/schema/dsl.rb', line 58 def initialize(*, &block) super @attributes = {} @plugins = {} @definition = block end |
Instance Attribute Details
#adapter ⇒ Symbol (readonly)
Returns The adapter identifier used in gateways.
36 |
# File 'core/lib/rom/schema/dsl.rb', line 36 option :adapter, default: -> { :default } |
#associations_dsl ⇒ Object (readonly)
55 56 57 |
# File 'core/lib/rom/schema/dsl.rb', line 55 def associations_dsl @associations_dsl end |
#attr_class ⇒ Class (readonly)
Returns Attribute class that should be used.
32 |
# File 'core/lib/rom/schema/dsl.rb', line 32 option :attr_class, default: -> { Attribute } |
#attributes ⇒ Object (readonly)
43 44 45 |
# File 'core/lib/rom/schema/dsl.rb', line 43 def attributes @attributes end |
#definition ⇒ Object (readonly)
51 52 53 |
# File 'core/lib/rom/schema/dsl.rb', line 51 def definition @definition end |
#inferrer ⇒ Inferrer (readonly)
Returns Optional attribute inferrer.
24 |
# File 'core/lib/rom/schema/dsl.rb', line 24 option :inferrer, default: -> { DEFAULT_INFERRER } |
#plugins ⇒ Object (readonly)
47 48 49 |
# File 'core/lib/rom/schema/dsl.rb', line 47 def plugins @plugins end |
#relation ⇒ Relation::Name (readonly)
Returns The name of the schema's relation.
20 |
# File 'core/lib/rom/schema/dsl.rb', line 20 param :relation |
#schema_class ⇒ Class (readonly)
Returns Schema class that should be instantiated.
28 |
# File 'core/lib/rom/schema/dsl.rb', line 28 option :schema_class, default: -> { Schema } |
Instance Method Details
#app_plugin(plugin, options = ::ROM::EMPTY_HASH) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
176 177 178 179 |
# File 'core/lib/rom/schema/dsl.rb', line 176 def app_plugin(plugin, = ::ROM::EMPTY_HASH) plugin.extend_dsl(self) @plugins[plugin.name] = [plugin, plugin.config.to_hash.merge()] end |
#associations(&block) ⇒ AssociationDSL
Define associations for a relation
115 116 117 |
# File 'core/lib/rom/schema/dsl.rb', line 115 def associations(&block) @associations_dsl = AssociationsDSL.new(relation, &block) end |
#attribute(name, type_or_options, options = EMPTY_HASH) ⇒ Object
Defines a relation attribute with its type and options.
When only options are given, type is left as nil. It makes sense when it is used alongside an schema inferrer, which will populate the type.
77 78 79 80 81 82 83 84 |
# File 'core/lib/rom/schema/dsl.rb', line 77 def attribute(name, , = EMPTY_HASH) if attributes.key?(name) ::Kernel.raise ::ROM::AttributeAlreadyDefinedError, "Attribute #{name.inspect} already defined" end attributes[name] = build_attribute_info(name, , ) end |
#build_attribute_info(name, type_or_options, options = EMPTY_HASH) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Builds a representation of the information needed to create an
attribute. It returns a hash with :type
and :options
keys.
127 128 129 130 131 132 133 134 135 136 |
# File 'core/lib/rom/schema/dsl.rb', line 127 def build_attribute_info(name, , = EMPTY_HASH) type, = if .is_a?(::Hash) [nil, ] else [build_type(, ), ] end Schema.build_attribute_info( type, **, name: name ) end |
#build_type(type, options = EMPTY_HASH) ⇒ Dry::Types::Type
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Builds a type instance from base type and meta options
143 144 145 146 147 148 149 150 151 |
# File 'core/lib/rom/schema/dsl.rb', line 143 def build_type(type, = EMPTY_HASH) if [:read] type.(source: relation, read: [:read]) elsif type.optional? && type.[:read] type.(source: relation, read: type.[:read].optional) else type.(source: relation) end.(Attribute::META_OPTIONS.map { |opt| [opt, [opt]] if .key?(opt) }.compact.to_h) end |
#call(&block) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
182 183 184 185 186 187 188 189 190 191 |
# File 'core/lib/rom/schema/dsl.rb', line 182 def call(&block) instance_exec(&block) if block instance_exec(&definition) if definition schema_class.define(relation, **opts) do |schema| plugins.values.each do |plugin, | plugin.apply_to(schema, **) end end end |
#plugin_options(plugin) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
194 195 196 |
# File 'core/lib/rom/schema/dsl.rb', line 194 def (plugin) @plugins[plugin][1] end |
#primary_key(*names) ⇒ Object
Specify which key(s) should be the primary key
156 157 158 159 160 161 162 |
# File 'core/lib/rom/schema/dsl.rb', line 156 def primary_key(*names) names.each do |name| attributes[name][:type] = attributes[name][:type].(primary_key: true) end self end |
#use(plugin_name, options = ::ROM::EMPTY_HASH) ⇒ Object
Enables for the schema
170 171 172 173 |
# File 'core/lib/rom/schema/dsl.rb', line 170 def use(plugin_name, = ::ROM::EMPTY_HASH) plugin = ::ROM.plugin_registry[:schema].fetch(plugin_name, adapter) app_plugin(plugin, ) end |