UnrealReaper

A gem help you collect and package your code into a class, Let's use them easily.

Features

Usage some top methods, the method should know current model

User.ur.my_report.state # It will return some state of the User model
Topic.ur.my_report.state # It will return some state of the Topic model

Call some methods of a/many record(s)

user = User.new # <User#xxx id: nil, access_token: nil>
user.ur.token.generate(:access_token) # the generate will know the user record, and write a token to user.access_token

book = Book.first
user.ur.payment.buy(book) # I can put the book to user library and build a bill for user.

Make a easy way to change record pass form

user = User.first
# slim: input type=text name=book[payment][book_id] value=book_id
user.update(ur: {payment: {book_id: book_id}} # It will do the same feature as user.ur.payment.buy(book)

Installation

Add this line to your application's Gemfile:

gem 'unreal_reaper'

And then execute:

$ bundle

Or install it yourself as:

$ gem install unreal_reaper

Usage

Getting Started

New a package

class Publisher
  class Top
    def initialize(model)
      model.class_eval do
        scope :published, -> { where("published_at >= ?", Time.now) }
      end
    end

    def state
      puts {published: model.published.count, total: model.count}
    end
  end

  def publish!(time = Time.now)
    update(published_at: Time.now)
  end

  def published_at=(time_str)
    publish!(Time.zone.parse(time_str))
  end

  UnrealReaper.load(Publisher)
end

Install it into a class

class User
  include UnrealReaper

  ur_package :publisher
end

Usage the package

User.published # defined in initialize method
User.ur.publisher.state
u = User.create
u.ur.publisher.publish!
u.update(ur: {publisher: {published_at: "2018-03-17"}})

Package load

  • top_cls: We can call its methods by User.ur.{package_name}.{method}, its initialize will call when call ur_package package_name
  • helper_cls: We can call its methods by User.new.ur.{package_name}.{method}
  • writer_cls: We can call its methods by User.new.ur = {package_name => {attr_method => 'value'}} or User.new.ur.{package_name} = {attr_method => 'value'}
# Basic load
UnrealReaper.load(:publisher, top_cls: Publisher::Top, helper_cls: Publisher::Helper, attr_cls: Publisher::Writer)

# Auto found classes
UnrealReaper.load(Publisher)
# equal UnrealReaper.load(Publisher.name.underscore, top_cls: Publisher::Top, helper_cls: Publisher, attr_cls: Publisher)
# top_cls: if not found Publisher::Top, it will be nil
# helper_cls: if not found Publisher::Helper, it will be Publisher
# attr_cls: if not found Publisher::Writer, it will be Publisher

# Other
UnrealReaper.load(:other_name, top_cls: Publisher::Top) # it has no helper & writer classes 
UnrealReaper.load(Publisher, top_cls: Publisher) # the top class is itself, and automaticlly find helper & writer classes

Package initalize and install

class Token
  class Top
    def initialize(model, opts = {length: 32})
      # The model and opts will be store as model & options
      # you also can get them in the arguments
    end
  end

  UnrealReaper.load(self)
end
class User
  ur_package :token, length: 62 # the second argument is the opts of initialize
end

Object Structure

Manager, instance_manager, package_objs all is lazy-load objects.

   Class   =>   .manager   =>   .top_objs(package1, package2, package3)
     ||
     \/
  instance(Class) =>  .instance_manager  => .helper_objs(package1, package2, package3)
                                        ||
                                        \/
                                        .writer_objs=(package1, package2, package3)

Development

  • bin/console run a console
  • rspec run a testing

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/xiejiangzhi/unreal_reaper.

License

The gem is available as open source under the terms of the MIT License.