GithubHelp home page GithubHelp logo

prodis / wannabe_bool Goto Github PK

View Code? Open in Web Editor NEW
160.0 6.0 14.0 66 KB

If string, numeric, symbol and nil values wanna be a boolean value, they can with the new #to_b method (and more).

License: MIT License

Ruby 100.00%
wannabe-bool predicate-methods boolean conversion ruby prodis

wannabe_bool's Introduction

Wannabe Bool

If string, numeric, symbol and nil values wanna be a boolean value, they can with the new to_b method. Moreover, you can use WannabeBool::Attributes module to create predicate methods in your classes.

Gem Version Build Status Coverage Status Code Climate Dependency Status GitHub license

Installing

Gemfile

gem 'wannabe_bool'

Direct installation

$ gem install wannabe_bool

Using

to_b method is available on String, Symbol, Numeric, TrueClass, FalseClass and NilClass.

For sake of readability (and personal choice), to_b has two aliases:

  • to_bool
  • to_boolean

Given this example:

{
  one: 'value',
  two: 2,
  mobile?: params[:mobile].to_b
}

It could be "more readable" like this:

{
  one: 'value',
  two: 2,
  mobile?: params[:mobile].to_boolean
}

Don't forget to require the gem:

require 'wannabe_bool'

String

  • Returns true if string is one of these values: t, true, on, y, yes, 1.
  • Returns false if string is one of these values: f, false, off, n, no, 0.
  • For invalid boolean string representations, returns false by default. See "Invalid Value Behaviour" section for more options.

It ignores trailing spaces and letter cases.

'1'.to_b    # => true
't'.to_b    # => true
'T'.to_b    # => true
'true'.to_b # => true
'TRUE'.to_b # => true
'on'.to_b   # => true
'ON'.to_b   # => true
'y'.to_b    # => true
'yes'.to_b  # => true
'YES'.to_b  # => true

' 1 '.to_b    # => true
' t '.to_b    # => true
' T '.to_b    # => true
' true '.to_b # => true
' TRUE '.to_b # => true
' on '.to_b   # => true
' ON '.to_b   # => true
' y '.to_b    # => true
'Y'.to_b      # => true
' Y '.to_b    # => true
' yes '.to_b  # => true
' YES '.to_b  # => true

'0'.to_b     # => false
'f'.to_b     # => false
'F'.to_b     # => false
'false'.to_b # => false
'FALSE'.to_b # => false
'off'.to_b   # => false
'OFF'.to_b   # => false
'n'.to_b     # => false
'N'.to_b     # => false
'no'.to_b    # => false
'NO'.to_b    # => false

' 0 '.to_b     # => false
' f '.to_b     # => false
' F '.to_b     # => false
' false '.to_b # => false
' FALSE '.to_b # => false
' off '.to_b   # => false
' OFF '.to_b   # => false
' n '.to_b     # => false
' N '.to_b     # => false
' no '.to_b    # => false
' NO '.to_b    # => false

''.to_b  # => false
' '.to_b # => false

Invalid Value Behaviour for strings

You can configure the result for invalid boolean string representations, using the WannabeBool.invalid_value_behaviour option.

There are 3 predefined behaviours available: to return false (default), nil or raise an ArgumentError:

WannabeBool.invalid_value_behaviour = WannabeBool::InvalidValueBehaviour::False
'wherever'.to_b # => false

WannabeBool.invalid_value_behaviour = WannabeBool::InvalidValueBehaviour::Nil
'wherever'.to_b # => nil

WannabeBool.invalid_value_behaviour = WannabeBool::InvalidValueBehaviour::Error
'wherever'.to_b # => ArgumentError: is not a valid boolean representation

Moreover, you can provide your own behaviour for invalid boolean string representations. Just set a proc or lambda, or even any class or object that responds to call method.

WannabeBool.invalid_value_behaviour = -> { :prodis }
'wherever'.to_b # => :prodis

Note that WannabeBool.invalid_value_behaviour is a global configuration. Said that, all the results for to_b method with invalid boolean string representations will be affected.

Symbol

Same as symbol.to_s.to_b.

:'1'.to_b  # => true
:t.to_b    # => true
:true.to_b # => true
:on.to_b   # => true
:y.to_b    # => true
:yes.to_b  # => true

:'0'.to_b   # => false
:f.to_b     # => false
:false.to_b # => false
:off.to_b   # => false
:n.to_b     # => false
:no.to_b    # => false

Numeric

Returns false if number is zero. Returns true otherwise.

Integer

0.to_b  # => false
1.to_b  # => true
2.to_b  # => true
-1.to_b # => true
-2.to_b # => true

Float

0.0.to_b  # => false
0.1.to_b  # => true
1.0.to_b  # => true
-0.1.to_b # => true
-1.0.to_b # => true

BigDecimal

require 'bigdecimal'

BigDecimal('0.0').to_b  # => false
BigDecimal('0.1').to_b  # => true
BigDecimal('1.0').to_b  # => true
BigDecimal('-0.1').to_b # => true
BigDecimal('-1.0').to_b # => true

TrueClass

Returns true.

true.to_b # => true

FalseClass

Returns false.

false.to_b # => false

NilClass

Returns false.

nil.to_b # => false

Creating predicate methods

class Fake
  include WannabeBool::Attributes

  attr_accessor :name, :main, :published
  attr_wannabe_bool :main, :published
end

fake = Fake.new
fake.main?      # => false
fake.published? # => false

fake.main = true
fake.main? # => true

fake.published = 1
fake.published? # => true

fake.main = 'true'
fake.main? # => true

fake.published = :true
fake.published? # => true

Changelog

See the changes in each version.

Author

Fernando Hamasaki de Amorim (prodis)

Prodis Logo

Contributing to wannabe_bool

See the contributing guide.

wannabe_bool's People

Contributors

galliani avatar prodis avatar reshleman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

wannabe_bool's Issues

Empty String

I believe there should be examples with an empty string as well. What would be the result of ''.to_b ?

Boolean Integers

I think that the treatment of integers, in this Gem, is contrary to other places in Ruby.
0 represents absence, much like nil does. It makes sense that they share the false value.
1 represents presence, as do other values. In conditional statements, the presence of a hash key is truthy. Therefore I propose that non-zero integers should return true.

Strings that have non-specific values (not "true" or "F") should also be true, because they have value. "F","false", etc are special cases that should be false.

Aliases for to_b method

For sake of readability (and personal choice):

  • to_bool
  • to_boolean

Given this example:

{
  one: 'value',
  two: 2,
  mobile?: params[:mobile].to_b
}

It could be "more readable" like this:

{
  one: 'value',
  two: 2,
  mobile?: params[:mobile].to_boolean
}

Option to return nil

It would be awesome to have an option to return nil if the value is not an acceptable truthy or falsey value. Or maybe raise an exception?

A use case would be if parsing params, you wouldn't want to always have everything that is not true default to false, you might instead want to throw some sort of error if the param was not an acceptable true/false input.

'true'.to_b        # => true
'false'.to_b       # => false
'invalid'.to_b     # => nil
42.to_b            # => nil

Maybe this is something that can be setup in an initializer?

Thanks!

bin/console requires Pry in production

First, thank you for the great gem!

When I added the wannabe_bool gem to my project, I noticed that I was unable to access the Rails console in production. After digging in, it appeared that my project was falling back to the bin/console in the wannabe_bool gem.

Of course, I could always add my own bin/console in my project, but I thought it would be nice to remove the dependency on Pry in production in the gem itself.

I've created #15 to address the issue. It was a bit tricky to test, so just the changes. Please let me know if you have any feedback.

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.