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 foreign_key definition.foreign_key || join_relation.foreign_key(source.name) end
Return configured or inferred FK name @return [Symbol] @api public
foreign_key
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
def associate(children, parent) ((spk, sfk), (tfk, tpk)) = join_key_map case parent when Array parent.map { |p| associate(children, p) }.flatten(1) else children.map { |tuple| { sfk => tuple.fetch(spk), tfk => parent.fetch(tpk) } } end end
Associate child tuples with the provided parent @param [Array<Hash>] children An array with child tuples @param [Array,Hash] parent An array with parent tuples or a single tuple @return [Array<Hash>] @api private
associate
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
def join_assoc if join_relation.associations.key?(through.assoc_name) join_relation.associations[through.assoc_name] else join_relation.associations[through.target] end end
Return association for many-to-many-through @return [Association] @api protected
join_assoc
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
def join_key_map left = super right = join_assoc.join_key_map [left, right] end
Return a [pk, fk] mapping for source/target relations @return [Array<Symbol>] @api protected
join_key_map
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
def call(*) raise NotImplementedError end
Adapters must implement this method @abstract @api public
call
ruby
rom-rb/rom
core/lib/rom/associations/many_to_one.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/associations/many_to_one.rb
MIT
def foreign_key definition.foreign_key || source.foreign_key(target.name) end
Return configured or inferred FK name @return [Symbol] @api public
foreign_key
ruby
rom-rb/rom
core/lib/rom/associations/many_to_one.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/associations/many_to_one.rb
MIT
def associate(child, parent) fk, pk = join_key_map child.merge(fk => parent.fetch(pk)) end
Associate child with a parent @param [Hash] child The child tuple @param [Hash] parent The parent tuple @return [Hash] @api private
associate
ruby
rom-rb/rom
core/lib/rom/associations/many_to_one.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/associations/many_to_one.rb
MIT
def call(*) raise NotImplementedError end
Adapters must implement this method @abstract @api public
call
ruby
rom-rb/rom
core/lib/rom/associations/one_to_many.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/associations/one_to_many.rb
MIT
def foreign_key definition.foreign_key || target.foreign_key(source.name) end
Return configured or inferred FK name @return [Symbol] @api public
foreign_key
ruby
rom-rb/rom
core/lib/rom/associations/one_to_many.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/associations/one_to_many.rb
MIT
def associate(child, parent) pk, fk = join_key_map child.merge(fk => parent.fetch(pk)) end
Associate child tuple with a parent @param [Hash] child The child tuple @param [Hash] parent The parent tuple @return [Hash] @api private
associate
ruby
rom-rb/rom
core/lib/rom/associations/one_to_many.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/associations/one_to_many.rb
MIT
def inherited(klass) super klass.instance_variable_set(:@before, before.dup) klass.instance_variable_set(:@after, after.dup) end
This hook sets up default class state @api private
inherited
ruby
rom-rb/rom
core/lib/rom/commands/class_interface.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/commands/class_interface.rb
MIT
def adapter_namespace(adapter) ROM.adapters.fetch(adapter).const_get(:Commands) rescue KeyError raise AdapterNotPresentError.new(adapter, :relation) end
Return namespaces that contains command subclasses of a specific adapter @param [Symbol] adapter identifier @return [Module] @api private
adapter_namespace
ruby
rom-rb/rom
core/lib/rom/commands/class_interface.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/commands/class_interface.rb
MIT
def build(relation, **options) new(relation, **self.options, **options) end
Build a command class for a specific relation with options @example class CreateUser < ROM::Commands::Create[:memory] end command = CreateUser.build(rom.relations[:users]) @param [Relation] relation @param [Hash] options @return [Command] @api public
build
ruby
rom-rb/rom
core/lib/rom/commands/class_interface.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/commands/class_interface.rb
MIT
def create_class(name, type, &) klass = Dry::Core::ClassBuilder .new(name: "#{Inflector.classify(type)}[:#{name}]", parent: type) .call if block_given? yield(klass) else klass end end
Create a command class with a specific type @param [Symbol] name Command name @param [Class] type Command class @yield [Class] @return [Class, Object] @api public
create_class
ruby
rom-rb/rom
core/lib/rom/commands/class_interface.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/commands/class_interface.rb
MIT
def use(plugin, **options) ROM.plugin_registry[:command].fetch(plugin, adapter).apply_to(self, **options) end
Use a configured plugin in this relation @example class CreateUser < ROM::Commands::Create[:memory] use :pagintion per_page 30 end @param [Symbol] plugin @param [Hash] options @option options [Symbol] :adapter (:default) first adapter to check for plugin @api public
use
ruby
rom-rb/rom
core/lib/rom/commands/class_interface.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/commands/class_interface.rb
MIT
def before(*hooks) if hooks.empty? @before else set_hooks(:before, hooks) end end
Set before-execute hooks @overload before(hook) Set an before hook as a method name @example class CreateUser < ROM::Commands::Create[:sql] relation :users register_as :create before :my_hook def my_hook(tuple, *) puts "hook called# end end @overload before(hook_opts) Set an before hook as a method name with arguments @example class CreateUser < ROM::Commands::Create[:sql] relation :users register_as :create before my_hook: { arg1: 1, arg2: 2 } def my_hook(tuple, arg1:, arg2:) puts "hook called with args: #{arg1} and #{arg2}" end end @param [Hash<Symbol=>Hash>] hook Options with method name and pre-set args @return [Array<Hash, Symbol>] A list of all configured before hooks @api public
before
ruby
rom-rb/rom
core/lib/rom/commands/class_interface.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/commands/class_interface.rb
MIT
def after(*hooks) if hooks.empty? @after else set_hooks(:after, hooks) end end
Set after-execute hooks @overload after(hook) Set an after hook as a method name @example class CreateUser < ROM::Commands::Create[:sql] relation :users register_as :create after :my_hook def my_hook(tuple, *) puts "hook called# end end @overload after(hook_opts) Set an after hook as a method name with arguments @example class CreateUser < ROM::Commands::Create[:sql] relation :users register_as :create after my_hook: { arg1: 1, arg1: 2 } def my_hook(tuple, arg1:, arg2:) puts "hook called with args: #{arg1} and #{arg2}" end end @param [Hash<Symbol=>Hash>] hook Options with method name and pre-set args @return [Array<Hash, Symbol>] A list of all configured after hooks @api public
after
ruby
rom-rb/rom
core/lib/rom/commands/class_interface.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/commands/class_interface.rb
MIT
def set_hooks(type, hooks) ivar = :"@#{type}" if instance_variable_defined?(ivar) instance_variable_get(ivar).concat(hooks) else instance_variable_set(ivar, hooks) end end
Set new or more hooks @api private
set_hooks
ruby
rom-rb/rom
core/lib/rom/commands/class_interface.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/commands/class_interface.rb
MIT
def options { input: input, result: result, before: before, after: after } end
Return default options based on class macros @return [Hash] @api private
options
ruby
rom-rb/rom
core/lib/rom/commands/class_interface.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/commands/class_interface.rb
MIT
def call(*args) response = left.call(*args) if response.nil? || (many? && response.empty?) return one? ? nil : EMPTY_ARRAY end if one? && !graph? if right.is_a?(Command) || right.is_a?(Commands::Composite) right.call([response].first) else right.call([response]).first end elsif one? && graph? right.call(response).first else right.call(response) end end
Calls the composite command Right command is called with a result from the left one @return [Object] @api public rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
call
ruby
rom-rb/rom
core/lib/rom/commands/composite.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/commands/composite.rb
MIT
def call(*args) left = root.call(*args) right = nodes.map { |node| response = if node.lazy? node.call(args.first, left) else node.call(left) end if node.one? && !node.graph? [response] else response end } if one? [[left], right] else [left, right] end end
Calls root and all nodes with the result from root Graph results are mappable through `combine` operation in mapper DSL @example create_user = rom.commands[:users].create create_task = rom.commands[:tasks].create command = create_user .curry(name: 'Jane') .combine(create_task.curry(title: 'Task')) command.call @return [Array] nested array with command results @api public rubocop:disable Metrics/PerceivedComplexity
call
ruby
rom-rb/rom
core/lib/rom/commands/graph.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/commands/graph.rb
MIT
def call(*_args) raise NotImplementedError end
Evaluate command's input using the input proc and pass to command @return [Array,Hash] @api public
call
ruby
rom-rb/rom
core/lib/rom/commands/lazy.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/commands/lazy.rb
MIT
def combine(*others) Graph.new(self, others) end
Combine with other lazy commands @see Abstract#combine @return [Graph] @api public
combine
ruby
rom-rb/rom
core/lib/rom/commands/lazy.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/commands/lazy.rb
MIT
def build(registry, options, path = EMPTY_ARRAY) options.reduce { |spec, other| build_command(registry, spec, other, path) } end
Build a command graph recursively This is used by `Container#command` when array with options is passed in @param [Registry] registry The command registry from container @param [Array] options The options array @param [Array] path The path for input evaluator proc @return [Graph] @api private
build
ruby
rom-rb/rom
core/lib/rom/commands/graph/class_interface.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/commands/graph/class_interface.rb
MIT
def build_command(registry, spec, other, path) cmd_opts, nodes = other key, relation = if spec.is_a?(Hash) spec.to_a.first else [spec, spec] end name, opts = if cmd_opts.is_a?(Hash) cmd_opts.to_a.first else [cmd_opts] end command = registry[relation][name] tuple_path = [*path] << key input_proc = InputEvaluator.build(tuple_path, nodes) command = command.curry(input_proc, opts) if nodes if nodes.all? { |node| node.is_a?(Array) } command.combine(*nodes.map { |node| build(registry, node, tuple_path) }) else command.combine(build(registry, nodes, tuple_path)) end else command end end
@api private rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
build_command
ruby
rom-rb/rom
core/lib/rom/commands/graph/class_interface.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/commands/graph/class_interface.rb
MIT
def initialize(tuple_path, excluded_keys) @tuple_path = tuple_path @excluded_keys = excluded_keys @exclude_proc = self.class.exclude_proc(excluded_keys) end
Initialize a new input evaluator @return [InputEvaluator] @api private
initialize
ruby
rom-rb/rom
core/lib/rom/commands/graph/input_evaluator.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/commands/graph/input_evaluator.rb
MIT
def call(input, index = nil) value = begin if index tuple_path[0..tuple_path.size - 2] .reduce(input) { |a, e| a.fetch(e) } .at(index)[tuple_path.last] else tuple_path.reduce(input) { |a, e| a.fetch(e) } end rescue KeyError => e raise KeyMissing, e.message end if excluded_keys value.is_a?(Array) ? value.map(&exclude_proc) : exclude_proc[value] else value end end
Evaluate input hash @param [Hash] input The input hash @param [Integer] index Optional index @return [Hash] rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity
call
ruby
rom-rb/rom
core/lib/rom/commands/graph/input_evaluator.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/commands/graph/input_evaluator.rb
MIT
def call(*args) first = args.first last = args.last size = args.size if size > 1 && last.is_a?(Array) last.map.with_index do |parent, index| children = evaluator.call(first, index) command_proc[command, parent, children].call(children, parent) end.reduce(:concat) else input = evaluator.call(first) command.call(input, *args[1..size - 1]) end end
Execute a command @see Command::Create#call @return [Hash,Array<Hash>] @api public rubocop:disable Metrics/AbcSize
call
ruby
rom-rb/rom
core/lib/rom/commands/lazy/create.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/commands/lazy/create.rb
MIT
def call(*args) first = args.first last = args.last size = args.size if size > 1 && last.is_a?(Array) raise NotImplementedError else input = evaluator.call(first) if input.is_a?(Array) input.map do |item| command_proc[command, *(size > 1 ? [last, item] : [input])].call end else command_proc[command, input].call end end end
Execute a lazy delete command @see Commands::Delete#call @return [Hash, Array<Hash>] @api public
call
ruby
rom-rb/rom
core/lib/rom/commands/lazy/delete.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/commands/lazy/delete.rb
MIT
def call(*args) first = args.first last = args.last size = args.size if size > 1 && last.is_a?(Array) last.map.with_index do |parent, index| children = evaluator.call(first, index) children.map do |child| command_proc[command, parent, child].call(child, parent) end end.reduce(:concat) else input = evaluator.call(first) if input.is_a?(Array) input.map.with_index do |item, _index| command_proc[command, last, item].call(item, *args[1..size - 1]) end else command_proc[command, *(size > 1 ? [last, input] : [input])] .call(input, *args[1..size - 1]) end end end
Execute a lazy update command @see Commands::Update#call @return [Hash, Array<Hash>] @api public rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity
call
ruby
rom-rb/rom
core/lib/rom/commands/lazy/update.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/commands/lazy/update.rb
MIT
def define(name, options = EMPTY_HASH, &) @command_classes << Command.build_class( name, relation, { adapter: adapter }.merge(options), & ) end
Define a command class @param [Symbol] name of the command @param [Hash] options @option options [Symbol] :type The type of the command @return [Class] generated class @api public
define
ruby
rom-rb/rom
core/lib/rom/configuration_dsl/command_dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/configuration_dsl/command_dsl.rb
MIT
def define(name, options = EMPTY_HASH, &) @defined_mappers << Mapper::Builder.build_class( name, @mapper_classes + @defined_mappers, options, & ) self end
Define a mapper class @param [Symbol] name of the mapper @param [Hash] options @return [Class] @api public
define
ruby
rom-rb/rom
core/lib/rom/configuration_dsl/mapper_dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/configuration_dsl/mapper_dsl.rb
MIT
def register(name, mod, options = EMPTY_HASH) registry.register(name, mod, defaults.merge(options)) end
Register a plugin @param [Symbol] name of the plugin @param [Module] mod to include @param [Hash] options @api public
register
ruby
rom-rb/rom
core/lib/rom/global/plugin_dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/global/plugin_dsl.rb
MIT
def adapter(type, &) self.class.new(registry, adapter: type, &) end
Register plugins for a specific adapter @param [Symbol] type The adapter identifier @api public
adapter
ruby
rom-rb/rom
core/lib/rom/global/plugin_dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/global/plugin_dsl.rb
MIT
def typed? type != :object end
Return if an attribute has a specific type identifier @api private
typed?
ruby
rom-rb/rom
core/lib/rom/header/attribute.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/header/attribute.rb
MIT
def aliased? key != name end
Return if an attribute should be aliased @api private
aliased?
ruby
rom-rb/rom
core/lib/rom/header/attribute.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/header/attribute.rb
MIT
def mapping { key => name } end
Return :key-to-:name mapping hash @return [Hash] @api private
mapping
ruby
rom-rb/rom
core/lib/rom/header/attribute.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/header/attribute.rb
MIT
def initialize(dataset, data) super() @dataset = dataset @data = data end
Create a linter for EnumerableDataset @param [EnumerableDataset] dataset the linted subject @param [Object] data the expected data @api public
initialize
ruby
rom-rb/rom
core/lib/rom/lint/enumerable_dataset.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/lint/enumerable_dataset.rb
MIT
def lint_each result = [] dataset.each do |tuple| # rubocop:disable Style/MapIntoArray result << tuple end return if result == data complain "#{dataset.class}#each must yield tuples" end
Lint: Ensure that +dataset+ yield tuples via +each+ @api public
lint_each
ruby
rom-rb/rom
core/lib/rom/lint/enumerable_dataset.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/lint/enumerable_dataset.rb
MIT
def lint_to_a return if dataset.to_a == data complain "#{dataset.class}#to_a must cast dataset to an array" end
Lint: Ensure that +dataset+'s array equals to expected +data+ @api public
lint_to_a
ruby
rom-rb/rom
core/lib/rom/lint/enumerable_dataset.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/lint/enumerable_dataset.rb
MIT
def initialize(identifier, gateway, uri = nil) super() @identifier = identifier @gateway = gateway @uri = uri @gateway_instance = setup_gateway_instance end
Create a gateway linter @param [Symbol] identifier @param [Class] gateway @param [String] uri optional
initialize
ruby
rom-rb/rom
core/lib/rom/lint/gateway.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/lint/gateway.rb
MIT
def lint_gateway_setup return if gateway_instance.instance_of? gateway complain <<-STRING #{gateway}.setup must return a gateway instance but returned #{gateway_instance.inspect} STRING end
Lint: Ensure that +gateway+ setups up its instance @api public
lint_gateway_setup
ruby
rom-rb/rom
core/lib/rom/lint/gateway.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/lint/gateway.rb
MIT
def lint_dataset_reader return if gateway_instance.respond_to? :[] complain "#{gateway_instance} must respond to []" end
Lint: Ensure that +gateway_instance+ responds to +[]+ @api public
lint_dataset_reader
ruby
rom-rb/rom
core/lib/rom/lint/gateway.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/lint/gateway.rb
MIT
def lint_dataset_predicate return if gateway_instance.respond_to? :dataset? complain "#{gateway_instance} must respond to dataset?" end
Lint: Ensure that +gateway_instance+ responds to +dataset?+ @api public
lint_dataset_predicate
ruby
rom-rb/rom
core/lib/rom/lint/gateway.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/lint/gateway.rb
MIT
def lint_transaction_support result = gateway_instance.transaction { 1 } complain "#{gateway_instance} must return the result of a transaction block" if result != 1 gateway_instance.transaction do |t| t.rollback! complain "#{gateway_instance} must interrupt a transaction on rollback" end end
Lint: Ensure +gateway_instance+ supports +transaction+ interface @api public
lint_transaction_support
ruby
rom-rb/rom
core/lib/rom/lint/gateway.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/lint/gateway.rb
MIT
def lint(name) before_lint public_send name after_lint true # for assertions end
Run a lint method @param [String] name @raise [ROM::Lint::Linter::Failure] if linting fails @api public
lint
ruby
rom-rb/rom
core/lib/rom/lint/linter.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/lint/linter.rb
MIT
def complain(*args) raise Failure, *args end
Raise a failure if a lint verification fails @raise [ROM::Lint::Linter::Failure] @api private
complain
ruby
rom-rb/rom
core/lib/rom/lint/linter.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/lint/linter.rb
MIT
def define_test_method(name, &) define_method "test_#{name}" do instance_eval(&) rescue ROM::Lint::Linter::Failure => e raise Minitest::Assertion, e.message end end
Defines a test method converting lint failures to assertions @param [String] name @api private
define_test_method
ruby
rom-rb/rom
core/lib/rom/lint/test.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/lint/test.rb
MIT
def initialize(attributes, options) @attributes = attributes @options = options @copy_keys = options.fetch(:copy_keys) @symbolize_keys = options.fetch(:symbolize_keys) @prefix = options.fetch(:prefix) @prefix_separator = options.fetch(:prefix_separator) @reject_keys = options.fetch(:reject_keys) @steps = [] end
@param [Array] attributes accumulator array @param [Hash] options @api private
initialize
ruby
rom-rb/rom
core/lib/rom/mapper/attribute_dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper/attribute_dsl.rb
MIT
def prefix(value = Undefined) if value.equal?(Undefined) @prefix else @prefix = value end end
Redefine the prefix for the following attributes @example dsl = AttributeDSL.new([]) dsl.attribute(:prefix, 'user') @api public
prefix
ruby
rom-rb/rom
core/lib/rom/mapper/attribute_dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper/attribute_dsl.rb
MIT
def prefix_separator(value = Undefined) if value.equal?(Undefined) @prefix_separator else @prefix_separator = value end end
Redefine the prefix separator for the following attributes @example dsl = AttributeDSL.new([]) dsl.attribute(:prefix_separator, '.') @api public
prefix_separator
ruby
rom-rb/rom
core/lib/rom/mapper/attribute_dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper/attribute_dsl.rb
MIT
def attribute(name, options = EMPTY_HASH, &block) with_attr_options(name, options) do |attr_options| if options[:type] && block_given? raise ArgumentError, "can't specify type and block at the same time" end attr_options[:coercer] = block if block_given? add_attribute(name, attr_options) end end
Define a mapping attribute with its options and/or block @example dsl = AttributeDSL.new([]) dsl.attribute(:name) dsl.attribute(:email, from: 'user_email') dsl.attribute(:name) { 'John' } dsl.attribute(:name) { |t| t.upcase } @api public
attribute
ruby
rom-rb/rom
core/lib/rom/mapper/attribute_dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper/attribute_dsl.rb
MIT
def step(options = EMPTY_HASH, &) steps << new(options, &) end
Perform transformations sequentially @example dsl = AttributeDSL.new() dsl.step do attribute :name end @api public
step
ruby
rom-rb/rom
core/lib/rom/mapper/attribute_dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper/attribute_dsl.rb
MIT
def embedded(name, options, &) with_attr_options(name) do |attr_options| mapper = options[:mapper] if mapper embedded_options = { type: :array }.update(options) attributes_from_mapper( mapper, name, embedded_options.update(attr_options) ) else dsl = new(options, &) attr_options.update(options) add_attribute( name, { header: dsl.header, type: :array }.update(attr_options) ) end end end
Define an embedded attribute Block exposes the attribute dsl too @example dsl = AttributeDSL.new([]) dsl.embedded :tags, type: :array do attribute :name end dsl.embedded :address, type: :hash do model Address attribute :name end @param [Symbol] name attribute @param [Hash] options @option options [Symbol] :type Embedded type can be :hash or :array @option options [Symbol] :prefix Prefix that should be used for its attributes @api public
embedded
ruby
rom-rb/rom
core/lib/rom/mapper/attribute_dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper/attribute_dsl.rb
MIT
def wrap(*args, &) ensure_mapper_configuration('wrap', args, block_given?) with_name_or_options(*args) do |name, options, mapper| wrap_options = { type: :hash, wrap: true }.update(options) if mapper attributes_from_mapper(mapper, name, wrap_options) else dsl(name, wrap_options, &) end end end
Define an embedded hash attribute that requires "wrapping" transformation Typically this is used in sql context when relation is a join. @example dsl = AttributeDSL.new([]) dsl.wrap(address: [:street, :zipcode, :city]) dsl.wrap(:address) do model Address attribute :street attribute :zipcode attribute :city end @see AttributeDSL#embedded @api public
wrap
ruby
rom-rb/rom
core/lib/rom/mapper/attribute_dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper/attribute_dsl.rb
MIT
def unwrap(*args, &) with_name_or_options(*args) do |name, options, mapper| unwrap_options = { type: :hash, unwrap: true }.update(options) if mapper attributes_from_mapper(mapper, name, unwrap_options) else dsl(name, unwrap_options, &) end end end
Define an embedded hash attribute that requires "unwrapping" transformation Typically this is used in no-sql context to normalize data before inserting to sql gateway. @example dsl = AttributeDSL.new([]) dsl.unwrap(address: [:street, :zipcode, :city]) dsl.unwrap(:address) do attribute :street attribute :zipcode attribute :city end @see AttributeDSL#embedded @api public
unwrap
ruby
rom-rb/rom
core/lib/rom/mapper/attribute_dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper/attribute_dsl.rb
MIT
def group(*args, &) ensure_mapper_configuration('group', args, block_given?) with_name_or_options(*args) do |name, options, mapper| group_options = { type: :array, group: true }.update(options) if mapper attributes_from_mapper(mapper, name, group_options) else dsl(name, group_options, &) end end end
Define an embedded hash attribute that requires "grouping" transformation Typically this is used in sql context when relation is a join. @example dsl = AttributeDSL.new([]) dsl.group(tags: [:name]) dsl.group(:tags) do model Tag attribute :name end @see AttributeDSL#embedded @api public
group
ruby
rom-rb/rom
core/lib/rom/mapper/attribute_dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper/attribute_dsl.rb
MIT
def ungroup(*args, &) with_name_or_options(*args) do |name, options, *| ungroup_options = { type: :array, ungroup: true }.update(options) dsl(name, ungroup_options, &) end end
Define an embedded array attribute that requires "ungrouping" transformation Typically this is used in non-sql context being prepared for import to sql. @example dsl = AttributeDSL.new([]) dsl.ungroup(tags: [:name]) @see AttributeDSL#embedded @api public
ungroup
ruby
rom-rb/rom
core/lib/rom/mapper/attribute_dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper/attribute_dsl.rb
MIT
def fold(*args, &) with_name_or_options(*args) do |name, *| fold_options = { type: :array, fold: true } dsl(name, fold_options, &) end end
Define an embedded hash attribute that requires "fold" transformation Typically this is used in sql context to fold single joined field to the array of values. @example dsl = AttributeDSL.new([]) dsl.fold(tags: [:name]) @see AttributeDSL#embedded @api public
fold
ruby
rom-rb/rom
core/lib/rom/mapper/attribute_dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper/attribute_dsl.rb
MIT
def unfold(name, options = EMPTY_HASH) with_attr_options(name, options) do |attr_options| old_name = attr_options.fetch(:from, name) dsl(old_name, type: :array, unfold: true) do attribute name, attr_options yield if block_given? end end end
Define an embedded hash attribute that requires "unfold" transformation Typically this is used in non-sql context to convert array of values (like in Cassandra 'SET' or 'LIST' types) to array of tuples. Source values are assigned to the first key, the other keys being left blank. @example dsl = AttributeDSL.new([]) dsl.unfold(tags: [:name, :type], from: :tags_list) dsl.unfold :tags, from: :tags_list do attribute :name, from: :tag_name attribute :type, from: :tag_type end @see AttributeDSL#embedded @api public
unfold
ruby
rom-rb/rom
core/lib/rom/mapper/attribute_dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper/attribute_dsl.rb
MIT
def combine(name, options, &) dsl = new(options, &) attr_opts = { type: options.fetch(:type, :array), keys: options.fetch(:on), combine: true, header: dsl.header } add_attribute(name, attr_opts) end
Define an embedded combined attribute that requires "combine" transformation Typically this can be used to process results of eager-loading @example dsl = AttributeDSL.new([]) dsl.combine(:tags, user_id: :id) do model Tag attribute :name end @param [Symbol] name @param [Hash] options @option options [Hash] :on The "join keys" @option options [Symbol] :type The type, either :array (default) or :hash @api public
combine
ruby
rom-rb/rom
core/lib/rom/mapper/attribute_dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper/attribute_dsl.rb
MIT
def header Header.coerce(attributes, copy_keys: copy_keys, model: model, reject_keys: reject_keys) end
Generate a header from attribute definitions @return [Header] @api private
header
ruby
rom-rb/rom
core/lib/rom/mapper/attribute_dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper/attribute_dsl.rb
MIT
def remove(*names) attributes.delete_if { |attr| names.include?(attr.first) } end
Remove the attribute used somewhere else (in wrap, group, model etc.) @api private
remove
ruby
rom-rb/rom
core/lib/rom/mapper/attribute_dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper/attribute_dsl.rb
MIT
def with_attr_options(name, options = EMPTY_HASH) attr_options = options.dup if @prefix attr_options[:from] ||= "#{@prefix}#{@prefix_separator}#{name}" attr_options[:from] = attr_options[:from].to_sym if name.is_a? Symbol end attr_options.update(from: attr_options.fetch(:from) { name }.to_s) if symbolize_keys yield(attr_options) end
Handle attribute options common for all definitions @api private
with_attr_options
ruby
rom-rb/rom
core/lib/rom/mapper/attribute_dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper/attribute_dsl.rb
MIT
def with_name_or_options(*args) name, options = if args.size > 1 args else [args.first, {}] end yield(name, options, options[:mapper]) end
Handle "name or options" syntax used by `wrap` and `group` @api private
with_name_or_options
ruby
rom-rb/rom
core/lib/rom/mapper/attribute_dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper/attribute_dsl.rb
MIT
def dsl(name_or_attrs, options, &) if block_given? attributes_from_block(name_or_attrs, options, &) else attributes_from_hash(name_or_attrs, options) end end
Create another instance of the dsl for nested definitions This is used by embedded, wrap and group @api private
dsl
ruby
rom-rb/rom
core/lib/rom/mapper/attribute_dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper/attribute_dsl.rb
MIT
def attributes_from_block(name, options, &) dsl = new(options, &) header = dsl.header add_attribute(name, options.update(header: header)) header.each { |attr| remove(attr.key) unless name == attr.key } end
Define attributes from a nested block Used by embedded, wrap and group @api private
attributes_from_block
ruby
rom-rb/rom
core/lib/rom/mapper/attribute_dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper/attribute_dsl.rb
MIT
def attributes_from_hash(hash, options) hash.each do |name, header| with_attr_options(name, options) do |attr_options| add_attribute(name, attr_options.update(header: header.zip)) header.each { |attr| remove(attr) unless name == attr } end end end
Define attributes from the `name => attributes` hash syntax Used by wrap and group @api private
attributes_from_hash
ruby
rom-rb/rom
core/lib/rom/mapper/attribute_dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper/attribute_dsl.rb
MIT
def attributes_from_mapper(mapper, name, options) if mapper.is_a?(Class) add_attribute(name, { header: mapper.header }.update(options)) else raise( ArgumentError, ":mapper must be a class #{mapper.inspect}" ) end end
Infer mapper header for an embedded attribute @api private
attributes_from_mapper
ruby
rom-rb/rom
core/lib/rom/mapper/attribute_dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper/attribute_dsl.rb
MIT
def add_attribute(name, options) remove(name, name.to_s) attributes << [name, options] end
Add a new attribute and make sure it overrides previous definition @api private
add_attribute
ruby
rom-rb/rom
core/lib/rom/mapper/attribute_dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper/attribute_dsl.rb
MIT
def new(options, &) dsl = self.class.new([], @options.merge(options)) dsl.instance_exec(&) if block_given? dsl end
Create a new dsl instance of potentially overidden options Embedded, wrap and group can override top-level options like `prefix` @api private
new
ruby
rom-rb/rom
core/lib/rom/mapper/attribute_dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper/attribute_dsl.rb
MIT
def ensure_mapper_configuration(method_name, args, block_present) if args.first.is_a?(Hash) && block_present raise MapperMisconfiguredError, "Cannot configure `#{method_name}#` using both options and a block" end if args.first.is_a?(Hash) && args.first[:mapper] raise MapperMisconfiguredError, "Cannot configure `#{method_name}#` using both options and a mapper" end end
Ensure the mapping configuration isn't ambiguous @api private
ensure_mapper_configuration
ruby
rom-rb/rom
core/lib/rom/mapper/attribute_dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper/attribute_dsl.rb
MIT
def inherited(klass) super klass.instance_variable_set('@attributes', nil) klass.instance_variable_set('@header', nil) klass.instance_variable_set('@dsl', nil) end
Set base ivars for the mapper class @api private
inherited
ruby
rom-rb/rom
core/lib/rom/mapper/dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper/dsl.rb
MIT
def use(plugin, options = {}) adapter = options.fetch(:adapter, :default) ROM.plugin_registry[:mapper].fetch(plugin, adapter).apply_to(self) end
include a registered plugin in this mapper @param [Symbol] plugin @param [Hash] options @option options [Symbol] :adapter (:default) first adapter to check for plugin @api public
use
ruby
rom-rb/rom
core/lib/rom/mapper/dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper/dsl.rb
MIT
def base_relation superclass.relation || relation end
Return base_relation used for creating mapper registry This is used to "gather" mappers under same root name @api private
base_relation
ruby
rom-rb/rom
core/lib/rom/mapper/dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper/dsl.rb
MIT
def header @header ||= dsl.header end
Return header of the mapper This is memoized so mutating mapper class won't have an effect wrt header after it was initialized for the first time. TODO: freezing mapper class here is probably a good idea @api private
header
ruby
rom-rb/rom
core/lib/rom/mapper/dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper/dsl.rb
MIT
def options { copy_keys: copy_keys, prefix: prefix, prefix_separator: prefix_separator, symbolize_keys: symbolize_keys, reject_keys: reject_keys } end
Return default Attribute DSL options based on settings of the mapper class @api private
options
ruby
rom-rb/rom
core/lib/rom/mapper/dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper/dsl.rb
MIT
def attributes @attributes ||= if superclass.respond_to?(:attributes, true) && inherit_header superclass.attributes.dup else [] end end
Return default attributes that might have been inherited from the superclass @api private
attributes
ruby
rom-rb/rom
core/lib/rom/mapper/dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper/dsl.rb
MIT
def dsl @dsl ||= AttributeDSL.new(attributes, options) end
Create the attribute DSL instance used by the mapper class @api private
dsl
ruby
rom-rb/rom
core/lib/rom/mapper/dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper/dsl.rb
MIT
def method_missing(name, ...) if dsl.respond_to?(name) dsl.public_send(name, ...) else super end end
Delegate Attribute DSL method to the dsl instance @api private
method_missing
ruby
rom-rb/rom
core/lib/rom/mapper/dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper/dsl.rb
MIT
def model(options = nil) if options.is_a?(Class) @klass = options elsif options type = options.fetch(:type) { DEFAULT_TYPE } @builder = ModelBuilder[type].new(options) end build_class unless options end
Set or generate a model @example class MyDefinition include ROM::Mapper::ModelDSL def initialize @attributes = [[:name], [:title]] end end definition = MyDefinition.new # just set a model constant definition.model(User) # generate model class for the attributes definition.model(name: 'User') @api public
model
ruby
rom-rb/rom
core/lib/rom/mapper/model_dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper/model_dsl.rb
MIT
def build_class return klass if klass included_attrs = attributes.reject do |_name, opts| opts && opts[:exclude] end builder&.call(included_attrs.map(&:first)) end
Build a model class using a specialized builder @api private
build_class
ruby
rom-rb/rom
core/lib/rom/mapper/model_dsl.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/mapper/model_dsl.rb
MIT
def join(*args) left, right = args.size > 1 ? args : [self, args.first] join_map = left.each_with_object({}) { |tuple, h| others = right.to_a.find_all { |t| tuple.to_a.intersect?(t.to_a) } (h[tuple] ||= []).concat(others) } tuples = left.flat_map { |tuple| join_map[tuple].map { |other| tuple.merge(other) } } self.class.new(tuples, **options) end
Join with other datasets @param [Array<Dataset>] args A list of dataset to join with @return [Dataset] @api public rubocop:disable Metrics/AbcSize
join
ruby
rom-rb/rom
core/lib/rom/memory/dataset.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/memory/dataset.rb
MIT
def restrict(criteria = nil, &) return find_all(&) unless criteria find_all do |tuple| criteria.all? do |k, v| case v when ::Array then v.include?(tuple[k]) when ::Regexp then tuple[k].match(v) else tuple[k].eql?(v) end end end end
rubocop:enable Metrics/AbcSize Restrict a dataset @param [Hash] criteria A hash with conditions @return [Dataset] @api public
restrict
ruby
rom-rb/rom
core/lib/rom/memory/dataset.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/memory/dataset.rb
MIT
def project(*names) map { |tuple| tuple.select { |key| names.include?(key) } } end
Project a dataset @param [Array<Symbol>] names A list of attribute names @return [Dataset] @api public
project
ruby
rom-rb/rom
core/lib/rom/memory/dataset.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/memory/dataset.rb
MIT
def order(*fields) nils_first = fields.pop[:nils_first] if fields.last.is_a?(Hash) sort do |a, b| fields # finds the first difference between selected fields of tuples .map { |n| __compare__ a[n], b[n], nils_first } .detect(-> { 0 }) { |r| r != 0 } end end
Sort a dataset @param [Array<Symbol>] fields Names of fields to order tuples by @option [Boolean] :nils_first (false) Whether `nil` values should be placed before others @return [Dataset] @api public
order
ruby
rom-rb/rom
core/lib/rom/memory/dataset.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/memory/dataset.rb
MIT
def insert(tuple) data << tuple self end
Insert tuple into a dataset @param [Hash] tuple A new tuple for insertion @api public @return [Dataset]
insert
ruby
rom-rb/rom
core/lib/rom/memory/dataset.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/memory/dataset.rb
MIT
def delete(tuple) data.delete(tuple) self end
Delete tuples from a dataset @param [Hash] tuple A new tuple for deletion @return [Dataset] @api public
delete
ruby
rom-rb/rom
core/lib/rom/memory/dataset.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/memory/dataset.rb
MIT
def __compare__(a, b, nils_first) return a <=> b unless a.nil? ^ b.nil? nils_first ^ b.nil? ? -1 : 1 end
Compares two values, that are either comparable, or can be nils @api private
__compare__
ruby
rom-rb/rom
core/lib/rom/memory/dataset.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/memory/dataset.rb
MIT
def use_logger(logger) @logger = logger end
Set default logger for the gateway @param [Object] logger object @api public
use_logger
ruby
rom-rb/rom
core/lib/rom/memory/gateway.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/memory/gateway.rb
MIT
def dataset(name) self[name] || connection.create_dataset(name) end
Register a dataset in the gateway If dataset already exists it will be returned @return [Dataset] @api public
dataset
ruby
rom-rb/rom
core/lib/rom/memory/gateway.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/memory/gateway.rb
MIT
def insert(*args) dataset.insert(*args) self end
Insert tuples into the relation @example users.insert(name: 'Jane') @return [Relation] @api public
insert
ruby
rom-rb/rom
core/lib/rom/memory/relation.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/memory/relation.rb
MIT
def delete(*args) dataset.delete(*args) self end
Delete tuples from the relation @example users.insert(name: 'Jane') users.delete(name: 'Jane') @return [Relation] @api public
delete
ruby
rom-rb/rom
core/lib/rom/memory/relation.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/memory/relation.rb
MIT
def finalize_associations!(relations:) super do associations.map do |definition| Memory::Associations.const_get(definition.type).new(definition, relations) end end end
Internal hook used during setup process @see Schema#finalize_associations! @api private
finalize_associations!
ruby
rom-rb/rom
core/lib/rom/memory/schema.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/memory/schema.rb
MIT
def create_dataset(name) data[name] = Dataset.new(Concurrent::Array.new) end
Register a new dataset @return [Dataset] @api private
create_dataset
ruby
rom-rb/rom
core/lib/rom/memory/storage.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/memory/storage.rb
MIT
def build(relation, **options) if relation.schema? && !options.key?(:input) relation_input = relation.input_schema command_input = input composed_input = if command_input.equal?(ROM::Command.input) relation_input else -> tuple { relation_input[command_input[tuple]] } end super(relation, **options, input: composed_input) else super end end
Build a command and set it input to relation's input_schema @see Command.build @return [Command] @api public
build
ruby
rom-rb/rom
core/lib/rom/plugins/command/schema.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/plugins/command/schema.rb
MIT
def set_timestamps(tuples, *) timestamps = build_timestamps map_input_tuples(tuples) { |t| timestamps.merge(t) } end
Set the timestamp attributes on the given tuples @param [Array<Hash>, Hash] tuples the input tuple(s) @return [Array<Hash>, Hash] @api private
set_timestamps
ruby
rom-rb/rom
core/lib/rom/plugins/command/timestamps.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/plugins/command/timestamps.rb
MIT
def timestamps(*names) timestamp_columns timestamp_columns.merge(names) end
@api private Set up attributes to timestamp when the command is called @example class CreateTask < ROM::Commands::Create[:sql] result :one use :timestamps timestamps :created_at, :updated_at end create_user = rom.command(:user).create.curry(name: 'Jane') result = create_user.call result[:created_at] #=> Time.now.utc @param [Array<Symbol>] names A list of attribute names @api public
timestamps
ruby
rom-rb/rom
core/lib/rom/plugins/command/timestamps.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/plugins/command/timestamps.rb
MIT
def datestamps(*names) datestamp_columns datestamp_columns.merge(names) end
Set up attributes to datestamp when the command is called @example class CreateTask < ROM::Commands::Create[:sql] result :one use :timestamps datestamps :created_on, :updated_on end create_user = rom.command(:user).create.curry(name: 'Jane') result = create_user.call result[:created_at] #=> Date.today @param [Array<Symbol>] names A list of attribute names @api public
datestamps
ruby
rom-rb/rom
core/lib/rom/plugins/command/timestamps.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/plugins/command/timestamps.rb
MIT
def instrument(*methods) (methods - Instrumentation.mixin.instance_methods).each do |meth| Instrumentation.mixin.send(:define_method, meth) do instrument { super() } end end end
Configure provided methods for instrumentation @param [Array<Symbol>] methods A list of method names @api public
instrument
ruby
rom-rb/rom
core/lib/rom/plugins/relation/instrumentation.rb
https://github.com/rom-rb/rom/blob/master/core/lib/rom/plugins/relation/instrumentation.rb
MIT