Nobiru

Gem Version Build Status Coverage Status

Nobiru (japanese: extend) is a collection of commonly used object helpers in a ruby based project. It currently includes extensions for: arrays, enumerables, hashes, numerics, objects, strings, and time.

Rails Safe = methods extracted from rails but that do not override that rails method.

Highly recommended extensions:

Installation

Add this line to your application's Gemfile:

gem 'nobiru'

And then execute:

$ bundle

Or install it yourself as:

$ gem install nobiru

Usage

ArrayExtensions

Remove Blanks:

Use the remove_blanks method removes blank elements from an array.

["this", "", "that", nil].remove_blanks #=> ["this", "that"]
"this    is   a  test".split(" ").remove_blanks #=> ["this", "is", "a", "test"]

Remove First Element:

Use the remove_first method removes the first element from an array. Like Array.shift, but returns the array instead of the removed element.

["1", "2", "3"].remove_first #=> ["2", "3"]

Remove Last Element:

Use the remove_last method removes the last element from an array. Like Array.pop, but returns the array instead of the removed element.

["1", "2", "3"].remove_last #=> ["1", "2"]

EnumerableExtensions

Difference:

Use the difference method to return the difference of a collection of numbers.

[1,2,3].difference #=> -4
[].difference #=> 0
[].difference(nil) #=> nil

Use the divisible method to return the division of a collection of numbers.

[16,4,2].divisible #=> 2
[].divisible #=> 0
[].divisible(nil) #=> nil

Drop Last:

Use the drop_last method to drops the last number of elements of a collection.

[1,2,3].drop_last(1) #=> [1,2]
[].drop_last(3) #=> []

Drop Last While:

Use the drop_last_while method to drops the last number of elements of a collection while it meets a criteria.

[1,2,3].drop_last_while(&:odd?) #=> [1,2]
[].drop_last_while(&:odd?) #=> []

Exactly:

Use the exactly? method to return if there are exactly the number of an element type.

[1,2,3].excatly?(3) #=> true
[1,1,3,3].exactly?(2, &:even?) #=> false
[].exactly?(1) #=> false

Use the exponential method to return the exponential of a collection of numbers.

[2,3,4].exponential #=> 4096
[].exponential #=> 0
[].exponential(nil) #=> nil

Frequencies:

Use the frequencies method to return a hash of the number of times a value in an array appears.

[1, :symbol, 'string', 3, :symbol, 1].frequencies #=> { 1 => 2, :symbol => 2, 'string' => 1, 3 => 1 }
[].frequencies #=> {}

Max:

Use the max method to return the largest value of a collection of numbers.

[2,3,1].max #=> 3
[].max #=> 0
[].max(nil) #=> nil

Min:

Use the min method to return the smallest value of a collection of numbers.

[2,3,1].min #=> 3
[].min #=> 0
[].min(nil) #=> nil

Mean:

Use the mean method to return the average of a collection of numbers.

[1,2,3].mean #=> 2
[].mean #=> 0
[].mean(nil) #=> nil

Median:

Use the median method to return the middle value of a collection of numbers.

[1,2,6].median #=> 2
[1,2,3,6].median #=> 2.5
[].median #=> 0
[].median(nil) #=> nil

Mode:

Use the mode method to return the most frequent value of a collection of numbers.

[1,1,2,6].mode #=> 1
[1,2,3].mode #=> nil
[].mode #=> 0
[].mode(nil) #=> nil

Use the multiple method to return the multiplication of a collection of numbers.

[1,2,3].multiple #=> 6
[].multiple #=> 0
[].multiple(nil) #=> nil

Range:

Use the range method to return the difference between the smallest and largest value of a collection of numbers.

[1,2,6].range #=> 5
[].range #=> 0
[].range(nil) #=> nil

Several:

Use the several? method to return if there are several types of an element.

[1,2,3].several? #=> true
[1,1,3,3].several?(&:even?) #=> false
[].several? #=> false

Standard Deviation:

Use the standard_deviation method to return the standard deviation of elements of a collection.

[1,2,6].standard_deviation #=> 2.6457513110645907
[].standard_deviation #=> nil

Sum:

Use the sum method to return the sum of a collection of numbers.

