Code Monkey home page Code Monkey logo

cacchern's Introduction

Cacchern

operate Redis like ActiveRecord interface

Installation

Add this line to your application's Gemfile:

gem 'cacchern'

And then execute:

$ bundle

Or install it yourself as:

$ gem install cacchern

Usage

  1. Create classes

Create class that overrides Cacchern::SortableMember

class Score < Cacchern::SortableMember
  validates :key, presence: true, numericality: { only_integer: true }
  validates :value, presence: true, numericality: true
end

Create class that overrides Cacchern::SortableMember

class ScoreSet < Cacchern::SortedSet
  contain_class Score
end
  1. Add value
base_set = ScoreSet.new('base')
#=> #<ScoreSet:0x000056544559a520 @key="score_set:base">
score = Score.new(1, 100)
#=> #<Score:0x00005654456194b0
# @errors=#<ActiveModel::Errors:0x0000565445619050 @base=#<Score:0x00005654456194b0 ...>, @details={}, @messages={}>,
# @key=1,
# @validation_context=nil,
# @value=100>
base_set.add(score)
base_set.add(Score.new(2,50))

base_set.add_all([Score.new(3,50),Score.new(4,40)])
  1. Get Values
base_set.order(:desc)
#=> [#<Score:0x000056544570ebb8
#  @errors=#<ActiveModel::Errors:0x000056544570ea28 @base=#<Score:0x000056544570ebb8 ...>, @details={}, @messages={}>,
#  @key="1",
#  @validation_context=nil,
#  @value=100.0>,
# #<Score:0x000056544570e230
#  @errors=#<ActiveModel::Errors:0x000056544570e0a0 @base=#<Score:0x000056544570e230 ...>, @details={}, @messages={}>,
#  @key="2",
#  @validation_context=nil,
#  @value=50.0>]
base_set.order(:asc)
#=> [#<Score:0x000056544579aaa0
#  @errors=#<ActiveModel::Errors:0x000056544579a910 @base=#<Score:0x000056544579aaa0 ...>, @details={}, @messages={}>,
#  @key="2",
#  @validation_context=nil,
#  @value=50.0>,
# #<Score:0x000056544579a168
#  @errors=#<ActiveModel::Errors:0x0000565445799fd8 @base=#<Score:0x000056544579a168 ...>, @details={}, @messages={}>,
#  @key="1",
#  @validation_context=nil,
#  @value=100.0>]
  1. Remove value
base_set.remove(2)
#=> true
base_set.order(:asc)
#=> [#<Score:0x0000565445850990
#  @errors=#<ActiveModel::Errors:0x0000565445850800 @base=#<Score:0x0000565445850990 ...>, @details={}, @messages={}>,
#  @key="1",
#  @validation_context=nil,
#  @value=100.0>]
base_set.remove_all 
  • IncrementableSortedSet
class IScoreSet < Cacchern::IncrementableSortedSet
  contain_class Score
end
base_set = IScoreSet.new('base')
base_set.add(Score.new(2,50))
base_set.increment(Score.new(2,50))

Development

After checking out the repo, run bin/setup to install dependencies. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/shuyuhey/cacchern.

License

The gem is available as open source under the terms of the MIT License.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.