Project

zyra

0.0
No release in over a year
Gem for seeding data in the database
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 0.1.1
 Project Readme

Zyra

Code Climate Test Coverage Issue Count Gem Version Codacy Badge Inline docs

zyra

Zyra is intented to easy the seeding stage of projects by ensuring an entity exists without having to reinsert it every time in the database

The process is done by registering a model class, then performing a creation in case of missing the entry

Current Release: 1.2.0

Next release

Yard Documentation

https://www.rubydoc.info/gems/zyra/1.2.0

Installation

  • Install it
  gem install zyra
  • Or add Zyra to your Gemfile and bundle install:
  gem 'zyra'
  bundle install zyra

Usage

The usage is done by registering a model, adding hooks and calling find_or_create and passing a block to be executed after

  Zyra
    .register(User, find_by: :email)
    .on(:build) do |user|
      user.reference = SecureRandom.hex(16)
    end

  attributes = {
    email: 'usr@srv.com',
    name: 'Some User',
    password: 'pass'
  }

  user = Zyra.find_or_create(:user, attributes) do |usr|
    usr.update(attributes)
  end

  # returns an instance of User that is persisted in the database
  # user.email is the key as 'usr@srv.com'
  # user.name will always be updated to 'Some User'
  # user.password will always be updated to 'pass'
  # user.reference will be generated in the first time, and never again regenerated

Hooks

hooks can be registered when registering a model or after to be executed in 4 points, found, build, create and return

  Zyra
    .register(User, find_by: :email)
    .on(:build) do |user|
      user.posts.build(name: 'first', content: 'some content')
    end

  Zyra.on(:user, :return) do |user|
    user.update(reference: SecureRandom.hex(16))
  end

  attributes = {
    email: 'usr@srv.com',
    name: 'Some User',
    password: 'pass'
  }

  user = Zyra.find_or_create(:user, attributes).reload

  # Returns a User with email 'usr@srv.com'
  # Creates a post for the user, only in the first time
  # Regenerates the reference every time the code is ran