Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

assignbot

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

assignbot

  • 0.1.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

Assignbot

In Ruby, assigning variables can be tricky. Could you assign from new? Could you always guarantee that?

Assignbot guarantee you can assign from .assign, code changes to your codebase is not required unless you want custom behaviour.

Codeship Status for saveav/assignbot

Use cases

  1. Assign from the hash, from params
  2. Assign from the hash, from JSON

Installation

To install Assignbot, add this line to your Gemfile:

gem 'assignbot'

And then execute:

$ bundle

Or install it system-wide by issuing:

$ gem install assignbot

Usage

Assume you have the following User class:

class User
  attr_accessor :name, :age, :id
end

Basic usage of assigner does not require significant code change to your model/ruby class, other than a one-liner include statement:

class User
  include Assignbot

  # your other ruby code
end

By doing so, you already make User to be assignable from a Hash:

user_params = {
  name: "Adam Pahlevi",
  age: 23,
  id: "934-2311"
}

user = User.new
user.assign(user_params)
user.name == "Adam Pahlevi" # returns true

If you would like to explicitly tell Assignbot what are the assignable variables, then you have to define the variables explicitly:

class User
  include Assignbot

  assigner do
    name from: :name
    age from: :age
    id from: :id
  end
  # your other ruby code
end

Explicitly defining assigner also allow you to map the value from source variable, which have different nomenclature, to the target variable in your Ruby class.

License

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

FAQs

Package last updated on 18 Oct 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc