Git Product home page Git Product logo

dry-struct-setters's Introduction

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.

dry-struct-setters's People

Contributors

tbuehlmann avatar tomharvey avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.