[1,2,3].sum #=> 6
[].sum #=> 0
[].sum(nil) #=> nil

Take Last:

Use the take_last method to return the last number of elements of a collection.

[1,2,3].take_last(2) #=> [2,3]
[].take_last(3) #=> []

Take Last While:

Use the take_last_while method to return the last number of elements of a collection while it meets a criteria.

[1,2,3,5].take_last_while(&:odd?) #=> [5, 5]
[].take_last_while(&:odd?) #=> []

Variance:

Use the variance method to return the variance of elements of a collection.

[1,2,6].variance #=> 7
[].variance #=> nil

HashExtensions

Except:

Use the except method to return only key/value pairs not matching certain keys. Rails Safe

{ foo: 'foo', baz: 'baz', bar: 'bar' }.except(:foo) #=> { baz: 'baz', bar: 'bar' }
{ :foo => 'foo', :baz => 'baz', :bar => 'bar' }.except(:baz, :bar) #=> { :foo => 'foo' }
{}.except(:foo) #=> {}

Only:

Use the only method to return only key/value pairs matching certain keys. Rails Safe

{ foo: 'foo', baz: 'baz', bar: 'bar' }.only(:foo) #=> { foo: 'foo' }
{ :foo => 'foo', :baz => 'baz', :bar => 'bar' }.only(:baz, :bar) #=> { :baz => 'baz', :bar => 'bar' }
{}.only(:foo) #=> {}

Rename Keys:

Use the rename_keys and rename_keys! method to rename the keys of a hash.

{ foo: 'foo', baz: 'baz' }.rename_keys(foo: :bar) #=> { bar: 'foo', baz: 'baz' }
{ foo: 'foo', 'baz' => 'baz' }.rename_keys(foo: :bar, 'baz' => 'tick') #=> { bar: 'foo', tick: 'baz' }

Stringify Keys:

Use the stringify_keys and stringify_keys! method to convert the hash keys to strings. Rails Safe

{ foo: 'foo', 'bar' => 'bar' }.stringify_keys #=> { 'foo' => 'foo', 'baz' => 'baz' }

Symbolize Keys:

Use the symbolize_keys and symbolize_keys! method to convert the hash keys to symbols. Rails Safe

{ foo: 'foo', 'bar' => 'bar' }.symbolize_keys #=> { foo: 'foo', baz: 'baz' }

Symbolize and Underscore Keys:

Use the symbolize_and_underscore_keys and symbolize_and_underscore_keys! method to symbolize and underscore keys.

{ 'firstName' => 'example', lastName: 'string' }.symbolize_and_underscore_keys #=> { first_name: 'foo', last_name: 'test' }

ObjectExtensions

Blank:

Use the blank? method on a object to determine if it is empty or nil. Rails Safe

"".blank? #=> true
"Awesome Sting".blank? #=> false

Present:

Use the present? method on a object to determine if it is not empty or nil. Rails Safe

"Awesome Sting".blank? #=> true
"".present? #=> false

Numeric:

Use the numeric? method to determine whether an object's to_s value is numeric.

"-32.50".numeric? #=> true
"$2.55".numeric? #=> false

Palindrome:

Use the palindrome? method to determine if an object is a palindrome.

"racecar".palindrome? #=> true
12321.palindrome? #=> true
"example".palindrome? #=> false
12345.palindrome? #=> false

Try:

Use the try method on a object to try that method with out raising an error. Rails Safe

"example".try(:upcase) #=> "EXAMPLE"
"example".try(:fake_method) #=> nil

NumericExtensions

Add:

Use the add method to add two numbers.

4.add(2) #=> 6

Divide:

Use the divide method to divide two numbers.

4.divide(2) #=> 2

Multiply:

Use the multiply method to multiply two numbers.

4.multiply(2) #=> 8

Multiple Of:

Use the multiple_of? method to check if a number is the multiple of another. Rails Safe

9.multiple_of?(3) #=> true
7.multiple_of?(3) #=> false

Negative:

Use the negative? method to check if a number is negative.

-1.negative? #=> true
1.negative? #=> false

Positive:

Use the positive? method to check if a number is positive.

1.positive? #=> true
-1.positive? #=> false

