Project

mongokit

0.0
No commit activity in last 3 years
No release in over 3 years
Helpers for mongoid i.e auto increment fields, search index
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.9
~> 10.0

Runtime

~> 4.0.0
 Project Readme

Mongokit

Mongoid helpers like export/import csv, autoincrement, find in batch, address helper, securetoken field, boolean methods

Installation

Add this line to your application's Gemfile:

gem 'mongokit', '0.0.1'

gem 'mongokit', github: 'jiren/mongokit' # master branch

And then execute:

$ bundle

Or install it yourself as:

$ gem install mongokit 

Usage

  1. Auto incrementing field with pattern and without patterns.

     auto_increment :order_no, pattern: '%Y%m#####’   # this can be use for invoice generation.
     auto_increment :rank
    
  2. secure_token to generate api keys, season keys etc.

    has_secure_token :auth_token, token_length: 20  # token length is optional
    

    Output:

    user.auth_token # => “77TMHrHJFvFDwodq8w7Ev2m7"
    
  3. find_in_batch

    Mongodb using cursor to iterate collections if collection is too large then it is giving cursor timeout error. In this case this is helpful.

    Order.in_batches(batch_size: 100, start: 50)  do |orders|
      ....
    end
    
    # with criteria
    Order.where(:created_at.gt => Time.now.yesterday).in_batches do |orders|
       ...
     end
  4. Csv transformer. Import / Export csv by defining mapping like this in models.

  • Import

    csv_import_mapping :address, [:name, :zip_code], headers: true do |row, attrs|
      attrs[:zip_code] = attrs[:zip_code].to_i  # Processing block
    end

    Use:

     Address.from_address_csv('address.csv')
    
    • Export
    csv_export_mapping :address, [:zip_code, :name, :region] do |row, record|
      row[:zip_code] = "IN-#{row[:zip_code]}"
    end

    Use:

     Address.to_address_csv('address.csv’)
    
  1. Model helpers: mult fields definition, field with validations and boolean methods.

    • Define multiple fields using single line.

      i.e

    multi_fields :city, { venue: 'Mumbai' }, :country
    multi_fields :start_time, :end_time, DateTime
    
    • Define field with validations.

      i.e

    field_with_validation(:team_type, inclusion: { in: %w(t20 odi test) })
    
    • Boolean methods
    field :format, type: String # defined field
    
    boolean_methods(:match_type, %w(t20 odi test), { postfix: 'match' } )
    

    Defined methods

    record.t20_match?   
    record.odi_match?
    

    Internal methods declaration for match_type field

    def t20_match?
       match_type == “t20"
    end

Check examples for more detail

Development

After checking out the repo, run bin/setup to install dependencies. Then, 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 to create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

  1. Fork it ( https://github.com/[my-github-username]/mongokit/fork )
  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 a new Pull Request