GithubHelp home page GithubHelp logo

redistent's Introduction

Redistent

WORK IN PROGRESS: this gem is neither code complete, neither usable at the moment.

Build Status Dependency Status Code Climate Coverage Status

Light persistent layer for Ruby objects using Redis and a centralized persister object.

Installation

Add this line to your application's Gemfile:

gem 'redistent'

And then execute:

$ bundle

Or install it yourself as:

$ gem install redistent

Usage

require "redistent"
require "virtus"
require "scrivener"

module Models
  class Queue
    include Virtus
    include Scrivener::Validations
    attr_accessor :uid, :persisted_attributes
    attribute :name, String
    def validate
      assert_present :name
    end
  end

  class Task
    include Virtus
    include Scrivener::Validations
    attr_accessor :uid, :persisted_attributes
    attribute :title, String
    attribute :queue, Queue
    attribute :queued_at, DateTime
    def validate
      assert_present :title
    end
  end

  class Team
    include Virtus
    include Scrivener::Validations
    attr_accessor :uid, :persisted_attributes
    attribute :name, String
    def validate
      assert_present :name
    end
  end

  class Teammate
    include Virtus
    include Scrivener::Validations
    attr_accessor :uid, :persisted_attributes
    attribute :name, String
    attribute :team, Team
    def validate
      assert_present :name
      assert_present :team
    end
  end

  class Skill
    include Virtus
    include Scrivener::Validations
    attr_accessor :uid, :persisted_attributes
    attribute :level, Integer
    attribute :queue, Queue
    attribute :teammate, Teammate
    def validate
      %i[level queue teammate].each { |name| assert_present(name) }
      assert_numeric :level
    end
  end
end

class PersistentAccessor
  include Redistent::Accessor
  ModelNotValid = Class.new(StandardError)
  before_write { |model| raise ModelNotValid, model.errors unless model.valid? }
  namespace Models
  model :team do
    collection :teammates, model: :teammate, index: :team
  end
  model :teammate do
    index :team
    collection :skills, model: :skill, index: :teammate
    collection :queues, model: :queue, using: :skill, index: [:teammate, :queue]
  end
  model :task do
    index :queue do
      store :tasks_waiting, sorted_by: :queued_at, if: ->(task) { task.status == "queued" }
      store :tasks_offered,                        if: ->(task) { task.status == "offered" }
    end
  end
  model :queue, class: TaskQueue do
    sorted_collection :tasks_waiting, model: :task, index: {:queue => :tasks_waiting}
    collection :tasks_offered,        model: :task, index: {:queue => :tasks_offered}
    collection :skills, model: :skill, index: :queue
    collection :teammates, model: :teammate, using: :skill, index: [:queue, :teammate]
  end
  model :skill do
    index :queue, inline_reference: true
    index :teammate, inline_reference: true
  end
end

accessor = PersistentAccessor.new(url: "redis://127.0.0.1:6379/7")

# write/read/erase
bug_queue = Queue.new(name: "fix bugs")
accessor.write(bug_queue)
feature_queue = accessor.read(:queue, "id123")
accessor.erase(bug_queue)

# collection of referrers
skill_collection = accessor.collection(feature_queue, :skills)
num_skills = skill_collection.count
all_skills = skill_collection.all

# collection of indirect referrers
all_teammates = accessor.collection(feature_queue, :teammates).all

# collection of sorted referrers
accessor.write(Task.new(title: "generate csv report", queue: feature_queue))
task_collection = accessor.collection(feature_queue, :tasks)
next_task_uid = task_collection.first_uid

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

redistent's People

Contributors

mathieul avatar

Watchers

 avatar James Cloos avatar

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.