{"id":18087897,"url":"https://github.com/soveran/nest","last_synced_at":"2025-04-04T02:08:50.978Z","repository":{"id":861417,"uuid":"597127","full_name":"soveran/nest","owner":"soveran","description":"Generate nested namespaced keys for key-value databases.","archived":false,"fork":false,"pushed_at":"2019-11-06T16:20:54.000Z","size":40,"stargazers_count":184,"open_issues_count":1,"forks_count":25,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-28T01:13:25.382Z","etag":null,"topics":["lesscode","redis","ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/soveran.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2010-04-06T14:16:10.000Z","updated_at":"2025-02-08T12:41:47.000Z","dependencies_parsed_at":"2022-08-16T11:15:09.246Z","dependency_job_id":null,"html_url":"https://github.com/soveran/nest","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soveran%2Fnest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soveran%2Fnest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soveran%2Fnest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soveran%2Fnest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/soveran","download_url":"https://codeload.github.com/soveran/nest/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247107829,"owners_count":20884797,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["lesscode","redis","ruby"],"created_at":"2024-10-31T17:09:24.300Z","updated_at":"2025-04-04T02:08:50.964Z","avatar_url":"https://github.com/soveran.png","language":"Ruby","readme":"Nest\n====\n\nObject Oriented Keys for Redis.\n\nDescription\n-----------\n\nIf you are familiar with databases like [Redis](http://redis.io)\nand libraries like [Ohm](http://ohm.keyvalue.org) you already know how\nimportant it is to craft the keys that will hold the data.\n\n```ruby\n\u003e\u003e redis = Redic.new\n\u003e\u003e redis.call(\"HSET\", \"Event:3\", \"name\", \"Redis Meetup\")\n\u003e\u003e redis.call(\"HGET\", \"Event:3\", \"name\")\n=\u003e [\"Redis Meetup\"]\n```\n\nIt is a design pattern in key-value databases to use the key to simulate\nstructure, and you can read more about this in the [case study for a\nTwitter clone](http://redis.io/topics/twitter-clone).\n\nNest helps you generate those keys by providing chainable namespaces that are\nalready connected to Redis:\n\n```ruby\n\u003e\u003e event = Nest.new(\"Event\")\n\u003e\u003e event[3].call(\"HSET\", \"name\", \"Redis Meetup\")\n\u003e\u003e event[3].call(\"HGET\", \"name\")\n=\u003e [\"Redis Meetup\"]\n```\n\nAlternatively, you can send the Redis commands as messages to Nest,\nand if the method definition is missing it will forward the command\nto Redis:\n\n```ruby\n\u003e\u003e event = Nest.new(\"Event\")\n\u003e\u003e event[3].hset(\"name\", \"Redis Meetup\")\n\u003e\u003e event[3].hget(\"name\")\n=\u003e [\"Redis Meetup\"]\n```\n\nUsage\n-----\n\nTo create a new namespace:\n\n```ruby\n\u003e\u003e ns = Nest.new(\"foo\")\n=\u003e \"foo\"\n\n\u003e\u003e ns[\"bar\"]\n=\u003e \"foo:bar\"\n\n\u003e\u003e ns[\"bar\"][\"baz\"][\"qux\"]\n=\u003e \"foo:bar:baz:qux\"\n```\n\nAnd you can use any object as a key, not only strings:\n\n```ruby\n\u003e\u003e ns[:bar][42]\n=\u003e \"foo:bar:42\"\n```\n\nIn a more realistic tone, lets assume you are working with Redis and\ndealing with events:\n\n```ruby\n\u003e\u003e event = Nest.new(\"Event\")\n=\u003e \"Event\"\n\n\u003e\u003e id = event[:id].incr\n=\u003e 1\n\n\u003e\u003e event[id].hset(\"name\", \"Redis Meetup\")\n=\u003e 1\n\n\u003e\u003e meetup = event[id]\n=\u003e \"Event:1\"\n\n\u003e\u003e meetup.hget(\"name\")\n=\u003e [\"Redis Meetup\"]\n```\n\nAPI\n---\n\n`call`: Receives a Redis command and its arguments, and returns the\nreply from the Redis server. If the reply from Redis is an error,\nan instance of `RuntimeError` is returned.\n\n`call!`: Similar to `call`, but instead of returning\nan instance of `RuntimeError` when the command fails, the error is\nraised.\n\n`queue`: Receives the same kind of arguments as `call`, but enqueues\nthe command in a transaction.\n\n`commit`: Commits the transaction and returns the reply from Redis.\n\nAny call to a missing method will result in `Nest` converting the\nmethod name to a Redis command and applying the arguments in an\ninvocation to `call`.\n\nFor example:\n\n```ruby\nns = Nest.new(\"foo\")\nns.append(\"hello,\")\nns.append(\" world\")\nns.get\n```\n\nIs equivalent to:\n\n```ruby\nns = Nest.new(\"foo\")\nns.call(\"APPEND\", \"hello,\")\nns.call(\"APPEND\", \" world\")\nns.call(\"GET\")\n```\n\nSupplying your existing Redis instance\n--------------------------------------\n\nYou can supply a [Redic](https://github.com/amakawa/redic) instance as\na second parameter. If you don't, a default instance is created for you:\n\n```ruby\n\u003e\u003e redis = Redic.new(\"redis://localhost:6379\")\n=\u003e #\u003cRedic:0x007fa640845f10 ...\u003e\n\n\u003e\u003e event = Nest.new(\"Event\", redis)\n=\u003e \"Event\"\n\n\u003e\u003e event[:id].call(\"TYPE\")\n=\u003e \"string\"\n```\n\n`Nest` objects respond to `redis` and return a `Redic` instance. It is\nautomatically reused when you create a new namespace, and you can reuse it when\ncreating a new instance of Nest:\n\n```ruby\n\u003e\u003e event = Nest.new(\"Event\", meetup.redis)\n=\u003e \"Event\"\n```\n\nNest allows you to execute all the Redis commands that expect a key as the\nfirst parameter. If you use any other command, the result can be unexpected.\n\nDifferences with redis-namespace\n--------------------------------\n\n[redis-namespace](http://github.com/defunkt/redis-namespace) wraps Redis\nand translates the keys back and forth transparently.\n\nUse redis-namespace when you want all your application keys to live in a\ndifferent scope.\n\nUse Nest when you want to use the keys to represent structure.\n\nTip: instead of using redis-namespace, it is recommended that you run a\ndifferent instance of `redis-server`. Translating keys back and forth is not\nonly delicate, but unnecessary and counterproductive.\n\nDifferences with Ohm\n--------------------\n\n[Ohm](http://ohm.keyvalue.org) lets you map Ruby objects to Redis with\nlittle effort. It not only alleviates you from the pain of generating\nkeys for each object, but also helps you when dealing with references\nbetween objects.\n\nUse Ohm when you want to use Redis as your database.\n\nUse Nest when mapping objects with Ohm is not possible or overkill.\n\nTip: Ohm uses Nest internally to deal with keys. Having a good knowledge\nof Nest will let you extend Ohm to suit your needs.\n\nInstallation\n------------\n\n```\n$ gem install nest\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoveran%2Fnest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsoveran%2Fnest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoveran%2Fnest/lists"}