<commit_before># Maybe So

Ever get annoyed that ActiveRecord blows up if you set a boolean column to nil? Have you ever wanted to allow custom "truthy" values for booleans rather than just `true`, `false`, `0`, and `1`? Wish you could call `to_bool` on random objects and have it return something reasonable?

Maybe So handles this for you!

Tested on the following Rubies: MRI 1.9.3, 2.0.0, 2.1.x, JRuby (1.7.2 and newer).

[![Build Status](https://secure.travis-ci.org/ministrycentered/maybe_so.svg?branch=master)](http://travis-ci.org/ministrycentered/maybe_so)

## Installation

Add this line to your application's Gemfile:

    gem 'maybe_so'

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install maybe_so

## Usage

### ActiveRecord

Let's say you have an ActiveRecord attribute called `hidden` that you want to only be 0 or 1, never nil.

```ruby
add_column :products, :hidden, :boolean, default: false, null: false
```

Add the `boolean_attribute` class method on your class.

```ruby
class Product < ActiveRecord::Base
  boolean_attribute :hidden
end
```

By default, Maybe So accepts the following case insensitive values as `true`. Everything else is `false`. You can override these values with the `truthy_values` option (see below).

```ruby
true, "true", "Y", "Yes", 1, "1", "T"
```

### ActiveModel

```ruby
class Product
  include ActiveModel::Model

  boolean_attribute :hidden
end
```

### Configuration options

`boolean_attributes` accepts an options hash for the following:

- `truthy_values`: array of values (will be coerced into strings when checking) to be considered truthy
- `skip_validator`: this will not add the default `validates_inclusion_of` to ensure records are not valid unless they are set to `true` or `false`

Example custom configuration:

```ruby
class Product < ActiveRecord::Base
  boolean_attribute :hidden, truthy_values: ["Si"], skip_validator: true
end
```

## Copyright

Copyright (c) 2014 <NAME> and <NAME><commit_msg>Add docs for to_bool and clarify that we don't include the presence validator [ci skip]<commit_after># Maybe So

Ever get annoyed that ActiveRecord blows up if you set a boolean column to nil? Have you ever wanted to allow custom "truthy" values for booleans rather than just `true`, `false`, `0`, and `1`? Wish you could call `to_bool` on random objects and have it return something reasonable?

Maybe So handles this for you!

Tested on the following Rubies: MRI 1.9.3, 2.0.0, 2.1.x, JRuby (1.7.2 and newer).

[![Build Status](https://secure.travis-ci.org/ministrycentered/maybe_so.svg?branch=master)](http://travis-ci.org/ministrycentered/maybe_so)

## Installation

Add this line to your application's Gemfile:

    gem 'maybe_so'

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install maybe_so

## Usage

### ActiveRecord

Let's say you have an ActiveRecord attribute called `hidden` that you want to only be 0 or 1, never nil.

```ruby
add_column :products, :hidden, :boolean, default: false, null: false
```

Add the `boolean_attribute` class method on your class.

```ruby
class Product < ActiveRecord::Base
  boolean_attribute :hidden
end
```

By default, Maybe So accepts the following case insensitive values as `true`. Everything else is `false`. You can override these values with the `truthy_values` option (see below).

```ruby
true, "true", "Y", "Yes", 1, "1", "T"
```

### ActiveModel

```ruby
class Product
  include ActiveModel::Model

  boolean_attribute :hidden
end
```

### Configuration options

`boolean_attributes` accepts an options hash for the following:

- `truthy_values`: array of values (will be coerced into strings when checking) to be considered truthy
- `skip_validator`: this will _omit_ adding the `validates_inclusion_of` to ensure records are not valid unless they are set to `true` or `false`. Note it does _not_ validate presence. If you want that, you'll need to add a separate validation.

Example custom configuration:

```ruby
class Product < ActiveRecord::Base
  boolean_attribute :hidden, truthy_values: ["Si"], skip_validator: true
end
```

### #to_bool

`String`, `Fixnum`, `TrueClass`, `FalseClass`, `NilClass` all have `#to_bool` added.

```ruby
"Yes".to_bool # Uses the same truthy values as shown above
# => true

"zing".to_bool
# => false

"1".to_bool
# => true

1.to_bool
# => true

42.to_bool
# => false

true.to_bool
# => true

false.to_bool
# => false

nil.to_bool
# => false
```

## Copyright

Copyright (c) 2014 <NAME> and <NAME>
{'fraction_non_alphanumeric': 0.10889332446218673, 'fraction_numerical': 0.010201818585052118, 'mean_word_length': 4.630461922596754, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5945833', 'n_tokens_mistral': 1449, 'n_tokens_neox': 1362, 'n_words': 568}