No commit activity in last 3 years
No release in over 3 years
This gem adds the ability to add setter methods to otherwise setter-method-less Dry::Struct subclasses.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
>= 0
>= 0

Runtime

>= 1.4.0
 Project Readme

Dry::Struct::Setters

Gem Version Build Status

Dry::Struct::Setters is an extension to the 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:

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:

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:

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:

gem 'dry-struct-setters', require: 'dry/struct/with_setters'

Installation

Add this line to your application's Gemfile:

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.