nobiru 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,141 @@
1
+ module Enumerable
2
+
3
+ unless method_defined?(:sum)
4
+ def sum(identity=0)
5
+ inject(:+) || identity
6
+ end
7
+ end
8
+
9
+ def drop_last(n)
10
+ array = to_a
11
+
12
+ return array if n > array.size
13
+ array[0...(array.size - n)]
14
+ end
15
+
16
+ def drop_last_while
17
+ return to_enum(:drop_last_while) unless block_given?
18
+
19
+ result = []
20
+ dropping = true
21
+ reverse_each do |value|
22
+ result.unshift(value) unless dropping &&= yield(value)
23
+ end
24
+ result
25
+ end
26
+
27
+ def exactly?(n)
28
+ found_count = 0
29
+
30
+ if block_given?
31
+ each do |*o|
32
+ found_count += 1 if yield(*o)
33
+ end
34
+ else
35
+ each do |o|
36
+ found_count += 1 if o
37
+ end
38
+ end
39
+
40
+ (found_count > n) ? false : n == found_count
41
+ end
42
+
43
+ def frequencies
44
+ each_with_object(Hash.new(0)) { |e, a| a[e] += 1 }
45
+ end
46
+
47
+ def max(identity=0)
48
+ size > 0 ? sort.last : identity
49
+ end
50
+
51
+ def min(identity=0)
52
+ size > 0 ? sort.first : identity
53
+ end
54
+
55
+ def mean(identity=0)
56
+ collection_size = size
57
+ collection_size > 0 ? inject(:+) / collection_size.to_f : identity
58
+ end
59
+
60
+ def median(identity=0)
61
+ collection_size = size
62
+ collection_sorted = sort
63
+
64
+ if collection_size > 0
65
+ if collection_size % 2 == 0
66
+ (collection_sorted[(collection_size / 2) -1] + collection_sorted[collection_size / 2]) / 2.0
67
+ else
68
+ collection_sorted[collection_size / 2]
69
+ end
70
+ else
71
+ identity
72
+ end
73
+ end
74
+
75
+ def mode(identity=0)
76
+ if size > 0
77
+ frequency_distribution = inject(Hash.new(0)) { |h, v| h[v] += 1; h }
78
+ frequency_top_2 = frequency_distribution.sort { |a, b| b[1] <=> a[1] }.take(2)
79
+
80
+ if frequency_top_2.length == 1
81
+ frequency_top_2.first.first
82
+ elsif frequency_top_2.first.last == frequency_top_2.last.last
83
+ nil
84
+ else
85
+ frequency_top_2.first.first
86
+ end
87
+ else
88
+ identity
89
+ end
90
+ end
91
+
92
+ def range(identity=0)
93
+ collection_sorted = sort
94
+ size > 0 ? collection_sorted.last - collection_sorted.first : identity
95
+ end
96
+
97
+ def several?
98
+ found_count = 0
99
+
100
+ if block_given?
101
+ each do |*o|
102
+ found_count += 1 if yield(*o)
103
+ end
104
+ else
105
+ each do |o|
106
+ found_count += 1 if o
107
+ end
108
+ end
109
+
110
+ (found_count > 1) ? true : false
111
+ end
112
+
113
+ def standard_deviation
114
+ return 0 if length < 2
115
+ Math.sqrt(variance)
116
+ end
117
+
118
+ def take_last(n)
119
+ array = to_a
120
+
121
+ return array if n > array.size
122
+ array[(array.size - n)..-1]
123
+ end
124
+
125
+ def take_last_while
126
+ return to_enum(:take_last_while) unless block_given?
127
+
128
+ result = []
129
+ reverse_each { |e| yield(e) ? result.unshift(e) : break }
130
+ result
131
+ end
132
+
133
+ def variance
134
+ collection_length = length
135
+
136
+ return 0 if collection_length <= 1
137
+ sum = inject(0) { |accumulator, value| accumulator + (value - mean) ** 2 }
138
+ sum / (collection_length.to_f - 1)
139
+ end
140
+
141
+ end
@@ -0,0 +1,70 @@
1
+ class Hash
2
+
3
+ unless method_defined?(:stringify_keys)
4
+ def stringify_keys
5
+ inject({}) do |options, (key, value)|
6
+ options[key.to_s] = value
7
+ options
8
+ end
9
+ end
10
+ end
11
+
12
+ unless method_defined?(:stringify_keys!)
13
+ def stringify_keys!
14
+ replace(stringify_keys)
15
+ end
16
+ end
17
+
18
+ unless method_defined?(:symbolize_keys)
19
+ def symbolize_keys
20
+ inject({}) do |options, (key, value)|
21
+ options[(key.to_sym rescue key) || key] = value
22
+ options
23
+ end
24
+ end
25
+ end
26
+
27
+ unless method_defined?(:symbolize_keys!)
28
+ def symbolize_keys!
29
+ replace(symbolize_keys)
30
+ end
31
+ end
32
+
33
+ unless method_defined?(:except)
34
+ def except(*args)
35
+ hash = dup
36
+ args.each {|k| hash.delete(k) }
37
+ hash
38
+ end
39
+ end
40
+
41
+ unless method_defined?(:only)
42
+ def only(*args)
43
+ hash = {}
44
+ args.each {|k| hash[k] = self[k] if self.has_key?(k) }
45
+ hash
46
+ end
47
+ end
48
+
49
+ def rename_keys(*args)
50
+ keys = Hash[*args.flatten]
51
+ keys.each { |k, v| self[v] = delete(k) if self[k] }
52
+ self
53
+ end
54
+
55
+ def rename_keys!(*args)
56
+ replace(rename_keys(*args))
57
+ end
58
+
59
+ def symbolize_and_underscore_keys
60
+ inject({}) do |options, (key, value)|
61
+ options[(key.to_s.underscore.to_sym rescue key) || key] = value
62
+ options
63
+ end
64
+ end
65
+
66
+ def symbolize_and_underscore_keys!
67
+ replace(symbolize_and_underscore_keys)
68
+ end
69
+
70
+ end
@@ -0,0 +1,102 @@
1
+ class Numeric
2
+
3
+ unless method_defined?(:multiple_of?)
4
+ def multiple_of?(number)
5
+ number != 0 ? modulo(number).zero? : zero?
6
+ end
7
+ end
8
+
9
+ def negative?
10
+ self < 0
11
+ end
12
+
13
+ def positive?
14
+ self > 0
15
+ end
16
+
17
+ def to_byte(from=:b, to=:kb)
18
+ scalers = { b: 1, kb: 1024 ** 1, mb: 1024 ** 2, gb: 1024 ** 3, tb: 1024 ** 4, pb: 1024 ** 5, eb: 1024 ** 6 }
19
+
20
+ to_f * scalers[from] / scalers[to]
21
+ end
22
+
23
+ def to_length(from=:mm, to=:in)
24
+ imperical_scalers = {
25
+ mm: 25.4, cm: 2.54, m: 0.0254, km: 0.0000254,
26
+ in: 1, ft: 12, yd: 36, mi: 63360, nm: 72913.4
27
+ }
28
+ metric_scalers = {
29
+ mm: 1, cm: 10, m: 1000, km: 1000000,
30
+ in: 0.039370078740157, ft: 0.0032808398950131, yd: 0.0010936132983377, mi: 0.00000062137119223733, nm: 0.00000053995680345572
31
+ }
32
+
33
+ case to
34
+ when from
35
+ self
36
+ when :mm, :cm, :m, :km
37
+ if [:in, :ft, :yd, :mi, :nm].include?(from)
38
+ to_f * imperical_scalers[from] * imperical_scalers[to]
39
+ else
40
+ to_f * metric_scalers[from] / metric_scalers[to]
41
+ end
42
+ when :in, :ft, :yd, :mi, :nm
43
+ if [:mm, :cm, :m, :km].include?(from)
44
+ to_f * metric_scalers[from] * metric_scalers[to]
45
+ else
46
+ to_f * imperical_scalers[from] / imperical_scalers[to]
47
+ end
48
+ end
49
+ end
50
+
51
+ def to_temperature(from=:c, to=:f)
52
+ case to
53
+ when from
54
+ self
55
+ when :c, :celsius
56
+ (to_f - 32) * 5 / 9
57
+ when :f, :fahrenheit
58
+ (to_f * 9 / 5) + 32
59
+ when :k, :kelvin
60
+ if [:c, :celsius].include?(from)
61
+ to_f + 273.15
62
+ else
63
+ ((to_f - 32) * 5 / 9) + 273.15
64
+ end
65
+ end
66
+ end
67
+
68
+ def to_time_unit(from=:sec, to=:min)
69
+ scalers = { sec: 1, min: 60 ** 1, hr: 60 ** 2, day: (60 ** 2) * 24, yr: (60 ** 2) * 24 * 365 }
70
+
71
+ to_f * scalers[from] / scalers[to]
72
+ end
73
+
74
+ def to_weight(from=:g, to=:oz)
75
+ imperical_scalers = {
76
+ mg: 28349.523125, cg: 2834.9523125, g: 28.349523125, kg: 0.028349523125, mt: 0.000028349523125,
77
+ oz: 1, lb: 16, tn: 32000
78
+ }
79
+ metric_scalers = {
80
+ mg: 1, cg: 10, g: 1000, kg: 1000000, mt: 1000000000,
81
+ oz: 0.00003527396194958, lb: 0.0000022046226218488, tn: 0.0000000011023113109244
82
+ }
83
+
84
+ case to
85
+ when from
86
+ self
87
+ when :mg, :cg, :g, :kg, :mt
88
+ if [:oz, :lb, :tn].include?(from)
89
+ to_f * imperical_scalers[from] * imperical_scalers[to]
90
+ else
91
+ to_f * metric_scalers[from] / metric_scalers[to]
92
+ end
93
+ when :oz, :lb, :tn
94
+ if [:mg, :cg, :g, :kg, :mt].include?(from)
95
+ to_f * metric_scalers[from] * metric_scalers[to]
96
+ else
97
+ to_f * imperical_scalers[from] / imperical_scalers[to]
98
+ end
99
+ end
100
+ end
101
+
102
+ end
@@ -0,0 +1,29 @@
1
+ class Object
2
+
3
+ unless method_defined?(:blank?)
4
+ def blank?
5
+ respond_to?(:empty?) ? !!empty? : !self
6
+ end
7
+ end
8
+
9
+ unless method_defined?(:present?)
10
+ def present?
11
+ !blank?
12
+ end
13
+ end
14
+
15
+ unless method_defined?(:try)
16
+ def try(method)
17
+ send(method) if respond_to?(method)
18
+ end
19
+ end
20
+
21
+ def numeric?
22
+ !to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/).nil?
23
+ end
24
+
25
+ def palindrome?
26
+ to_s == to_s.reverse
27
+ end
28
+
29
+ end
@@ -0,0 +1,183 @@
1
+ class String
2
+
3
+ unless method_defined?(:camelize)
4
+ def camelize(first_letter_in_uppercase = true)
5
+ if first_letter_in_uppercase != :lower
6
+ to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
7
+ else
8
+ to_s[0].chr.downcase + camelize(self)[1..-1]
9
+ end
10
+ end
11
+ end
12
+
13
+ unless method_defined?(:camelize!)
14
+ def camelize!(first_letter_in_uppercase = true)
15
+ replace(camelize(first_letter_in_uppercase))
16
+ end
17
+ end
18
+
19
+ unless method_defined?(:ends_with?)
20
+ def ends_with?(suffix)
21
+ suffix = suffix.to_s
22
+ self[-suffix.length, suffix.length] == suffix
23
+ end
24
+ end
25
+
26
+ unless method_defined?(:humanize)
27
+ def humanize(options = {})
28
+ underscore.
29
+ gsub(/_id$/, "").
30
+ tr('_', ' ').
31
+ gsub(/([a-z\d]*)/i) { |match| match.downcase }.
32
+ gsub(/^\w/) { |match| match.upcase } if options.fetch(:capitalize, true)
33
+ end
34
+ end
35
+
36
+ unless method_defined?(:humanize!)
37
+ def humanize!(options = {})
38
+ replace(humanize)
39
+ end
40
+ end
41
+
42
+ unless method_defined?(:titleize)
43
+ def titleize
44
+ underscore.
45
+ humanize.
46
+ gsub(/\b(?<!['’`])[a-z]/) { $&.capitalize }
47
+ end
48
+ end
49
+
50
+ unless method_defined?(:titleize!)
51
+ def titleize!
52
+ replace(titleize)
53
+ end
54
+ end
55
+
56
+ unless method_defined?(:starts_with?)
57
+ def starts_with?(prefix)
58
+ prefix = prefix.to_s
59
+ self[0, prefix.length] == prefix
60
+ end
61
+ end
62
+
63
+ unless method_defined?(:underscore)
64
+ def underscore
65
+ gsub(/::/, '/').
66
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
67
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
68
+ tr("-", "_").
69
+ downcase
70
+ end
71
+ end
72
+
73
+ unless method_defined?(:underscore!)
74
+ def underscore!
75
+ replace(underscore)
76
+ end
77
+ end
78
+
79
+ def domain(options={})
80
+ url = dup
81
+ url =~ (/^(?:\w+:\/\/)?([^\/?]+)(?:\/|\?|$)/) ? $1 : url
82
+ end
83
+
84
+ def downcase?
85
+ downcase == self
86
+ end
87
+
88
+ def ellipsize(options={})
89
+ length = options[:length] || 30
90
+ separator = options[:separator] || '...'
91
+ return self if size <= length
92
+ offset = options[:offset] || 4
93
+ self[0,offset] + separator + self[-offset,offset]
94
+ end
95
+
96
+ def gnix(string)
97
+ gsub(string, "")
98
+ end
99
+
100
+ def gnix!(string)
101
+ gsub!(string, "")
102
+ end
103
+
104
+ def mixedcase?
105
+ !upcase? && !downcase?
106
+ end
107
+
108
+ def nix(string)
109
+ sub(string, "")
110
+ end
111
+
112
+ def nix!(string)
113
+ sub!(string, "")
114
+ end
115
+
116
+ def pollute(delimiter="^--^--^")
117
+ split('').map{ |letter| "#{letter}#{delimiter}" }.join
118
+ end
119
+
120
+ def slugify
121
+ gsub(/[^\x00-\x7F]+/, ''). # Remove anything non-ASCII entirely (e.g. diacritics).
122
+ gsub(/[^\w_ \-]+/i, ''). # Remove unwanted chars.
123
+ gsub(/[ \-]+/i, '-'). # No more than one of the separator in a row.
124
+ gsub(/^\-|\-$/i, ''). # Remove leading/trailing separator.
125
+ downcase
126
+ end
127
+
128
+ def slugify!
129
+ replace(slugify)
130
+ end
131
+
132
+ def squish
133
+ strip.gsub(/\s+/, ' ')
134
+ end
135
+
136
+ def squish!
137
+ replace(squish)
138
+ end
139
+
140
+ def strip_tags
141
+ gsub(/<\/?[^>]*>/, "")
142
+ end
143
+
144
+ def strip_tags!
145
+ replace(strip_tags)
146
+ end
147
+
148
+ def strip_whitespace
149
+ gnix("\t").split(" ").remove_blanks.join(" ")
150
+ end
151
+
152
+ def strip_whitespace!
153
+ replace(strip_whitespace)
154
+ end
155
+
156
+ def truncate_preserving_words(options={})
157
+ separator = options[:separator] || '...'
158
+ max_words = options[:max_words] || nil
159
+ if max_words
160
+ words = split
161
+ return self if words.size < max_words
162
+ words = words[0..(max_words-1)]
163
+ words << separator
164
+ words.join(" ")
165
+ else
166
+ max_chars = options[:max_chars] || 30
167
+ return self if size < max_chars
168
+ out = self[0..(max_chars-1)].split(" ")
169
+ out.pop
170
+ out << separator
171
+ out.join(" ")
172
+ end
173
+ end
174
+
175
+ def unpollute(delimiter="^--^--^")
176
+ gsub(delimiter, "")
177
+ end
178
+
179
+ def upcase?
180
+ upcase == self
181
+ end
182
+
183
+ end
@@ -0,0 +1,166 @@
1
+ class Time
2
+
3
+ def format(string="yyyy-m-d h:n z")
4
+ delimiters = string.scan /\W+/
5
+ formatters = string.scan /[a-z0-9_]+/i
6
+
7
+ format_units = {
8
+ ## Date keys
9
+ "d" => "d",
10
+ "day" => "d",
11
+ "day_zero" => "d",
12
+ "dd" => "-d",
13
+ "Day" => "-d",
14
+ "day_unpadded" => "-d",
15
+ "ddd" => "_d",
16
+ "DAY" => "_d",
17
+ "day_blank" => "_d",
18
+ "dddd" => "j",
19
+ "day_of_the_year" => "j",
20
+ "m" => "m",
21
+ "month" => "m",
22
+ "month_zero" => "m",
23
+ "mm" => "-m",
24
+ "Month" => "-m",
25
+ "month_unpadded" => "-m",
26
+ "mmm" => "_m",
27
+ "MONTH" => "_m",
28
+ "month_blank" => "_m",
29
+ "mmmm" => "B",
30
+ "month_name" => "B",
31
+ "mmmmm" => "b",
32
+ "month_name_abbr" => "b",
33
+ "w" => "u",
34
+ "weekday" => "u",
35
+ "ww" => "w",
36
+ "weekday_offset" => "w",
37
+ "www" => "A",
38
+ "weekday_name" => "A",
39
+ "wwww" => "a",
40
+ "weekday_name_abbr" => "a",
41
+ "wwwww" => "W",
42
+ "week" => "W",
43
+ "wwwwww" => "U",
44
+ "week_offset" => "U",
45
+ "yy" => "y",
46
+ "yr" => "y",
47
+ "yyyy" => "Y",
48
+ "year" => "Y",
49
+
50
+ ## Time keys
51
+ "h" => "H",
52
+ "hour" => "H",
53
+ "hour_zero" => "H",
54
+ "hh" => "k",
55
+ "HOUR" => "k",
56
+ "hour_blank" => "k",
57
+ "hhh" => "I",
58
+ "hour_imperical" => "I",
59
+ "hour_imperical_zero" => "I",
60
+ "hhhh" => "l",
61
+ "HOUR_IMPERICAL" => "l",
62
+ "hour_imperical_blank" => "l",
63
+ "ampm" => "P",
64
+ "meridian" => "P",
65
+ "AMPM" => "p",
66
+ "MERIDIAN" => "p",
67
+ "n" => "M",
68
+ "minute" => "M",
69
+ "s" => "S",
70
+ "second" => "S",
71
+ "z" => "z",
72
+ "time_zone" => "z",
73
+ "zz" => ":z",
74
+ "time_zone_offset" => ":z",
75
+ "zzz" => "::z",
76
+ "time_zone_offset_full" => "::z",
77
+ "zzzz" => "Z",
78
+ "time_zone_name" => "Z"
79
+ }
80
+
81
+ strftime(
82
+ if string.empty? || string.nil?
83
+ "%Y-%m-%d %H:%M %z"
84
+ else
85
+ formatters.map do |f|
86
+ "%#{format_units[f]}#{delimiters.shift || ''}"
87
+ end.join
88
+ end
89
+ )
90
+ end
91
+
92
+ def to_format(key=:datetime_iso_tzn)
93
+ format_units = {
94
+ ## Date keys
95
+ month: "%m",
96
+ month_zero: "%m",
97
+ month_unpadded: "%-m",
98
+ month_blank: "%_m",
99
+ month_name: "%B",
100
+ month_name_abbr: "%b",
101
+ weekday: "%d",
102
+ weekday_zero: "%d",
103
+ weekday_unpadded: "%-d",
104
+ weekday_blank: "%_d",
105
+ weekday_name: "%A",
106
+ weekday_name_abbr: "%a",
107
+ yr: "%y",
108
+ year: "%Y",
109
+
110
+ ## Time keys
111
+ hour: "%H",
112
+ hour_zero: "%H",
113
+ hour_blank: "%k",
114
+ hour_imperical: "%I",
115
+ hour_imperical_zero: "%I",
116
+ hour_imperical_blank: "%l",
117
+ ampm: "%P",
118
+ meridian: "%P",
119
+ minute: "%M",
120
+ second: "%S",
121
+ time_zone: "%z",
122
+ time_zone_offset: "%:z",
123
+ time_zone_offset_full: "%::z",
124
+ time_zone_name: "%Z",
125
+
126
+ ## Datetime keys
127
+ date: "%B %-d, %Y",
128
+ date_abbr: "%b %-d, %Y",
129
+ date_iso: "%Y-%m-%d",
130
+ datetime: "%B %-d, %Y %H:%M",
131
+ datetime_abbr: "%b %-d, %Y %H:%M",
132
+ datetime_iso: "%Y-%m-%d %H:%M",
133
+ datetime_imperical: "%B %-d, %Y %I:%M %P",
134
+ datetime_imperical_abbr: "%b %-d, %Y %I:%M %P",
135
+ datetime_imperical_iso: "%Y-%m-%d %I:%M %P",
136
+ datetime_tzn: "%B %-d, %Y %H:%M %Z",
137
+ datetime_abbr_tzn: "%b %-d, %Y %H:%M %Z",
138
+ datetime_iso_tzn: "%Y-%m-%d %H:%M %z",
139
+ datetime_imperical_tzn: "%B %-d, %Y %I:%M %P %Z",
140
+ datetime_imperical_abbr_tzn: "%b %-d, %Y %I:%M %P %Z",
141
+ datetime_imperical_iso_tzn: "%Y-%m-%d %I:%M %P %z",
142
+ day: "%B %-d",
143
+ day_abbr: "%b %-d",
144
+ day_iso: "%m-%d",
145
+ daytime: "%B %-d %H:%M",
146
+ daytime_abbr: "%b %-d %H:%M",
147
+ daytime_iso: "%m-%d %H:%M",
148
+ daytime_imperical: "%B %-d %I:%M %P",
149
+ daytime_imperical_abbr: "%b %-d %I:%M %P",
150
+ daytime_imperical_iso: "%m-%d %I:%M %P",
151
+ time: "%H:%M",
152
+ time_zero: "%H:%M",
153
+ time_blank: "%k:%M",
154
+ time_tz: "%H:%M %z",
155
+ time_tzn: "%H:%M %Z",
156
+ time_imperical: "%I:%M %P",
157
+ time_imperical_zero: "%I:%M %P",
158
+ time_imperical_blank: "%l:%M %P",
159
+ time_imperical_tz: "%I:%M %P %z",
160
+ time_imperical_tzn: "%I:%M %P %Z"
161
+ }
162
+
163
+ strftime(format_units[key])
164
+ end
165
+
166
+ end
@@ -0,0 +1,3 @@
1
+ module Nobiru
2
+ VERSION = "0.0.1"
3
+ end
data/lib/nobiru.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'nobiru/version'
2
+ require 'nobiru/extensions/array_extension'
3
+ require 'nobiru/extensions/enumerable_extension'
4
+ require 'nobiru/extensions/hash_extension'
5
+ require 'nobiru/extensions/numeric_extension'
6
+ require 'nobiru/extensions/object_extension'
7
+ require 'nobiru/extensions/string_extension'
8
+ require 'nobiru/extensions/time_extension'