Giter Site home page Giter Site logo

ika's Introduction

Ika

Gem Version Circle CI Coverage Status

Ika implements the function that export/import ActiveModel data with json. Ika also supports carrierwave.

Installation

In Rails, add it to your Gemfile:

gem 'ika'

Usage

You can use export or import method on your model or relation.

Example

In case: Group has many tags and User belongs to multiple groups with GroupUsers such as below.

class User < ActiveRecord::Base
  has_many :group_users
  has_many :groups, through: :group_users
end

class GroupUsers < ActiveRecord::Base
  belongs_to :user
  belongs_to :group
end

class Group < ActiveRecord::Base
  has_many :group_users
  has_many :users, through: :group_users
  has_many :tags
end

class Tag < ActiveRecord::Base
  belongs_to :group
end

Now you can export with export method on your model or relation and import with import method on your model.

require 'json'
# with no options

JSON.parse User.export
# => [{"id":1,"name":"iruca3"},{"id":2,"name":"inkling"}]
JSON.parse User.where(id: 1).export
# => [{"id":1,"name":"iruca3"}]
JSON.parse User.find(id: 2).export
# => {"id":2,"name":"inkling"}

# with include option
JSON.parse User.export(include: :groups)
# => [{"id":1,"name":"iruca3","groups":[{"id":1,"name":"aqutras"},{"id":2,"name":"Splatoon"}]},{"id":2,"name":"inkling","groups":[{"id":2,"name":"Splatoon"}]}]
data = JSON.parse(User.find(id: 1).export(include: [{groups: [:tags]}]))
# => {"id":1,"name":"iruca3","groups":[{"id":1,"name":"aqutras","tags":[{"id":1,"name":"Company"}]},{"id":2,"name":"Splatoon","tags":[{"id":2,"name":"Game"},{"id":3,"name":"Inkling"}]}]}

# import (id, created_at and updated_at are completely imported with the same value)
User.destroy_all
Group.destroy_all
Tag.destroy_all
User.import(data)

# sync mode is available.
User.import(User.where(id: 1).export, sync: true)
User.exist?(id: 2)
# => false

Others

  • DO NOT USE sync mode if you are using include option.
  • If the same id exists, Ika uses UPDATE.
  • Uploaded files by carrierwave will be checked their md5 hash and do nothing if they exist and md5 is matched.
  • If already exists import or export methods, you can use ika_import or ika_export methods.

License

Copyright (c) 2015 Aqutras This project rocks and uses MIT-LICENSE.

ika's People

Contributors

8398a7 avatar iguto avatar iruca3 avatar

Watchers

 avatar  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.