code
stringlengths
26
124k
docstring
stringlengths
23
125k
func_name
stringlengths
1
98
language
stringclasses
1 value
repo
stringlengths
5
53
path
stringlengths
7
151
url
stringlengths
50
211
license
stringclasses
7 values
def command relation.command(command_type, **command_compiler_options) end
Return a command for this changeset @return [ROM::Command] @api private
command
ruby
rom-rb/rom
changeset/lib/rom/changeset.rb
https://github.com/rom-rb/rom/blob/master/changeset/lib/rom/changeset.rb
MIT
def command_compiler_options command_options.merge(use: command_plugins.keys, plugins_options: command_plugins) end
Return configured command compiler options @return [Hash] @api private
command_compiler_options
ruby
rom-rb/rom
changeset/lib/rom/changeset.rb
https://github.com/rom-rb/rom/blob/master/changeset/lib/rom/changeset.rb
MIT
def associate(other, name = Associated.infer_assoc_name(other)) self.class.new(left, associations: associations.merge(name => other)) end
Associate with other changesets @see Changeset#associate @return [Associated] @api public
associate
ruby
rom-rb/rom
changeset/lib/rom/changeset/associated.rb
https://github.com/rom-rb/rom/blob/master/changeset/lib/rom/changeset/associated.rb
MIT
def command associations.reduce(left.command.curry(left)) do |a, (assoc, other)| case other when Changeset a >> other.command.with_association(assoc).curry(other) when Associated a >> other.command.with_association(assoc) when Array raise NotImplementedError, 'Changeset::Associate does not support arrays yet' else a.with_association(assoc, parent: other) end end end
Create a composed command @example using existing parent data user_changeset = users.changeset(name: 'Jane') task_changeset = tasks.changeset(title: 'Task One') user = users.create(user_changeset) task = tasks.create(task_changeset.associate(user, :user)) @example saving both parent and child in one go user_changeset = users.changeset(name: 'Jane') task_changeset = tasks.changeset(title: 'Task One') task = tasks.create(task_changeset.associate(user, :user)) This works *only* with parent => child(ren) changeset hierarchy @return [ROM::Command::Composite] @api public
command
ruby
rom-rb/rom
changeset/lib/rom/changeset/associated.rb
https://github.com/rom-rb/rom/blob/master/changeset/lib/rom/changeset/associated.rb
MIT
def map(*steps, &) extend(*steps, for_diff: true, &) end
Pipe changeset's data using custom steps define on the pipe @overload map(*steps) Apply mapping using built-in transformations @example changeset.map(:add_timestamps) @param [Array<Symbol>] steps A list of mapping steps @overload map(&block) Apply mapping using a custom block @example changeset.map { |tuple| tuple.merge(created_at: Time.now) } @overload map(*steps, &block) Apply mapping using built-in transformations and a custom block @example changeset.map(:add_timestamps) { |tuple| tuple.merge(status: 'published') } @param [Array<Symbol>] steps A list of mapping steps @return [Changeset] @api public
map
ruby
rom-rb/rom
changeset/lib/rom/changeset/stateful.rb
https://github.com/rom-rb/rom/blob/master/changeset/lib/rom/changeset/stateful.rb
MIT
def extend(*steps, **options, &) if block_given? if steps.empty? with(pipe: pipe.compose(Pipe.new(proc(&)).bind(self), **options)) else extend(*steps, **options).extend(**options, &) end else with(pipe: steps.reduce(pipe.with(**options)) { |a, e| a.compose(pipe[e], **options) }) end end
Pipe changeset's data using custom steps define on the pipe. You should use #map instead except updating timestamp fields. Calling changeset.extend builds a pipe that excludes certain steps for generating the diff. Currently the only place where it is used is update changesets with the `:touch` step, i.e. `changeset.extend(:touch).diff` will exclude `:updated_at` from the diff. @see Changeset::Stateful#map @return [Changeset] @api public
extend
ruby
rom-rb/rom
changeset/lib/rom/changeset/stateful.rb
https://github.com/rom-rb/rom/blob/master/changeset/lib/rom/changeset/stateful.rb
MIT
def data(data) with(__data__: data) end
Return changeset with data @param [Hash] data @return [Changeset] @api public
data
ruby
rom-rb/rom
changeset/lib/rom/changeset/stateful.rb
https://github.com/rom-rb/rom/blob/master/changeset/lib/rom/changeset/stateful.rb
MIT
def to_a result == :one ? [to_h] : __data__.map { |element| pipe.call(element) } end
Coerce changeset to an array This will send the data through the pipe @return [Array] @api public
to_a
ruby
rom-rb/rom
changeset/lib/rom/changeset/stateful.rb
https://github.com/rom-rb/rom/blob/master/changeset/lib/rom/changeset/stateful.rb
MIT
def associate(other, name = Associated.infer_assoc_name(other)) Associated.new(self, associations: { name => other }) end
Associate a changeset with another changeset or hash-like object @example with another changeset new_user = users.changeset(name: 'Jane') new_task = users.changeset(:tasks, title: 'A task') new_task.associate(new_user, :users) @example with a hash-like object user = users.users.by_pk(1).one new_task = users.changeset(:tasks, title: 'A task') new_task.associate(user, :users) @param [#to_hash, Changeset] other Other changeset or hash-like object @param [Symbol] name The association identifier from schema @api public
associate
ruby
rom-rb/rom
changeset/lib/rom/changeset/stateful.rb
https://github.com/rom-rb/rom/blob/master/changeset/lib/rom/changeset/stateful.rb
MIT
def result __data__.is_a?(Array) ? :many : :one end
Return command result type @return [Symbol] @api private
result
ruby
rom-rb/rom
changeset/lib/rom/changeset/stateful.rb
https://github.com/rom-rb/rom/blob/master/changeset/lib/rom/changeset/stateful.rb
MIT
def inspect %(#<#{self.class} relation=#{relation.name.inspect} data=#{__data__}>) end
Return string representation of the changeset @return [String] @api public
inspect
ruby
rom-rb/rom
changeset/lib/rom/changeset/stateful.rb
https://github.com/rom-rb/rom/blob/master/changeset/lib/rom/changeset/stateful.rb
MIT
def pipe @pipe ||= self.class.default_pipe(self) end
Data transformation pipe @return [Changeset::Pipe] @api private
pipe
ruby
rom-rb/rom
changeset/lib/rom/changeset/stateful.rb
https://github.com/rom-rb/rom/blob/master/changeset/lib/rom/changeset/stateful.rb
MIT
def original @original ||= relation.one end
Return original tuple that this changeset may update @return [Hash] @api public
original
ruby
rom-rb/rom
changeset/lib/rom/changeset/update.rb
https://github.com/rom-rb/rom/blob/master/changeset/lib/rom/changeset/update.rb
MIT
def diff @diff ||= begin source = Hash(original) data = pipe.for_diff(__data__) data_tuple = data.to_a data_keys = data.keys & source.keys new_tuple = data_tuple.to_a.select { |k, _| data_keys.include?(k) } ori_tuple = source.to_a.select { |k, _| data_keys.include?(k) } (new_tuple - (new_tuple & ori_tuple)).to_h end end
Calculate the diff between the original and changeset data @return [Hash] @api public rubocop:disable Metrics/AbcSize
diff
ruby
rom-rb/rom
changeset/lib/rom/changeset/update.rb
https://github.com/rom-rb/rom/blob/master/changeset/lib/rom/changeset/update.rb
MIT
def changeset(*) raise NotImplementedError, "Changeset doesn't support combined relations yet" end
Build a changeset for a combined relation @raise NotImplementedError @api public
changeset
ruby
rom-rb/rom
changeset/lib/rom/changeset/extensions/relation.rb
https://github.com/rom-rb/rom/blob/master/changeset/lib/rom/changeset/extensions/relation.rb
MIT
def wrapped(name = source.dataset) prefixed(name).meta(wrapped: true) end
Return attribute type wrapped for the specified relation name @param [Symbol] name The name of the source relation (defaults to source.dataset) @return [Attribute] @api public
wrapped
ruby
rom-rb/rom
core/lib/rom/attribute.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/attribute.rb
MIT
def meta(opts = nil) if opts self.class.new(type.meta(opts), **options) else type.meta end end
Return attribute type with additional meta information Return meta information hash if no opts are provided @param [Hash] opts The meta options @return [Attribute] @api public
meta
ruby
rom-rb/rom
core/lib/rom/attribute.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/attribute.rb
MIT
def inspect opts = options.reject { |k| %i[type name].include?(k) } meta_and_opts = meta.merge(opts).map { |k, v| "#{k}=#{v.inspect}" } %(#<#{self.class}[#{type.name}] name=#{name.inspect} #{meta_and_opts.join(' ')}>) end
Return string representation of the attribute type @return [String] @api public
inspect
ruby
rom-rb/rom
core/lib/rom/attribute.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/attribute.rb
MIT
def eql?(other) other.is_a?(self.class) ? super : type.eql?(other) end
Check if the attribute type is equal to another @param [Dry::Type, Attribute] other @return [TrueClass,FalseClass] @api public
eql?
ruby
rom-rb/rom
core/lib/rom/attribute.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/attribute.rb
MIT
def optional sum = self.class.new(super, **options) if read? sum.meta(read: meta[:read].optional) else sum end end
Return nullable attribute @return [Attribute] @api public
optional
ruby
rom-rb/rom
core/lib/rom/attribute.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/attribute.rb
MIT
def to_ast [:attribute, [name, type.to_ast(meta: false), meta_options_ast]] end
Return AST for the type @return [Array] @api public
to_ast
ruby
rom-rb/rom
core/lib/rom/attribute.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/attribute.rb
MIT
def to_read_ast [:attribute, [name, to_read_type.to_ast(meta: false), meta_options_ast]] end
Return AST for the read type @return [Array] @api public
to_read_ast
ruby
rom-rb/rom
core/lib/rom/attribute.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/attribute.rb
MIT
def auto_curry(name, &) arity = instance_method(name).arity if public_instance_methods.include?(name) && arity != 0 mod = Wrapper.new(name, arity, &) auto_curried_methods << name prepend(mod) else self end end
Auto-curry a method @param [Symbol] name The name of a method @api private
auto_curry
ruby
rom-rb/rom
core/lib/rom/auto_curry.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/auto_curry.rb
MIT
def execute(*) raise( ::NotImplementedError, "#{self.class}##{__method__} must be implemented" ) end
Execute the command @abstract @return [Array] an array with inserted tuples @api private
execute
ruby
rom-rb/rom
core/lib/rom/command.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/command.rb
MIT
def call(*args, &) tuples = if hooks? prepared = if curried? apply_hooks(before_hooks, *(curry_args + args)) else apply_hooks(before_hooks, *args) end result = if prepared execute(prepared, &) else execute(&) end if curried? if !args.empty? apply_hooks(after_hooks, result, *args) elsif curry_args.size > 1 apply_hooks(after_hooks, result, curry_args[1]) else apply_hooks(after_hooks, result) end else apply_hooks(after_hooks, result, *args.drop(1)) end else execute(*(curry_args + args), &) end if one? tuples.first else tuples end end
Call the command and return one or many tuples This method will apply before/after hooks automatically @api public rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
call
ruby
rom-rb/rom
core/lib/rom/command.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/command.rb
MIT
def curry(*args) if curry_args.empty? && args.first.is_a?(Graph::InputEvaluator) Lazy[self].new(self, *args) else self.class.build(relation, **options, curry_args: args) end end
Curry this command with provided args Curried command can be called without args. If argument is a graph input processor, lazy command will be returned, which is used for handling nested input hashes. @return [Command, Lazy] @api public
curry
ruby
rom-rb/rom
core/lib/rom/command.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/command.rb
MIT
def before(*hooks) self.class.new(relation, **options, before: before_hooks + hooks) end
Return a new command with appended before hooks @param [Array<Hash>] hooks A list of before hooks configurations @return [Command] @api public
before
ruby
rom-rb/rom
core/lib/rom/command.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/command.rb
MIT
def after(*hooks) self.class.new(relation, **options, after: after_hooks + hooks) end
Return a new command with appended after hooks @param [Array<Hash>] hooks A list of after hooks configurations @return [Command] @api public
after
ruby
rom-rb/rom
core/lib/rom/command.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/command.rb
MIT
def new(new_relation) self.class.build(new_relation, **options, source: relation) end
Return a new command with other source relation This can be used to restrict command with a specific relation @return [Command] @api public
new
ruby
rom-rb/rom
core/lib/rom/command.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/command.rb
MIT
def map_input_tuples(tuples, &mapper) return enum_for(:with_input_tuples, tuples) unless mapper if tuples.respond_to? :merge mapper[tuples] else tuples.map(&mapper) end end
Yields tuples for insertion or return an enumerator @api private
map_input_tuples
ruby
rom-rb/rom
core/lib/rom/command.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/command.rb
MIT
def apply_hooks(hooks, tuples, *args) hooks.reduce(tuples) do |a, e| if e.is_a?(Hash) hook_meth, hook_args = e.to_a.flatten(1) __send__(hook_meth, a, *args, **hook_args) else __send__(e, a, *args) end end end
Apply provided hooks Used by #call @return [Array<Hash>] @api private
apply_hooks
ruby
rom-rb/rom
core/lib/rom/command.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/command.rb
MIT
def wrap_dataset(dataset) if relation.is_a?(Relation::Composite) relation.new(dataset).to_a else dataset end end
Pipes a dataset through command's relation @return [Array] @api private
wrap_dataset
ruby
rom-rb/rom
core/lib/rom/command.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/command.rb
MIT
def call(*args) cache.fetch_or_store(args.hash) do type, adapter, ast, plugins, plugins_options, meta = args compiler = with( id: type, adapter: adapter, plugins: Array(plugins), plugins_options: plugins_options, meta: meta ) graph_opts = compiler.visit(ast) command = ROM::Commands::Graph.build(registry, graph_opts) if command.graph? CommandProxy.new(command) elsif command.lazy? command.unwrap else command end end end
Return a specific command type for a given adapter and relation AST This class holds its own registry where all generated commands are being stored CommandProxy is returned for complex command graphs as they expect root relation name to be present in the input, which we don't want to have in repositories. It might be worth looking into removing this requirement from rom core Command::Graph API. @overload [](type, adapter, ast, plugins, meta) @param type [Symbol] The type of command @param adapter [Symbol] The adapter identifier @param ast [Array] The AST representation of a relation @param plugins [Array<Symbol>] A list of optional command plugins that should be used @param meta [Hash] Meta data for a command @return [Command, CommandProxy] @api private
call
ruby
rom-rb/rom
core/lib/rom/command_compiler.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/command_compiler.rb
MIT
def visit_relation(node, parent_relation = nil) name, header, meta = node other = header.map { |attr| visit(attr, name) }.compact if type register_command(name, type, meta, parent_relation) default_mapping = if meta[:combine_type] == :many name else { Inflector.singularize(name).to_sym => name } end mapping = if parent_relation associations = relations[parent_relation].associations assoc = associations[meta[:combine_name]] if assoc { assoc.key => assoc.target.name.to_sym } else default_mapping end else default_mapping end if other.empty? [mapping, type] else [mapping, [type, other]] end else registry[name][id] = commands[name][id] [name, id] end end
@api private rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
visit_relation
ruby
rom-rb/rom
core/lib/rom/command_compiler.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/command_compiler.rb
MIT
def register_command(rel_name, type, rel_meta, parent_relation = nil) relation = relations[rel_name] type.create_class(rel_name, type) do |klass| klass.result(rel_meta.fetch(:combine_type, result)) meta.each do |name, value| klass.public_send(name, value) end setup_associates(klass, relation, rel_meta, parent_relation) if rel_meta[:combine_type] plugins.each do |plugin| plugin_options = plugins_options.fetch(plugin) { EMPTY_HASH } klass.use(plugin, **plugin_options) end gateway = gateways[relation.gateway] notifications.trigger( 'configuration.commands.class.before_build', command: klass, gateway: gateway, dataset: relation.dataset, adapter: adapter ) klass.extend_for_relation(relation) if klass.restrictable registry[rel_name][type] = klass.build(relation) end end
Build a command object for a specific relation The command will be prepared for handling associations if it's a combined relation. Additional plugins will be enabled if they are configured for this compiler. @param [Symbol] rel_name A relation identifier from the container registry @param [Symbol] type The command type @param [Hash] rel_meta Meta information from relation AST @param [Symbol] parent_relation Optional parent relation identifier @return [ROM::Command] @api private rubocop:disable Metrics/AbcSize
register_command
ruby
rom-rb/rom
core/lib/rom/command_compiler.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/command_compiler.rb
MIT
def setup_associates(klass, relation, _meta, parent_relation) assoc_name = if relation.associations.key?(parent_relation) parent_relation else singular_name = Inflector.singularize(parent_relation).to_sym singular_name if relation.associations.key?(singular_name) end klass.associates(assoc_name || parent_relation) end
Sets up `associates` plugin for a given command class and relation @param [Class] klass The command class @param [Relation] relation The relation for the command @api private
setup_associates
ruby
rom-rb/rom
core/lib/rom/command_compiler.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/command_compiler.rb
MIT
def respond_to_missing?(name, include_private = false) key?(name) || super end
Allow checking if a certain command is available using dot-notation @api private
respond_to_missing?
ruby
rom-rb/rom
core/lib/rom/command_registry.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/command_registry.rb
MIT
def method_missing(name, *) if key?(name) self[name] else super end end
Allow retrieving commands using dot-notation @api private
method_missing
ruby
rom-rb/rom
core/lib/rom/command_registry.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/command_registry.rb
MIT
def initialize(*args) @environment = Environment.new(*args) @notifications = Notifications.event_bus(:configuration) @setup = Setup.new(notifications) yield self if block_given? end
Initialize a new configuration @see Environment#initialize @return [Configuration] @api private
initialize
ruby
rom-rb/rom
core/lib/rom/configuration.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/configuration.rb
MIT
def use(plugin, options = EMPTY_HASH) if plugin.is_a?(::Array) plugin.each { |p| use(p) } elsif plugin.is_a?(::Hash) plugin.to_a.each { |p| use(*p) } else ROM.plugin_registry[:configuration].fetch(plugin).apply_to(self, options) end self end
Apply a plugin to the configuration @param [Mixed] plugin The plugin identifier, usually a Symbol @param [Hash] options Plugin options @return [Configuration] @api public
use
ruby
rom-rb/rom
core/lib/rom/configuration.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/configuration.rb
MIT
def relation(name, options = EMPTY_HASH, &) klass_opts = { adapter: default_adapter }.merge(options) klass = Relation.build_class(name, klass_opts) klass.schema_opts(dataset: name, relation: name) klass.class_eval(&) if block_given? register_relation(klass) klass end
Relation definition DSL @example setup.relation(:users) do def names project(:name) end end @api public
relation
ruby
rom-rb/rom
core/lib/rom/configuration_dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/configuration_dsl.rb
MIT
def commands(name, &) register_command(*CommandDSL.new(name, default_adapter, &).command_classes) end
Command definition DSL @example setup.commands(:users) do define(:create) do input NewUserParams result :one end define(:update) do input UserParams result :many end define(:delete) do result :many end end @api public
commands
ruby
rom-rb/rom
core/lib/rom/configuration_dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/configuration_dsl.rb
MIT
def plugin(adapter, spec, &) type, name = spec.flatten(1) plugin = plugin_registry[type].adapter(adapter).fetch(name) do plugin_registry[type].fetch(name) end if block_given? register_plugin(plugin.configure(&)) else register_plugin(plugin) end end
Configures a plugin for a specific adapter to be enabled for all relations @example config = ROM::Configuration.new(:sql, 'sqlite::memory') config.plugin(:sql, relations: :instrumentation) do |p| p.notifications = MyNotificationsBackend end config.plugin(:sql, relations: :pagination) @param [Symbol] adapter The adapter identifier @param [Hash<Symbol=>Symbol>] spec Component identifier => plugin identifier @return [Plugin] @api public
plugin
ruby
rom-rb/rom
core/lib/rom/configuration_dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/configuration_dsl.rb
MIT
def each return to_enum unless block_given? data.each { |tuple| yield(row_proc[tuple]) } end
Iterate over data using row_proc @return [Enumerator] if block is not given @api private
each
ruby
rom-rb/rom
core/lib/rom/data_proxy.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/data_proxy.rb
MIT
def forward(*methods) # FIXME: we should probably raise if one of the non-forwardable methods # was provided (methods - NON_FORWARDABLE).each do |method_name| class_eval(<<-RUBY, __FILE__, __LINE__ + 1) def #{method_name}(...) # def find_all(...) response = data.public_send(:#{method_name}, ...) # response = data.public_send(:find_all, ...) # if response.equal?(data) # if response.equal?(data) self # self elsif response.is_a?(data.class) # elsif response.is_a?(data.class) self.class.new(response) # self.class.new(response) else # else response # response end # end end # end RUBY end end
Forward provided methods to the underlaying data object @example class MyDataset include DataProxy forward(:find_all, :map) end @return [undefined] @api public
forward
ruby
rom-rb/rom
core/lib/rom/data_proxy.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/data_proxy.rb
MIT
def normalize_gateways(gateways_config) gateways_config.each_with_object(map: {}, gateways: {}) do |(name, spec), hash| identifier, *args = Array(spec) if identifier.is_a?(Gateway) gateway = identifier else kwargs = args.last.is_a?(::Hash) ? args.pop : {} gateway = Gateway.setup(identifier, *args.flatten, **kwargs) end hash[:map][gateway] = name hash[:gateways][name] = gateway end end
Build gateways using the setup interface @api private
normalize_gateways
ruby
rom-rb/rom
core/lib/rom/environment.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/environment.rb
MIT
def adapter self.class.adapter || raise( MissingAdapterIdentifierError, "gateway class +#{self}+ is missing the adapter identifier" ) end
Returns the adapter, defined for the class @return [Symbol] @api public
adapter
ruby
rom-rb/rom
core/lib/rom/gateway.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/gateway.rb
MIT
def plugins(...) PluginDSL.new(plugin_registry, ...) end
Global plugin setup DSL @example ROM.plugins do register :publisher, Plugin::Publisher, type: :command end @api public
plugins
ruby
rom-rb/rom
core/lib/rom/global.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/global.rb
MIT
def register_adapter(identifier, adapter) adapters[identifier] = adapter self end
Register adapter namespace under a specified identifier @param [Symbol] identifier @param [Class,Module] adapter @return [self] @api private
register_adapter
ruby
rom-rb/rom
core/lib/rom/global.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/global.rb
MIT
def by_type(*types) select { |attribute| types.include?(attribute.class) } end
Find all attribute matching specific attribute class (not kind) @return [Array<Attribute>] @api private
by_type
ruby
rom-rb/rom
core/lib/rom/header.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/header.rb
MIT
def initialize_tuple_keys @tuple_keys = mapping.keys.flatten + non_primitives.flat_map(&:tuple_keys) end
Set all tuple keys from all attributes going deep into Wrap and Group too @api private
initialize_tuple_keys
ruby
rom-rb/rom
core/lib/rom/header.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/header.rb
MIT
def initialize_pop_keys @pop_keys = mapping.values + non_primitives.flat_map(&:tuple_keys) end
Set all tuple keys from all attributes popping from Unwrap and Ungroup @api private
initialize_pop_keys
ruby
rom-rb/rom
core/lib/rom/header.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/header.rb
MIT
def options @__options__ ||= self.class.dry_initializer.definitions.values.to_h do |item| [item.target, instance_variable_get(item.ivar)] end end
Instance options @return [Hash] @api public
options
ruby
rom-rb/rom
core/lib/rom/initializer.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/initializer.rb
MIT
def freeze options super end
This makes sure we memoize options before an object becomes frozen @api public
freeze
ruby
rom-rb/rom
core/lib/rom/initializer.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/initializer.rb
MIT
def call(relation) transformers.reduce(relation.to_a) { |a, e| e.call(a) } end
Process a relation using the transformers @api private
call
ruby
rom-rb/rom
core/lib/rom/mapper.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper.rb
MIT
def visit_relation(node) rel_name, header, meta_options = node name = meta_options[:combine_name] || meta_options[:alias] || rel_name namespace = meta_options.fetch(:struct_namespace) model = meta_options.fetch(:model) do if meta_options[:combine_name] false else struct_compiler[name, header, namespace] end end options = [header.map(&method(:visit)), mapper_options.merge(model: model)] if meta_options[:combine_type] type = meta_options[:combine_type] == :many ? :array : :hash keys = meta_options.fetch(:keys) [name, combine: true, type: type, keys: keys, header: Header.coerce(*options)] elsif meta_options[:wrap] [name, wrap: true, type: :hash, header: Header.coerce(*options)] else options end end
rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity
visit_relation
ruby
rom-rb/rom
core/lib/rom/mapper_compiler.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper_compiler.rb
MIT
def visit_attribute(node) name, _, meta_options = node if meta_options[:alias] [meta_options[:alias], from: name] else [name] end end
rubocop:enable Metrics/AbcSize, Metrics/PerceivedComplexity
visit_attribute
ruby
rom-rb/rom
core/lib/rom/mapper_compiler.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper_compiler.rb
MIT
def call(attrs) define_class(attrs) define_const if const_name @klass end
Build a model class supporting specific attributes @return [Class] @api private
call
ruby
rom-rb/rom
core/lib/rom/model_builder.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/model_builder.rb
MIT
def map_with(*names) [self, *names.map { |name| mappers[name] }] .reduce { |a, e| composite_class.new(a, e) } end
Send data through specified mappers @return [Relation::Composite] @api public
map_with
ruby
rom-rb/rom
core/lib/rom/pipeline.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/pipeline.rb
MIT
def apply_to(target, **options) if mod.respond_to?(:apply) mod.apply(target, **options) elsif mod.respond_to?(:new) target.include(mod.new(**options)) elsif target.is_a?(::Module) target.include(mod) end end
Apply this plugin to the target @param [Class,Object] target @api private
apply_to
ruby
rom-rb/rom
core/lib/rom/plugin.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/plugin.rb
MIT
def register(name, mod, options = EMPTY_HASH) type(options.fetch(:type)).register(name, mod, options) end
Register a plugin for future use @param [Symbol] name The registration name for the plugin @param [Module] mod The plugin to register @param [Hash] options optional configuration data @option options [Symbol] :type What type of plugin this is (command, relation or mapper) @option options [Symbol] :adapter (:default) which adapter this plugin applies to. Leave blank for all adapters
register
ruby
rom-rb/rom
core/lib/rom/plugin_registry.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/plugin_registry.rb
MIT
def register(name, mod, options) elements[name] = plugin_type.new(name, mod, **options) end
Assign a plugin to this environment registry @param [Symbol] name The registered plugin name @param [Module] mod The plugin to register @param [Hash] options optional configuration data @api private
register
ruby
rom-rb/rom
core/lib/rom/plugin_registry.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/plugin_registry.rb
MIT
def fetch(name, adapter_name = :default) adapter(adapter_name).fetch(name) do adapter(:default).fetch(name) do raise(UnknownPluginError, name) end end end
Return the plugin for a given adapter @param [Symbol] name The name of the plugin @param [Symbol] adapter_name (:default) The name of the adapter used @raise [UnknownPluginError] if no plugin is found with the given name @api public
fetch
ruby
rom-rb/rom
core/lib/rom/plugin_registry.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/plugin_registry.rb
MIT
def each(&) return to_enum unless block_given? if auto_map? mapper.(dataset.map { |tuple| output_schema[tuple] }).each(&) else dataset.each { |tuple| yield(output_schema[tuple]) } end end
Yields relation tuples Every tuple is processed through Relation#output_schema, it's a no-op by default @yield [Hash] @return [Enumerator] if block is not provided @api public
each
ruby
rom-rb/rom
core/lib/rom/relation.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/relation.rb
MIT
def node(name) assoc = associations[name] other = assoc.node other.eager_load(assoc) end
Create a graph node for a given association identifier @param [Symbol, Relation::Name] name @return [Relation] @api public
node
ruby
rom-rb/rom
core/lib/rom/relation.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/relation.rb
MIT
def eager_load(assoc) relation = assoc.prepare(self) if assoc.override? relation.(assoc) else relation.preload_assoc(assoc) end end
Return a graph node prepared by the given association @param [Association] assoc An association object @return [Relation] @api public
eager_load
ruby
rom-rb/rom
core/lib/rom/relation.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/relation.rb
MIT
def wrap(*names) wrap_around(*names.map { |n| associations[n].wrap }) end
Wrap other relations using association names @example tasks.wrap(:owner) @param [Array<Symbol>] names A list with association identifiers @return [Wrap] @api public
wrap
ruby
rom-rb/rom
core/lib/rom/relation.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/relation.rb
MIT
def new(dataset, **new_opts) opts = if new_opts.empty? options elsif new_opts.key?(:schema) options.merge(new_opts).except(:input_schema, :output_schema) else options.merge(new_opts) end self.class.new(dataset, **opts) end
Return a new relation with provided dataset and additional options Use this method whenever you need to use dataset API to get a new dataset and you want to return a relation back. Typically relation API should be enough though. If you find yourself using this method, it might be worth to consider reporting an issue that some dataset functionality is not available through relation API. @example with a new dataset users.new(users.dataset.some_method) @example with a new dataset and options users.new(users.dataset.some_method, other: 'options') @param [Object] dataset @param [Hash] new_opts Additional options @api public
new
ruby
rom-rb/rom
core/lib/rom/relation.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/relation.rb
MIT
def with(opts) new_options = if opts.key?(:meta) opts.merge(meta: meta.merge(opts[:meta])) else opts end new(dataset, **options, **new_options) end
Returns a new instance with the same dataset but new options @example users.with(output_schema: -> tuple { .. }) @param [Hash] opts New options @return [Relation] @api public
with
ruby
rom-rb/rom
core/lib/rom/relation.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/relation.rb
MIT
def map_to(klass, **opts) with(opts.merge(auto_map: false, auto_struct: true, meta: { model: klass })) end
Return a new relation that will map its tuples to instances of the provided class @example users.map_to(MyUserModel) @param [Class] klass Your custom model class @return [Relation] @api public
map_to
ruby
rom-rb/rom
core/lib/rom/relation.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/relation.rb
MIT
def foreign_key(name) attr = schema.foreign_key(name.dataset) if attr attr.name else :"#{Inflector.singularize(name.dataset)}_id" end end
Return a foreign key name for the provided relation name @param [Name] name The relation name object @return [Symbol] @api private
foreign_key
ruby
rom-rb/rom
core/lib/rom/relation.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/relation.rb
MIT
def struct_namespace(ns) options[:struct_namespace] == ns ? self : with(struct_namespace: ns) end
Return a new relation configured with the provided struct namespace @param [Module] ns Custom namespace module for auto-structs @return [Relation] @api public
struct_namespace
ruby
rom-rb/rom
core/lib/rom/relation.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/relation.rb
MIT
def to_h if block_given? super else super { [_1.name, _1] } end end
Coerce schema into a <AttributeName=>Attribute> Hash @return [Hash] @api public
to_h
ruby
rom-rb/rom
core/lib/rom/schema.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/schema.rb
MIT
def project(*names) new(names.map { |name| name.is_a?(Symbol) ? self[name] : name }) end
Project a schema to include only specified attributes @param [*Array<Symbol, Attribute>] names Attribute names @return [Schema] @api public
project
ruby
rom-rb/rom
core/lib/rom/schema.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/schema.rb
MIT
def rename(mapping) new_attributes = map do |attr| alias_name = mapping[attr.name] alias_name ? attr.aliased(alias_name) : attr end new(new_attributes) end
Project a schema with renamed attributes @param [Hash] mapping The attribute mappings @return [Schema] @api public
rename
ruby
rom-rb/rom
core/lib/rom/schema.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/schema.rb
MIT
def prefix(prefix) new(map { |attr| attr.prefixed(prefix) }) end
Project a schema with renamed attributes using provided prefix @param [Symbol] prefix The name of the prefix @return [Schema] @api public
prefix
ruby
rom-rb/rom
core/lib/rom/schema.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/schema.rb
MIT
def wrap(prefix = name.dataset) new(map { |attr| attr.wrapped? ? attr : attr.wrapped(prefix) }) end
Return new schema with all attributes marked as prefixed and wrapped This is useful when relations are joined and the right side should be marked as wrapped @param [Symbol] prefix The prefix used for aliasing wrapped attributes @return [Schema] @api public
wrap
ruby
rom-rb/rom
core/lib/rom/schema.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/schema.rb
MIT
def foreign_key(relation) detect { |attr| attr.foreign_key? && attr.target == relation } end
Return FK attribute for a given relation name @return [Attribute] @api public
foreign_key
ruby
rom-rb/rom
core/lib/rom/schema.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/schema.rb
MIT
def append(*new_attributes) new(attributes + new_attributes) end
Append more attributes to the schema This returns a new schema instance @param [Array<Attribute>] new_attributes @return [Schema] @api public
append
ruby
rom-rb/rom
core/lib/rom/schema.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/schema.rb
MIT
def uniq(&) if block_given? new(attributes.uniq(&)) else new(attributes.uniq(&:name)) end end
Return a new schema with uniq attributes @return [Schema] @api public
uniq
ruby
rom-rb/rom
core/lib/rom/schema.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/schema.rb
MIT
def key?(name) !attributes.detect { |attr| attr.name == name }.nil? end
Return if a schema includes an attribute with the given name @param [Symbol] name The name of the attribute @return [Boolean] @api public
key?
ruby
rom-rb/rom
core/lib/rom/schema.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/schema.rb
MIT
def finalize!(**_opts) return self if frozen? freeze end
Finalize a schema @return [self] @api private
finalize!
ruby
rom-rb/rom
core/lib/rom/schema.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/schema.rb
MIT
def finalize_attributes!(gateway: nil, **) inferrer.(self, gateway).each { |key, value| set!(key, value) } yield if block_given? initialize_primary_key_names self end
This hook is called when relation is being build during container finalization When block is provided it'll be called just before freezing the instance so that additional ivars can be set @return [self] @api private
finalize_attributes!
ruby
rom-rb/rom
core/lib/rom/schema.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/schema.rb
MIT
def finalize_associations!(**) set!(:associations, yield) if associations.any? self end
Finalize associations defined in a schema @param [RelationRegistry] relations @return [self] @api private
finalize_associations!
ruby
rom-rb/rom
core/lib/rom/schema.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/schema.rb
MIT
def to_output_hash HASH_SCHEMA.schema( to_h { |attr| [attr.key, attr.to_read_type] } ) end
Return coercion function using attribute read types This is used for `output_schema` in relations @return [Dry::Types::Hash] @api private
to_output_hash
ruby
rom-rb/rom
core/lib/rom/schema.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/schema.rb
MIT
def to_input_hash HASH_SCHEMA.schema( to_h { |attr| [attr.name, attr.to_write_type] } ) end
Return coercion function using attribute types This is used for `input_schema` in relations, typically commands use it for processing input @return [Dry::Types::Hash] @api private
to_input_hash
ruby
rom-rb/rom
core/lib/rom/schema.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/schema.rb
MIT
def extend_dsl(dsl) dsl.extend(mod.const_get(:DSL)) if mod.const_defined?(:DSL) end
Extends a DSL instance with a module provided by the plugin @param [ROM::Schema::DSL] dsl @api private
extend_dsl
ruby
rom-rb/rom
core/lib/rom/schema_plugin.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/schema_plugin.rb
MIT
def auto_registration(directory, **options) auto_registration = AutoRegistration.new(directory, **options) auto_registration.relations.each { |r| register_relation(r) } auto_registration.commands.each { |r| register_command(r) } auto_registration.mappers.each { |r| register_mapper(r) } self end
Enable auto-registration for a given setup object @param [String, Pathname] directory The root path to components @param [Hash] options @option options [Boolean, String] :namespace Enable/disable namespace or provide a custom namespace name @return [Setup] @api public
auto_registration
ruby
rom-rb/rom
core/lib/rom/setup.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/setup.rb
MIT
def register_relation(*klasses) klasses.reduce(@relation_classes, :<<) end
Relation sub-classes are being registered with this method during setup @api private
register_relation
ruby
rom-rb/rom
core/lib/rom/setup.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/setup.rb
MIT
def register_mapper(*klasses) klasses.reduce(@mapper_classes, :<<) end
Mapper sub-classes are being registered with this method during setup @api private
register_mapper
ruby
rom-rb/rom
core/lib/rom/setup.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/setup.rb
MIT
def register_command(*klasses) klasses.reduce(@command_classes, :<<) end
Command sub-classes are being registered with this method during setup @api private
register_command
ruby
rom-rb/rom
core/lib/rom/setup.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/setup.rb
MIT
def call(*args) cache.fetch_or_store(args) do name, header, ns = args attributes = header.map(&method(:visit)).compact if attributes.empty? ROM::OpenStruct else build_class(name, ROM::Struct, ns) do |klass| klass.attributes(attributes.to_h) end end end end
Build a struct class based on relation header ast @api private
call
ruby
rom-rb/rom
core/lib/rom/struct_compiler.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/struct_compiler.rb
MIT
def key as || name end
Return the name of a key in tuples under which loaded association data are returned @return [Symbol] @api public
key
ruby
rom-rb/rom
core/lib/rom/associations/abstract.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/associations/abstract.rb
MIT
def apply_view(schema, relation) view_rel = relation.public_send(view) schema.merge(view_rel.schema).uniq(&:key).(view_rel) end
Applies custom view to the default association view @return [Relation] @api protected
apply_view
ruby
rom-rb/rom
core/lib/rom/associations/abstract.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/associations/abstract.rb
MIT
def combine_keys definition.combine_keys || { source_key => target_key } end
Return combine keys hash Combine keys are used for merging associated data together, typically these are the same as fk<=>pk mapping @return [Hash<Symbol=>Symbol>] @api public
combine_keys
ruby
rom-rb/rom
core/lib/rom/associations/abstract.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/associations/abstract.rb
MIT
def node target.with( name: target.name.as(key), meta: { keys: combine_keys, combine_type: result, combine_name: key } ) end
Return target relation configured as a combine node @return [Relation] @api private
node
ruby
rom-rb/rom
core/lib/rom/associations/abstract.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/associations/abstract.rb
MIT
def wrap target.with( name: target.name.as(key), schema: target.schema.wrap, meta: { wrap: true, combine_name: key } ) end
Return target relation as a wrap node @return [Relation] @api private
wrap
ruby
rom-rb/rom
core/lib/rom/associations/abstract.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/associations/abstract.rb
MIT
def prepare(target) if override? target.public_send(view) else call(target: target) end end
Prepare association's target relation for composition @return [Relation] @api private
prepare
ruby
rom-rb/rom
core/lib/rom/associations/abstract.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/associations/abstract.rb
MIT
def self_ref? source.name.dataset == target.name.dataset end
Return if this association's source relation is the same as the target @return [Boolean] @api private
self_ref?
ruby
rom-rb/rom
core/lib/rom/associations/abstract.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/associations/abstract.rb
MIT
def call(*) raise NotImplementedError end
Adapters should implement this method @abstract @api public
call
ruby
rom-rb/rom
core/lib/rom/associations/many_to_many.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/associations/many_to_many.rb
MIT