项目作者: gkats

项目描述 :
Tiny library that ensures you always have an object to act upon.
高级语言: Ruby
项目地址: git://github.com/gkats/guaranteed.git
创建时间: 2017-10-10T14:13:56Z
项目社区:https://github.com/gkats/guaranteed

开源协议:MIT License

下载


guaranteed

Give me something for nothing!

What is this?

Guaranteed is a tiny library that ensures you always have an object to act upon. By eliminating continual checks for your objects’ capabilities you can focus on your code’s functionality.

Guaranteed’s primary goal is to eliminate nil checks.

Hmmm… I’m not sure I need this

Hey, I’m with you here, you probably don’t. Consider this a “nice to have” library. But the truth is that using the library can lead to much cleaner designs. Plus, it’s almost zero overhead for your project. All you need is Ruby.

Here’s the basic usage. Let’s fire up irb and illustrate a frequent problem we face when coding.

  1. # The most explicit approach
  2. # Too verbose!
  3. def username
  4. user.nil? ? nil : user.name
  5. end
  6. # An improvement (arguably)
  7. # What if the name attribute should return a boolean?
  8. def username
  9. user && user.name
  10. end
  11. # Rails developers think this is a neat trick :(
  12. def username
  13. user.try(:name)
  14. end
  15. # Guaranteed to the rescue!
  16. def username
  17. Guaranteed.Object(user).name
  18. end

I’m still not convinced

The above example is just scratching the surface of Guaranteed’s abilities. Let’s take a look at some more interesting examples.

Most common Ruby methods on objects are supported. They follow Ruby’s implicit type conversions for an instance of NilClass.

  1. object = Guaranteed.Object(nil)
  2. object.nil? # => true
  3. !object # => true
  4. object.to_a # => []
  5. object.to_h # => {}
  6. object.to_s # => ''
  7. object.to_f # => 0.0
  8. object.to_i # => 0
  9. object.empty? # => true

Need method chaining? You’re covered.

  1. object = Guaranteed.Object(nil)
  2. object.this.will.definitely.not.work!
  3. # => Returns self

Agreed, this amount of method chaining breaks the Law of Demeter. Sometimes reality hits you hard in the face though. Let’s try the above example using ActiveSupport::Object.

  1. # Ugh..
  2. object.try(:this).try(:will).try(:definitely).try(:not).try(:work!)
  3. # => nil

Bonus Ruby idiom that’s supported.

  1. object = Guaranteed.Object(nil)
  2. object.tap do |o|
  3. o.name = 'Homer Simpson'
  4. o.save!
  5. end
  6. # => Returns self

Using Rails? I’ve got good news for you! The library supports some Rails-specific methods too.

  1. object = Guaranteed.Object(nil)
  2. object.present? # => false
  3. object.as_json # => nil
  4. object.to_json # => "null"
  5. object.persisted? # => false

OK, I’m sold. How do I install it?

  1. $ gem install guaranteed
  2. # or add it to your Gemfile
  3. gem 'guaranteed', '~> 0.1.0'

Can I contribute?

Yes, please. Feel free to fork the project and create your own branches. Consider creating a pull request back to this repo.

Remember, you can run the tests with

  1. $ rake