Code Monkey home page Code Monkey logo

ciesta's Introduction

Ciesta

Build Status Coverage Status Gem Version

Create simple form objects

Supported Ruby 2.3+

You should keep it in mind that here uses dry-validation and dry-types for validation and typification respectively.

Installation

Add this line to your application's Gemfile:

gem "ciesta"

And then execute:

$ bundle

Or install it yourself as:

$ gem install ciesta

Usage

Basic case

For example will be used a hash with name and age attributes:

user = Hash[name: nil, age: nil]

For setting and syncing new values let's create a form object:

class Form
  include Ciesta

  field :name
  field :age

  def age
    super.to_i
  end
end

form = Form.new(user)
form.name = "John"
form.age = "33"

...

form.name # => "John"
form.age # => 33

Validation

For validating incoming values you can use validate method:

class Form
  include Ciesta

  field :name
  field :age

  validate do
    required(:name).filled
    required(:age).filled(gt?: 18)
  end
end

form = Form.new(user)

An attempt to sync with invalid form will raise Ciesta::FormNotValid error.

form.age = 15
form.valid? # => false
form.errors # => { age: ["must be greater than 18"] }
...
form.age = 42
form.valid?  # => true

Advanced field declaration

Types

You can define the type of a field using Ciesta::Types namespace.

field :age, type: Ciesta::Types::Coercible::Int
...
form.age = "42"
form.age # => 42

Default type is Ciesta::Types::Any.

Default value

If your attribute wasn’t set yet, but value is already in use, one can set a default option to avoid exceptions.

field :age, default: 42
...
form.age # => 42

Default value can also be a Proc, wich will be called in the object context.

class User
  def default_age
    42
  end
end
field :age, default: -> { default_age }
...
form.age # => 42

Values mass update

There are two methods for form fields mass update: assign and assign!.

form.assign!(name: "Neo", age: 30)
...
user.name # => "Neo"
user.age  # => 30

assign! method will raise Ciesta::FieldNotDefined error if one of the passed attributes is not declared in the form.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/nulldef/ciesta.

License

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

ciesta's People

Contributors

akxcv avatar nulldef avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

akxcv

ciesta's Issues

Update dependencies

Hi! It would be nice to update ciesta's dependencies, especially dry stuff.

Side note: maybe think about some automated way to update dependencies?

Set up Travis

It would be better for everyone if the tests were running on every PR.

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.