Class: ROM::Components::DSL::Schema
- Defined in:
- lib/rom/components/dsl/schema.rb,
lib/rom/compat/components/dsl/schema.rb
Overview
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity
Instance Method Summary collapse
-
#attribute(name, type_or_options, options = EMPTY_HASH) ⇒ Object
Defines a relation attribute with its type and options.
- #call ⇒ Object private
-
#configure ⇒ Object
private
rubocop:disable Metrics/AbcSize.
-
#primary_key(*names) ⇒ Object
Specify which key(s) should be the primary key.
Instance Method Details
#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 a schema inferrer, which will populate the type.
26 27 28 29 30 31 32 33 34 |
# File 'lib/rom/components/dsl/schema.rb', line 26 def attribute(name, , = EMPTY_HASH) if attributes.key?(name) raise(AttributeAlreadyDefinedError, "Attribute #{name.inspect} already defined") end build_attribute_info(name, , ).tap do |attr_info| attributes[name] = attr_info end end |
#call ⇒ 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.
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/rom/components/dsl/schema.rb', line 47 def call # Evaluate block only if it's not a schema defined by Relation.view DSL instance_eval(&block) if block && !config.view enabled_plugins.each_value do |plugin| plugin.apply unless plugin.applied? end configure components.add(key, config: config, block: config.view ? block : nil) end |
#configure ⇒ 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.
rubocop:disable Metrics/AbcSize
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/rom/components/dsl/schema.rb', line 62 def configure config.update(attributes: attributes.values) # TODO: make this simpler config.update( relation: relation_id, inferrer: config.inferrer.with(enabled: config.infer) ) if !view? && relation? config.join!({namespace: relation_id}, :right) if config.id != relation_id provider.config.component.update(dataset: config.dataset) if config.dataset provider.config.component.update(id: config.as) if config.as end provider.config.schema.infer = config.infer super end |
#primary_key(*names) ⇒ Object
Specify which key(s) should be the primary key
39 40 41 42 43 44 |
# File 'lib/rom/components/dsl/schema.rb', line 39 def primary_key(*names) names.each do |name| attributes[name][:type] = attributes[name][:type].(primary_key: true) end self end |