Skip to content

nulldef/ciesta

Repository files navigation

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.