https://github.com/tbuehlmann/dry-struct-setters
Setter methods for Dry::Struct subclasses
https://github.com/tbuehlmann/dry-struct-setters
dry-rb ruby
Last synced: 12 months ago
JSON representation
Setter methods for Dry::Struct subclasses
- Host: GitHub
- URL: https://github.com/tbuehlmann/dry-struct-setters
- Owner: tbuehlmann
- Created: 2017-06-10T11:34:54.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2021-09-07T16:09:23.000Z (over 4 years ago)
- Last Synced: 2025-05-07T03:46:34.572Z (12 months ago)
- Topics: dry-rb, ruby
- Language: Ruby
- Homepage:
- Size: 19.5 KB
- Stars: 8
- Watchers: 1
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Dry::Struct::Setters
[](http://badge.fury.io/rb/dry-struct-setters) [](https://travis-ci.org/tbuehlmann/dry-struct-setters)
Dry::Struct::Setters is an extension to the [Dry::Struct](https://github.com/dry-rb/dry-struct) library. Subclasses of Dry::Struct only define getter methods for its attributes by design (or preference). This library extends subclasses so they also provide setter methods.
## Usage
By including the Dry::Struct::Setters module into your Dry::Struct subclass every defined attribute will get a corresponding setter method:
```ruby
module Types
include Dry::Types.module
end
class Project < Dry::Struct
include Dry::Struct::Setters
attribute :name, Types::String
end
project = Project.new(name: 'some-project')
project.name = 'some-project-new'
project.name # => 'some-project-new'
```
### Mass Assignment
Including the Dry::Struct::Setters module into you Dry::Struct subclass will only define setter methods, mass assigning attributes won't work with this. If you want to be able to mass assign attributes, just include the Dry::Struct::Setters::MassAssignment module, which will provide an assign_attributes method:
```ruby
module Types
include Dry::Types.module
end
class Project < Dry::Struct
include Dry::Struct::Setters
include Dry::Struct::Setters::MassAssignment
attribute :name, Types::String
attribute :description, Types::String
end
project = Project.new(name: 'some-project', description: 'some-description')
project.assign_attributes(name: 'some-project-new', description: 'some-description-new')
project.name # => 'some-project-new'
project.description # => 'some-description-new'
```
### Convenience Class
Instead of including modules you can also just use the Dry::Struct::WithSetters class which inherits from Dry::Struct and includes both modules. Note that you'll need to explicitly require it:
```ruby
require 'dry/struct/with_setters'
module Types
include Dry::Types.module
end
class Project < Dry::Struct::WithSetters
attribute :name, Types::String
end
```
Hint: You can also require directly via your Gemfile:
```ruby
gem 'dry-struct-setters', require: 'dry/struct/with_setters'
```
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'dry-struct-setters'
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install dry-struct-setters
## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/tbuehlmann/dry-struct-setters.