Power:

Use the power method to return the power of two numbers.

4.power(2) #=> 16

Subtract:

Use the subtract method to subtract two numbers.

4.subtract(2) #=> 2

To Byte:

Use the to_byte method to convert a byte size from one unit to another unit.

1024.to_byte #=> 1 #KB
5120.to_byte(:kb, :mb) #=> 5 #MB
1.to_byte(:mb, :kb) #=> 1024 #KB
80.to_byte(:mb, :gb) #=> 0.1 #GB

To Length:

Use the to_length method to convert a length from one unit to another unit.

1.to_length #=> 0.039370078740157 #IN
10.to_length(:mm, :cm) #=> 1 #CM
2.to_length(:mi, :yd) #=> 3520 #IN

To Time Unit:

Use the to_time_unit method to convert a time unit from one unit to another unit.

120.to_time_unit #=> 2 #MIN
2.to_time_unite(:day, :sec) #=> 172800 #SEC

To Temperature:

Use the to_temperature method to convert a temperature from one unit to another unit.

100.to_temperature #=> 212 #F
212.to_temperature(:f, :c) #=> 100 #C
212.to_temperature(:fahrenheit, :kelvin) #=> 373.15 #K

To Weight:

Use the to_weight method to convert a weight from one unit to another unit.

1.to_weight #=> 0.035273961949580004 #OZ
2.to_weight(:kg, :lb) #=> 4.4092452436976 #LB
3.to_weight(:lb, :kg) #=> 1.3607771100000001 #LB

StringExtensions

Camelize:

Use the camelize and camelize! method to transfrom a string to camelcase. Rails Safe

"example_string".camelize #=> "ExampleString"
"example_string".camelize(:lower) #=> "exampleString"

Ends With:

Use the ends_with? method to determine whether a string ends with a certain value. Rails Safe

"example string".ends_with?("g") #=> true
"example string".ends_with?("ng") #=> true
"example string".ends_with?("e") #=> false

Starts With:

Use the starts_with? method to determine whether a string starts with a certain value. Rails Safe

"example string".starts_with?("e") #=> true
"example string".starts_with?("ex") #=> true
"example string".starts_with?("g") #=> false

Humanize:

Use the humanize and humanize! method to transform a string to a human readable string. Rails Safe

"ExampleString".humanize #=> "Example string"
"example_string".humanize #=> "Example string"

Titleize:

Use the titleize and titleize! method to capitalize each word in a string. Rails Safe

"example string".titleize #=> "Example String"
"example_string".titleize #=> "Example String"
"ExampleString".titleize #=> "Example String"

Underscore:

Use the underscore and underscore! method to transform a string to snakecase. Rails Safe

"ExampleString".underscore #=> "example_string"
"exampleString".underscore #=> "example_string"

Domain:

Use the domain method to extract the domain name from a URL.

"http://www.example.com/fake-page".domain #=> "www.example.com"

Downcase:

Use the downcase? method to determine if all characters are lowercase.

"example".downcase? #=> true
"Example".downcase? #=> false
"EXAMPLE".downcase? #=> false

Upcase:

Use the upcase? method to determine if all characters are uppercase.

"EXAMPLE".upcase? #=> true
"example".upcase? #=> false
"Example".upcase? #=> false

Mixcase:

Use the mixcase? method to determine if characters are mixedcase.

"Example".mixedcase? #=> true
"EXAMPLE".mixedcase? #=> false
"example".mixedcase? #=> false

Ellipsize:

Use the ellipsize method to truncate a string in the middle.

Options

  • Length: default to 30
  • Offset: default to 4
  • Separator: default to "..."
"example string".ellipsize #=> "example string"
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".ellipsize #=> "0123...WXYZ"
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".ellipsize(offset: 2, separator: "+++") #=> "01+++YZ"

Nix:

Use the nix and nix! method to remove the first instance of a string.

"this thing that thing".nix("thing") #=> "this  that thing"

GNix:

Use the gnix and gnix! method to remove the every instance of a string.

"this thing that thing".gnix("thing") #=> "this  that "

Pollute:

Use the pollute method to pollute the space between every letter in a string, so it will be exempt from any impending string searches.

