### Redis Sorting Options in Ohm Source: https://github.com/soveran/ohm/blob/master/README.md Details the options available for sorting collections in Ohm, which map to Redis SORT command parameters. Includes :order, :limit, :by, and :get. ```redis SORT mylist ASC LIMIT 0 10 GET #->name SORT mylist BY weight ASC LIMIT 0 5 GET #->name SORT mylist BY #->name DESC ALPHA ``` -------------------------------- ### Ohm Sorting Options Explained Source: https://github.com/soveran/ohm/blob/master/README.md Details the parameters for sorting collections in Ohm: `:order` (ASC, DESC, ALPHA), `:limit` (offset, count), `:by` (key or hash key), and `:get` (key pattern). ```ruby # :order options # ASC, ASC ALPHA, DESC, DESC ALPHA # :limit option # limit: [offset, count] # :by option # :title (for sort_by) # :title (for sort) # :get option # get: :title ``` -------------------------------- ### Redis Commands for Post Creation Source: https://github.com/soveran/ohm/blob/master/examples/redis-logging.txt Demonstrates the sequence of Redis commands executed when creating a new Post object in Ohm. This includes key generation, locking, set manipulation, transactions, and attribute setting. ```redis INCR Post:id SETNX Post:2:_lock 1285060009.409451 SADD Post:all 2 MULTI DEL Post:2 HMSET Post:2 title "Grokking Ohm" EXEC DEL Post:2:_lock ``` -------------------------------- ### Redis Commands for Indexing and Retrieving Posts by User Source: https://github.com/soveran/ohm/blob/master/examples/redis-logging.txt Explains the Redis commands used for indexing posts by a user ID and retrieving them. It demonstrates how Ohm uses sets to manage these relationships and how to query them. ```redis SADD Post:user_id:OQ== 3 SADD Post:3:_indices Post:user_id:OQ== SMEMBERS Post:user_id:OQ== ``` -------------------------------- ### Redis Commands for User Creation Source: https://github.com/soveran/ohm/blob/master/examples/redis-logging.txt Illustrates the Redis commands used by Ohm to create a new User record. It covers ID incrementing, locking, and adding the new user ID to a global set. ```redis INCR User:id SETNX User:9:_lock 1285061032.174834 SADD User:all 9 DEL User:9:_lock ``` -------------------------------- ### Redis Commands for Post Creation with User Reference Source: https://github.com/soveran/ohm/blob/master/examples/redis-logging.txt Details the Redis operations for creating a Post that references a User. This includes generating IDs, locking, updating sets, and setting attributes including the foreign key. ```redis INCR Post:id SETNX Post:3:_lock 1285061032.180314 SADD Post:all 3 MULTI DEL Post:3 HMSET Post:3 title Foo user_id 9 EXEC SADD Post:user_id:OQ== 3 SADD Post:3:_indices Post:user_id:OQ== DEL Post:3:_lock ``` -------------------------------- ### Ohm Reference Implementation Detail Source: https://github.com/soveran/ohm/blob/master/README.md Explains the internal mechanism of Ohm's 'reference' macro by showing its equivalent manual implementation using attributes, indices, and getter/setter methods. ```ruby class Comment < Ohm::Model attribute :body attribute :post_id index :post_id def post=(post) self.post_id = post.id end def post Post[post_id] end end ``` -------------------------------- ### Connecting to Redis with Ohm Source: https://github.com/soveran/ohm/blob/master/README.md Demonstrates how to establish a connection to a Redis database using Ohm and the Redic client. It shows setting a custom connection URL and performing basic Redis operations. ```ruby require "ohm" Ohm.redis = Redic.new("redis://127.0.0.1:6379") Ohm.redis.call "SET", "Foo", "Bar" Ohm.redis.call "GET", "Foo" # => "Bar" ``` ```ruby require "ohm" # Ohm defaults to a Redic connection to "redis://127.0.0.1:6379" Ohm.redis.call "SET", "Foo", "Bar" Ohm.redis.call "GET", "Foo" # => "Bar" ``` ```ruby require "ohm" Ohm.redis = Redic.new(ENV["REDIS_URL1"]) class User < Ohm::Model end User.redis = Redic.new(ENV["REDIS_URL2"]) ``` -------------------------------- ### Ohm Model Indexing for Lookups Source: https://github.com/soveran/ohm/blob/master/README.md Explains the purpose of Ohm's 'index' macro for creating efficient lookups, enabling methods like 'find' and 'except' to work effectively. ```ruby class Event < Ohm::Model attribute :name index :name end # This allows for searches like: # Event.find(name: "some value") ``` -------------------------------- ### Ohm Collection Implementation Detail Source: https://github.com/soveran/ohm/blob/master/README.md Illustrates how Ohm's 'collection' macro simplifies finding associated records by automatically creating a finder method based on an index. ```ruby class Post < Ohm::Model attribute :title attribute :body def comments Comment.find(post_id: self.id) end end ``` -------------------------------- ### Defining Ohm Models Source: https://github.com/soveran/ohm/blob/master/README.md Illustrates how to define models in Ohm, including attributes, references, sets, and counters. It showcases the basic structure of an Ohm model and its interaction with Redis. ```ruby class Event < Ohm::Model attribute :name reference :venue, :Venue set :participants, :Person counter :votes index :name end class Venue < Ohm::Model attribute :name collection :events, :Event end class Person < Ohm::Model attribute :name end ``` -------------------------------- ### Ohm Model Associations: References and Collections Source: https://github.com/soveran/ohm/blob/master/README.md Demonstrates how to define one-to-one and one-to-many associations between Ohm models using 'reference' and 'collection'. It shows the basic syntax and how these map to underlying Redis operations. ```ruby class Post < Ohm::Model attribute :title attribute :body collection :comments, :Comment end class Comment < Ohm::Model attribute :body reference :post, :Post end ``` -------------------------------- ### Interacting with Ohm Models Source: https://github.com/soveran/ohm/blob/master/README.md Demonstrates common operations with Ohm models, such as creating, retrieving, updating, and querying records. It covers ID management, finding by ID, and retrieving all records. ```ruby event = Event.create :name => "Ohm Worldwide Conference 2031" event.id # => 1 # Find an event by id event == Event[1] # => true # Update an event event.update :name => "Ohm Worldwide Conference 2032" # => #"Ohm Worldwide Conference"}, @_memo={}, @id="1"> # Trying to find a non existent event Event[2] # => nil # Finding all the events Event.all.to_a # => [] ``` -------------------------------- ### Working with Ohm Sets Source: https://github.com/soveran/ohm/blob/master/README.md Shows how to add elements to a set attribute and iterate over the elements in the set. The `add` method is used to add instances, and `each` to iterate. ```ruby class Event < Ohm::Model attribute :name set :attendees, :Person end event.attendees.add(Person.create(name: "Albert")) # And now... event.attendees.each do |person| # ...do what you want with this person. end ``` -------------------------------- ### Ohm Model Finding Records Source: https://github.com/soveran/ohm/blob/master/README.md Shows how to use the 'find' method in Ohm to retrieve collections of records based on attribute-value pairs. ```ruby # This returns a collection of users with the username "Albert" User.find(username: "Albert") ``` -------------------------------- ### Redis Commands for Appending Comment to Post Source: https://github.com/soveran/ohm/blob/master/examples/redis-logging.txt Shows the Redis commands executed when appending a Comment to a Post's comment list. This involves ID generation, locking, set updates, and using RPUSH to add to the list. ```redis INCR Comment:id SETNX Comment:1:_lock 1285061034.335855 SADD Comment:all 1 DEL Comment:1:_lock RPUSH Post:3:comments 1 ``` -------------------------------- ### Tracking Arbitrary Keys with Ohm Source: https://github.com/soveran/ohm/blob/master/README.md Demonstrates how to use the `track` keyword to associate arbitrary Redis keys with an Ohm model's lifecycle. When the model is deleted, the tracked keys are also deleted. ```ruby class Log < Ohm::Model track :text def append(msg) redis.call("APPEND", key[:text], msg) end def tail(n = 100) redis.call("GETRANGE", key[:text], -(n), -1) end end log = Log.create log.append("hello\n") assert_equal "hello\n", log.tail log.append("world\n") assert_equal "world\n", log.tail(6) ``` -------------------------------- ### Ohm Model Filtering Results Source: https://github.com/soveran/ohm/blob/master/README.md Illustrates advanced filtering techniques in Ohm using 'find', 'combine', 'except', and 'union' methods, which correspond to Redis set operations. ```ruby # Find all users from Argentina User.find(country: "Argentina") # Find all active users from Argentina User.find(country: "Argentina", status: "active") # Find all active users from Argentina and Uruguay User.find(country: "Argentina").combine(country: ["Argentina", "Uruguay"]) # Find all users from Argentina, except those with a suspended account. User.find(country: "Argentina").except(status: "suspended") # Find all users both from Argentina and Uruguay User.find(country: "Argentina").union(country: "Uruguay") ``` -------------------------------- ### Ohm Namespaced Model References Source: https://github.com/soveran/ohm/blob/master/README.md Demonstrates how to define references for models that are nested within Ruby modules, requiring the full namespaced class name as a string. ```ruby module SomeNamespace class Foo < Ohm::Model attribute :name end class Bar < Ohm::Model reference :foo, 'SomeNamespace::Foo' end end ``` -------------------------------- ### Redis Set Operations for Ohm Filtering Source: https://github.com/soveran/ohm/blob/master/README.md Provides context on the underlying Redis commands that Ohm's filtering methods like combine, except, and union map to. ```redis SINTERSTORE SDIFFSTORE SUNIONSTORE ``` -------------------------------- ### Ohm Sorting Methods: sort vs sort_by Source: https://github.com/soveran/ohm/blob/master/README.md Explains the difference between `Ohm::Model::Collection#sort` and `Ohm::Model::Collection#sort_by`. `sort` is for sorting by ID, while `sort_by` is for sorting by attribute values. ```ruby Post.all.sort_by(:title) # SORT Post:all BY Post:*->title Post.all.sort(by: :title) # SORT Post:all BY title Post.all.sort_by(:title, get: :title) # SORT Post:all BY Post:*->title GET Post:*->title Post.all.sort(by: :title, get: :title) # SORT Post:all BY title GET title ``` -------------------------------- ### Ohm Collection Declaration Variations Source: https://github.com/soveran/ohm/blob/master/README.md Shows different ways to declare a 'collection' in Ohm, including explicit index declaration and using the 'to_reference' helper. ```ruby # easiest, with the basic assumption that the index is `:post_id` collection :comments, :Comment # we can explicitly declare this as follows too: collection :comments, :Comment, :post # finally, we can use the default argument for the third parameter which # is `to_reference`. collection :comments, :Comment, to_reference ``` -------------------------------- ### Ohm Model Attribute Types Source: https://github.com/soveran/ohm/blob/master/README.md Explains different types of attributes that can be used in Ohm models, including attribute, set, list, counter, reference, and collection. ```ruby attribute :name set :attendees, :Person list :items counter :votes reference :user collection :posts ``` -------------------------------- ### Ohm Unique Constraint Source: https://github.com/soveran/ohm/blob/master/README.md Demonstrates how to enforce uniqueness for an attribute in an Ohm model using the 'unique' macro, preventing duplicate entries. ```ruby class User < Ohm::Model attribute :email unique :email end u = User.create(email: "foo@bar.com") u == User.with(:email, "foo@bar.com") # => true # This will raise an error or prevent creation if email already exists: # User.create(email: "foo@bar.com") ``` -------------------------------- ### Ohm Persistence Strategy Source: https://github.com/soveran/ohm/blob/master/README.md Explains that attributes declared with `attribute` are persisted after calling `save`. Operations on `list`, `set`, and `counter` attributes are immediate after object creation. ```ruby if event.save event.comments.add(Comment.create(body: "Wonderful event!")) end ``` -------------------------------- ### Ohm Attribute Types Source: https://github.com/soveran/ohm/blob/master/README.md Lists and describes the different attribute types available in Ohm::Model for defining data structures within Redis. ```APIDOC Ohm::Model Attribute Types: - attribute: For simple key-value attributes. - set: For storing collections of unique elements. - list: For storing ordered collections of elements. - counter: For atomic integer increments and decrements. Meta Types: - reference: Establishes a one-to-one or many-to-one relationship with another model. - collection: Establishes a one-to-many or many-to-many relationship with another model. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.