Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/derrickreimer/mass_assignable
Add Rails-like mass assignment to any Ruby object
https://github.com/derrickreimer/mass_assignable
Last synced: 5 days ago
JSON representation
Add Rails-like mass assignment to any Ruby object
- Host: GitHub
- URL: https://github.com/derrickreimer/mass_assignable
- Owner: derrickreimer
- License: mit
- Created: 2012-12-09T07:10:57.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2012-12-09T18:23:35.000Z (about 12 years ago)
- Last Synced: 2024-11-30T09:39:08.466Z (22 days ago)
- Language: Ruby
- Homepage:
- Size: 133 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# MassAssignable
MassAssignable is a simple gem that adds Rails-like mass-assignment behavior
to ordinary Ruby objects. This gem has no external dependencies and works
everywhere.## Installation
Add this line to your application's Gemfile:
gem 'mass_assignable'
And then execute:
$ bundle
Or install it yourself as:
$ gem install mass_assignable
## Usage
Simply include `MassAssignable` in your class and specify mass assignable
attributes using `attr_mass_assignable`. Any attributes not specified
will not be mass assignable.```ruby
require 'mass_assignable' # not needed with Railsclass Person
include MassAssignable
attr_accessor :name, :age, :height
attr_mass_assignable :name, :age
end
```Then, mass assignment is as easy as calling `attributes=`.
```ruby
person = Person.new
person.attributes = { :name => "Derrick", :age => 24, :height => 77 }person.name
# => "Derrick"person.age
# => 24person.height
# => nil
```Notice that `#height` is nil, because we didn't include it in our call to
`attr_mass_assignable`.If you want an error to be raised when invalid mass assignment is attempted,
simply use `attr_mass_assignable!`.```ruby
class ParanoidPerson
include MassAssignable
attr_accessor :name, :age, :height
attr_mass_assignable! :name, :age
endperson = ParanoidPerson.new
person.attributes = { :height => 77 }
# => Raises a RuntimeError
```## Contributing
1. Fork it
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 new Pull Request