"test".pollute #=> "t^--^--^e^--^--^s^--^--^t^--^--^"
"test".pollute("-") #=> "t-e-s-t-"

Unpollute:

Use the unpollute to remove the default or custom pollution character. Can also be used to remove an unwanted character.

"t^--^--^e^--^--^s^--^--^t^--^--^".unpollute #=> "test"
"t-e-s-t-".unpollute #=> "test"

Slugify:

Use the slugify and slugify! method to generate a permalink-style string, with odd characters removed.

"example".slugify #=> "example"
"example string".slugify #=> "example-string"
"Example string @@@ test!".slugify #=> "example-string-test"

Strip Tags:

Use the strip_tags and strip_tags! method to remove HTML tags from a string.

"example".strip_tags #=> "example"
"<a href='http://example.com'>click</a>".strip_tags #=> "click"
"this is <b>bold</b> and <em>emphatic</em>".strip_tags #=> "this is bold and emphatic"

Strip Whitespace:

Use the strip_whitespace and strip_whitespace! method removes tab characters and instances of more than one space.

"example   string      test".strip_whitespace #=> "example string test"
"  this \t is also a test  ".strip_whitespace #=> "this is also a test"

Truncate Preserving Words:

Use the truncate_preserving_words method to truncate a string while preserving words.

Options

  • max_words: default to nil
  • max_characters: default to 30
  • Separator: default to "..."
"example string".truncate_preserving_words #=> "example string"
"example string test another1 another2 another3".truncate_preserving_words #=> "example string test another1 ..."
"example string test another1 another2 another3".truncate_preserving_words(max_chars: 10, separator: "+++") #=> "example +++"

TimeExtensions

Format:

Use the format method on a Date or Time object to format it using a human readable string.

Rules

  • Characters: a-z 0-9 _
  • Characters can only be used to generate a format part
Time.now.format("year") #=> "2014"
Time.now.format("month_name day, year hour:minute ampm") #=> "January 09, 2014 02:31 pm"
Name Key Equivalent strftime Result
Month - digits zero-padded m or month or month_zero %m (01..12)
Month - digits unpadded mm or Month or month_unpadded %-m (1..12)
Month - digits blank-padded mmm or MONTH or day_blank %_m ( 1..12)
Month - name mmmm or month_name %B January
Month - name abbreviated mmmmm or month_name_abbr %b Jan
Day - digits zero-padded d or day or day_zero %d (01..31)
Day - digits unpadded dd or Day or day_unpadded %-d (1..31)
Day - digits blank-padded ddd or DAY or day_blank %_d ( 1..31)
Day - digits of the year dddd or day_of_the_year %j (001..366)
Week - starting monday wwwww or week %M (00..53)
Week - starting sunday wwwwww or weekday_offset %M (00..53)
Weekday - starting monday w or weekday %M (1..7)
Weekday - starting sunday ww or weekday %M (0..6)
Weekday - name www or weekday_name %M Sunday
Weekday - name abbreviated wwww or weekday_name_abbr %M Sun
Year - digits two yy or yr %y (00..99)
Year - digits four yyyy or year %Y 1999
Hour - digits zero-padded h or hour or hour_zero %H (00..23)
Hour - digits blank-padded hh or HOUR or hour_blank %k ( 0..23)
Hour - digits zero-padded hhh or hour_imperical or hour_imperical_zero %I (01..12)
Hour - digits blank-padded hhhh or HOUR_IMPERICAL or hour_imperical_blank %l ( 1..12)
Minute - minute n or minute %M (00..59)
Second - second s or second %S (00..60)
Meridian - lowercase ampm or meridian %p am..pm
Meridian - uppercase AMPM or MERIDIAN %P AM..PM
Time Zone - time zone z or time_zone %z +0900
Time Zone - hour and minute offset zz or time_zone_offset %z +09:00
Time Zone - hour, minute and second offset zzz or time_zone_offset_full %z +09:00:00

To Format:

Use the to_format method on a Date or Time object to format it without having to use strftime method. For a full list check out the time extention file.

