Giter Site home page Giter Site logo

rndb's Introduction

Ruby Style Guide test Coverage Status Gem Version Western Australia

RnDB

RnDB is a procedurally-generated fake database. Read the blog post for details.

Video Overview

IMAGE ALT TEXT HERE

Usage

First, create tables with columns that may have a pre-determined distribution of values (which can be queried on), or which may have a lambda for generating a random value, or both (such as for the weight column below).

class Widget < RnDB::Table
  column :colour, { red: 0.3, green: 0.12, brown: 0.01, blue: 0.5, orange: 0.07 }
  column :weight, { light: 0.3, medium: 0.64, heavy: 0.06 }, -> value do
    range =
      case value
      when :light
        (0.0..5.0)
      when :medium
        (6.0..9.0)
      when :heavy
        (10.0..20.0)
      end
    self.rand(range)
  end
  column :name, -> { Faker::Games::Pokemon.name }
end

Next, create a database with an optional random seed (137 in the example below), and add the table to the database, specifying the number of records to simulate (in this case, one trillion).

DB = RnDB::Database.new(137)
DB.add_table(Widget, 1e12)

Finally, fetch some records!

puts Widget.count
puts Widget[1234567890].name
puts Widget.find { |widget| (3.1415..3.1416).include?(widget.weight) }.attributes

Which will display the following:

1000000000000
Charmander
{:id=>61520, :weight=>3.1415121332762386, :colour=>:red, :name=>"Exeggcute"}

Note that the find command tested over sixty thousand records in just a second or two without needing to generate all attributes of each record first. But an even faster way of honing in on a particular record is to run a query, such as:

query = Widget.where(colour: [:brown, :orange], :weight => :heavy)

You can then retrieve random records that match the query with sample, use pluck to retrieve specific attributes without generating all of them, and use find or filter to further refine your search, like this:

puts query.count
puts query.sample.pluck(:colour, :weight)
puts query.lazy.filter { |ball| ball.name == 'Pikachu' }.map(&:id).take(10).to_a

Which will display the following:

4800000000
{:colour=>:orange, :weight=>16.096085279047017}
[429400000068, 429400000087, 429400000875, 429400000885, 429400000914, 429400001036, 429400001062, 429400001330, 429400001341, 429400001438]

Note that we used the lazy enumerator when filtering records to prevent running the block on all records before performing the map and taking the first ten results.

Release Process

  1. rake standard:fix
  2. rake version:bump:whatever
  3. rake gemspec:release BRANCH=main
  4. rake git:release BRANCH=main
  5. Create new release on GitHub to trigger ship workflow

Copyright

Copyright (c) 2021 Jason Hutchens. See LICENSE for further details.

rndb's People

Contributors

kranzky avatar

Stargazers

David Hall avatar Jonathan Sandusky avatar Leroy Hopson avatar Matt Sylvia avatar Hanna avatar  avatar Thomas Nicollet avatar  avatar  avatar Jeremy Roush avatar

Watchers

 avatar James Cloos avatar  avatar

rndb's Issues

`require': cannot load such file -- byebug

Hey there,

My name is Maciej Mensfeld and I run security & quality assessment for Ruby libraries using Diffend.io.

When requiring this gem, it fails:

lib/rndb/table.rb:4:in `require': cannot load such file -- byebug (LoadError)

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.