Socket
Book a DemoInstallSign in
Socket

redis-carpenter

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

redis-carpenter

0.0.1
bundlerRubygems
Version published
Maintainers
1
Created
Source

Usage

Include Carpenter::Model to your class and define any params and fields you need.

class Example
  include Carpenter::Model

  param :id

  field :first, :integer
  field :second
end

After this, you can get and set values in redis via the model.

Example[1].first # => nil
Example[1].first = 10
Example[1].first # => 10
Example[2].first # => nil

field can be one of:

  • string
  • integer
  • float
  • array
  • json
  • redis_list

When defining field you can pass some options:

  • default (default: nil)
    Use this to define a default value when getting if such a key does not exist in redis.
class Example
  include Carpenter::Model

  param :id

  field :first, :integer, default: 100
  field :second
end

Example[1].first # => 100
Example[1].first = 10
Example[1].first # => 10
Example[2].first # => 100

  • store (default: false)
    Use this to toggle automatic updating of stored value ​​when getting.
class Example
  include Carpenter::Model

  param :id

  field :first, :integer, store: true
  field :second
end

ex = Example[1]
ex.first # => nil
ex.first = 10
ex.first # => 10
ex.first = 20
ex.first # => 10
ex.first.reload
ex.first # => 20

Processing pipeline

Step 1. Class definition

  • Collect all params
  • Define class key (use defined params)
  • Collect all fields (use class key)

Class definition example:

class Example
  include Carpenter::Model

  param :id
  param :id2

  field :first
  field :second
end

After definition, we have:

  • Params list of [:id, :id2]
  • Key Example with params stab like Example-{id}-{id2}
  • Fields list of [:first, :second]. They contain class key and look like Example-{id}-{id2}:first

Step 2. Object initializing

  • Save given params
  • Define object key (use class key and apply saved params)
  • Transfer class fields to object (update present fields key to object key)

For past example:

Example[id: 1, id2: 5]

After initializing, we have:

  • Saved Params: { id: 1, id2: 5 }
  • Object Key with saved params: Example-1-5
  • Cloned Fields list ([:first, :second]) with updated keys: Example-1-5:first

We can initialize Model with empty params:

Example[id2: 10] #=> This creates key like "Example-@-10"

*Any empty params will be replaced to @

We also can initialize Model with positional arguments:

Example[1, 5] #=> "Example-1-5"
# OR
Example[nil, 10] #=> "Example-@-10"
# OR
Example[8] #=> "Example-8-@"

In this case, params will be filled in order at definition

Type redis_list examples


  class Example
    include Carpenter::Model

    param :id

    field :redis_list, :redis_list
  end

  Example[1].redis_list #=> []
  Example[1].redis_list.get #=> []

  Example[1].redis_list = [1, 2, 3]
  Example[1].redis_list.get #=> [1, 2, 3]
  Example[1].redis_list[1] #=> 2
  Example[1].redis_list[1..2] #=> [2, 3]
  Example[1].redis_list[1...2] #=> [2]

  Example[1].redis_list[1] = 10
  Example[1].redis_list.get #=> [1, 10, 3]

  Example[1].redis_list << 200
  Example[1].redis_list.get #=> [1, 10, 3, 200]

  Example[1].redis_list << { email: 'example@mail.ru' }
  Example[1].redis_list.get #=> [1, 10, 3, 200, { email: 'example@mail.ru' }]

FAQs

Package last updated on 23 Apr 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.