Time.now.to_format(:year) #=> "2014"
Time.now.to_format(:datetime) #=> "January 09, 2014 02:31 pm"
Name Key Equivalent strftime Result
Month - digits zero-padded :month or :month_zero %A (01..12)
Month - digits unpadded :month_unpadded %a (1..12)
Month - digits blank-padded :month_blank %a ( 1..12)
Month - name :month_name %A January
Month - name abbreviated :month_name_abbr %a Jan
Weekday - digits zero-padded :weekday_zero %A (01..31)
Weekday - digits unpadded :weekday_unpadded %a (1..31)
Weekday - digits blank-padded :weekday_blank %a ( 1..31)
Weekday - name :weekday_name %A Sunday
Weekday - name abbreviated :weekday_name_abbr %a Sun
Year - digits two :yr %y (00..99)
Year - digits four :year %Y 1999
Hour - digits zero-padded :hour or :hour_zero %H (00..23)
Hour - digits blank-padded :hour_blank %k ( 0..23)
Hour - digits zero-padded imperical :hour_imperical_zero %I (01..12)
Hour - digits blank-padded imperical :hour_imperical_blank %l ( 1..12)
Minute - minute :minute %M (00..59)
Second - second :second %S (00..60)
Time Zone - time zone :time_zone %z +0900
Time Zone - hour and minute offset :time_zone_offset %z +09:00
Time Zone - hour, minute and second offset :time_zone_offset_full %z +09:00:00
Date - name :date %B %-d, %Y January 9, 2014
Date - name abbreviated :date_abbr %b %-d, %Y Jan 9, 2014
Date - iso :date_iso %Y-%m-%d 2014-01-09
Datetime - name :datetime %B %-d, %Y %H:%M January 9, 2014 00:31
Datetime - name abbreviated :datetime_abbr %b %-d, %Y %H:%M Jan 9, 2014 00:31
Datetime - iso :datetime_iso %Y-%m-%d %H:%M 2014-01-09 00:31
Datetime - name imperical :datetime_imperical %B %-d, %Y %H:%M January 9, 2014 12:31 am
Datetime - name abbreviated imperical :datetime_imperical_abbr %b %-d, %Y %H:%M Jan 9, 2014 12:31 am
Datetime - iso imperical :datetime_imperical_iso %Y-%m-%d %H:%M 2014-01-09 12:31 am
Datetime - name time zone :datetime_tzn %B %-d, %Y %H:%M %Z January 9, 2014 00:31 UTC
Datetime - name abbreviated time zone :datetime_abbr_tzn %b %-d, %Y %H:%M %Z Jan 9, 2014 00:31 UTC
Datetime - iso time zone :datetime_iso_tzn %Y-%m-%d %H:%M %z 2014-01-09 00:31 +0000
Datetime - name imperical time zone :datetime_imperical_tzn %B %-d, %Y %H:%M %Z January 9, 2014 12:31 am UTC
Datetime - name abbreviated imperical time zone :datetime_imperical_abbr_tzn %b %-d, %Y %H:%M %Z Jan 9, 2014 12:31 am UTC
Datetime - iso imperical time zone :datetime_imperical_iso_tzn %Y-%m-%d %H:%M %z 2014-01-09 12:31 am +0000
Day - name :day %B %-d January 9
Day - name abbreviated :day_abbr %b %-d Jan 9
Day - iso :day_iso %m-%d 01-09
Daytime - name :daytime %B %-d %H:%M January 9 00:31
Daytime - name abbreviated :daytime_abbr %b %-d %H:%M Jan 9 00:31
Daytime - iso :daytime_iso %m-%d %H:%M 01-09 00:31
Daytime - name imperical :daytime_imperical %B %-d %H:%M January 9 12:31 am
Daytime - name abbreviated imperical :daytime_imperical_abbr %b %-d %H:%M Jan 9 12:31 am
Daytime - iso imperical :daytime_imperical_iso %m-%d %H:%M 01-09 12:31 am
Time - zero-padded :time or :time_zero %H:%M 00:31
Time - blank-padded :time_blank %k:%M %z 0:31
Time - with time zone :time_tz %H:%M %z 00:31 +0000
Time - with time zone name :time_tzn %H:%M %Z 00:31 UTC

Contributing

  1. Fork it ( http://github.com//nobiru/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request