### Connect to SQL Database and Define Relations with ROM Source: https://rom-rb.org/guides/sql-quick-start/connecting-to-existing-database This snippet demonstrates how to establish a connection to a PostgreSQL database using ROM and configure a relation for the 'users' table. It infers the schema automatically and enables struct auto-loading. It also shows how to create a new user record and retrieve it. ```ruby require "rom" rom = ROM.container(:sql, 'postgres://localhost/my_db', username: 'user', password: 'secret') do |config| config.relation(:users) do schema(infer: true) auto_struct true end end users = rom.relations[:users] users.changeset(:create, name: "Jane").commit jane = users.where(name: "Jane").one ``` -------------------------------- ### ROM Container Setup with Custom Commands (Ruby) Source: https://rom-rb.org/guides/adapters/how-to-build-an-adapter Illustrates setting up a ROM container with custom relation and command classes using the Array adapter. It shows how to define relation-specific methods, register commands, and then use the container to create, update, and delete data, including using relation methods for filtering before command execution. ```ruby configuration = ROM::Configuration.new(:array) class Users < ROM::Relation[:array] def by_name(name) select { |user| user[:name] == name } end end class CreateUser < ROM::Commands::Create[:array] relation :users register_as :create end class UpdateUser < ROM::Commands::Update[:array] relation :users result :one register_as :update end class DeleteUser < ROM::Commands::Delete[:array] relation :users result :one register_as :delete end configuration.register_relation(Users) configuration.register_command(CreateUser) configuration.register_command(UpdateUser) configuration.register_command(DeleteUser) rom = ROM.container(configuration) create_users = rom.commands[:users][:create] update_user = rom.commands[:users][:update] delete_user = rom.commands[:users][:delete] create_users.call([{ name: 'Jane' }, { name: 'John' }]) puts rom.relations[:users].to_a.inspect # [{:name=>"Jane"}, {:name=>"John"}] puts rom.relations[:users].by_name('Jane').to_a.inspect # [{:name=>"Jane"}] update_user.by_name('Jane').call(name: 'Jane Doe') puts rom.relations[:users].to_a.inspect # [{:name=>"Jane Doe"}, {:name=>"John"}] delete_user.by_name('John').call puts rom.relations[:users].to_a.inspect # [{:name=>"Jane Doe"}] ``` -------------------------------- ### Implement ROM Delete Command (Ruby) Source: https://rom-rb.org/guides/adapters/how-to-build-an-adapter Shows how to implement a custom `Delete` command for the Array adapter. It inherits from `ROM::Commands::Delete` and overrides the `execute` method to remove tuples from the source relation. The example illustrates creating a delete command instance and calling it. ```ruby require 'rom/commands/delete' module ROM module ArrayAdapter module Commands class Delete < ROM::Commands::Delete adapter :array def execute relation.each { |tuple| source.delete(tuple) } end end end end end delete_users = ROM::ArrayAdapter::Commands::Delete.new(users) delete_users.call puts users.to_a.inspect # [] ``` -------------------------------- ### Implement ROM Update Command (Ruby) Source: https://rom-rb.org/guides/adapters/how-to-build-an-adapter Details the creation of a custom `Update` command for the Array adapter, inheriting from `ROM::Commands::Update`. The `execute` method is defined to update tuples in the relation with provided attributes. The example demonstrates instantiating and calling the update command. ```ruby require 'rom/commands/update' module ROM module ArrayAdapter module Commands class Update < ROM::Commands::Update adapter :array def execute(attributes) relation.each { |tuple| tuple.update(attributes) } end end end end end update_users = ROM::ArrayAdapter::Commands::Update.new(users) update_users.call(age: 21) puts users.to_a.inspect # [{:name=>"Jane", :age=>21}] ``` -------------------------------- ### Implement ROM Create Command (Ruby) Source: https://rom-rb.org/guides/adapters/how-to-build-an-adapter Demonstrates how to create a custom `Create` command for the Array adapter in ROM. This involves inheriting from `ROM::Commands::Create` and defining the `execute` method to add tuples to a relation. It shows basic usage with a relation and command object. ```ruby require 'rom/commands/create' module ROM module ArrayAdapter module Commands class Create < ROM::Commands::Create adapter :array def execute(tuples) tuples.each { |tuple| relation << tuple } end end end end end users = ROM::ArrayAdapter::Relation.new(gateway.dataset(:users)) create_users = ROM::ArrayAdapter::Commands::Create.new(users) create_users.call([{ name: 'Jane' }]) puts users.to_a.inspect # [{:name=>"Jane"}] ``` -------------------------------- ### ROM Array Adapter Gateway Implementation Source: https://rom-rb.org/guides/adapters/how-to-build-an-adapter Implements the ROM::Gateway interface for a simple array-based data source. It manages datasets (arrays) and allows retrieving them by name. This is the foundation for accessing data within the adapter. ```ruby require 'rom' module ROM module ArrayAdapter class Gateway < ROM::Gateway attr_reader :datasets def initialize @datasets = Hash.new { |h, k| h[k] = [] } end def dataset(name) datasets[name] end def dataset?(name) datasets.key?(name) end end end end gateway = ROM::ArrayAdapter::Gateway.new users = gateway.dataset(:users) tasks = gateway.dataset(:tasks) gateway.dataset?(:users) # true gateway.dataset?(:tasks) # true ``` -------------------------------- ### Registering the ROM Array Adapter Source: https://rom-rb.org/guides/adapters/how-to-build-an-adapter Registers the custom array adapter with ROM using `ROM.register_adapter`. This allows ROM to recognize and utilize the adapter for setting up components. It also demonstrates configuring ROM with the registered adapter and defining a custom relation. ```ruby ROM.register_adapter(:array, ROM::ArrayAdapter) configuration= ROM::Configuration.new(:array) class Users < ROM::Relation[:array] def by_name(name) select { |user| user[:name] == name } end end configuration.register_relation(Users) rom = ROM.container(configuration) users = rom.gateways[:default].dataset(:users) users << { name: 'Jane' } users << { name: 'John' } rom.relations[:users].by_name('Jane').to_a # [{:name=>"Jane"}] ``` -------------------------------- ### ROM Array Adapter Relation Implementation Source: https://rom-rb.org/guides/adapters/how-to-build-an-adapter Defines a ROM::Relation subclass for the array adapter. It configures the adapter identifier and uses the `forward` macro to expose array methods like `select` and `reject` directly on the relation. This allows for adapter-specific query capabilities. ```ruby module ROM module ArrayAdapter class Relation < ROM::Relation # we must configure adapter identifier here adapter :array forward :select, :reject end end end users = gateway.dataset(:users) users << { name: 'Jane' } users << { name: 'John' } relation = ROM::ArrayAdapter::Relation.new(gateway.dataset(:users)) relation.select { |tuple| tuple[:name] == 'Jane' }.inspect # #"Jane"}]> ``` -------------------------------- ### ROM Array Adapter Relation with Writing Methods Source: https://rom-rb.org/guides/adapters/how-to-build-an-adapter Extends the ROM::ArrayAdapter::Relation to include writing capabilities by forwarding methods like `<<` (append) and `delete` to the underlying dataset. It also adds a `count` method for determining the number of records. ```ruby module ROM module ArrayAdapter class Relation < ROM::Relation adapter :array # reading forward :select, :reject # writing forward :<<, :delete def count dataset.size end end end end ``` -------------------------------- ### Define User and Team Relations in ROM Source: https://rom-rb.org/guides/sql-how-to/order-by-nested-association-attribute Defines the ROM relations for 'users' and 'teams' tables, including associations and attributes. Assumes 'users' table has 'created_at' and 'team_id', and 'teams' table has a 'name'. ```ruby module Relations class Users < ROM::Relation[:sql] attribute :created_at, ROM::Types::DateTime schema(:users) do associations do belongs_to :team end end end class Teams < ROM::Relation[:sql] schema(:teams) do attribute :name, ROM::Types::String associations do has_many :users end end end end ``` -------------------------------- ### Order Users by Team Name and Creation Date Source: https://rom-rb.org/guides/sql-how-to/order-by-nested-association-attribute Retrieves all users, ordered first by their associated team's name and then by their creation date. This query uses a JOIN to access the 'teams' table and applies multi-column ordering. ```sql users.join(:teams).order(teams[:name], :created_at) ``` ```ruby users.combine(:teams).join(:teams).order(teams[:name], :created_at).to_a ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.