core_extensions 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -16,4 +16,6 @@ Gem::Specification.new do |gem|
16
16
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ['lib']
19
+
20
+ gem.add_dependency 'activesupport'
19
21
  end
@@ -0,0 +1,21 @@
1
+ class Array
2
+ def to_h
3
+ Hash[self]
4
+ end
5
+
6
+ def to_data
7
+ map(&:to_h)
8
+ end
9
+
10
+ def map_to(field)
11
+ self.map { |hash| hash[field] }
12
+ end
13
+
14
+ def exclude?(value)
15
+ !include?(value)
16
+ end
17
+
18
+ def to_hash_with_indexes_as_keys
19
+ each_with_index.map { |option,index| [index,option] }.to_h
20
+ end
21
+ end
@@ -0,0 +1,9 @@
1
+ class Float
2
+ def to_percent(digits_after_decimal=2)
3
+ (self * 100).round digits_after_decimal
4
+ end
5
+
6
+ def to_formatted_percent
7
+ to_percent.to_s + '%'
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ class Hash
2
+ def to_ostruct(klass = OpenStruct, cch = {})
3
+ cch[self] = klass.new
4
+ each do |key,value|
5
+ raise "Invalid key: #{ key }" unless key =~ /[a-z_][a-zA-Z0-9_]*/
6
+ cch[self].__send__ "#{ key }=", value.is_a?(Hash) ? cch[value] || value.send(__method__,klass,cch) : value
7
+ end
8
+ cch[self]
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ class Integer
2
+ def commify
3
+ self.to_s.gsub(/(\d)(?=(\d{3})+$)/,'\1,')
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class NilClass
2
+ def blank?
3
+ true
4
+ end
5
+ end
@@ -0,0 +1,25 @@
1
+ class Object
2
+ def self.find_all_descendents_of(parent_klass)
3
+ ObjectSpace.each_object(Class).select { |klass| klass < parent_klass }
4
+ end
5
+
6
+ def try(method)
7
+ send method rescue nil
8
+ end
9
+
10
+ def present?
11
+ if respond_to? :blank?
12
+ !blank?
13
+ else
14
+ !nil?
15
+ end
16
+ end
17
+
18
+ def method_missing(method, *args, &block)
19
+ if Object.const_defined?('Platform') && Platform.respond_to?(method)
20
+ Platform.send method, *args, &block
21
+ else
22
+ super
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,10 @@
1
+ class String
2
+ def blank?
3
+ self !~ /\S/
4
+ end
5
+
6
+ # todo: add ability to escape quotes inside the string
7
+ def quote
8
+ "'#{ self }'"
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ class Symbol
2
+ def quote
3
+ to_s.quote
4
+ end
5
+ end
@@ -1,3 +1,3 @@
1
1
  module CoreExtensions
2
- VERSION = "0.0.1"
2
+ VERSION = '0.0.2'
3
3
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: core_extensions
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Sarkis Karayan
@@ -10,7 +10,18 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
  date: 2012-09-13 00:00:00.000000000Z
13
- dependencies: []
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ version_requirements: &2056 !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ! '>='
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ none: false
22
+ requirement: *2056
23
+ prerelease: false
24
+ type: :runtime
14
25
  description: Ruby Core Object Extensions
15
26
  email:
16
27
  - skarayan@gmail.com
@@ -25,6 +36,14 @@ files:
25
36
  - Rakefile
26
37
  - core_extensions.gemspec
27
38
  - lib/core_extensions.rb
39
+ - lib/core_extensions/array.rb
40
+ - lib/core_extensions/float.rb
41
+ - lib/core_extensions/hash.rb
42
+ - lib/core_extensions/integer.rb
43
+ - lib/core_extensions/nil.rb
44
+ - lib/core_extensions/object.rb
45
+ - lib/core_extensions/string.rb
46
+ - lib/core_extensions/symbol.rb
28
47
  - lib/core_extensions/version.rb
29
48
  homepage: https://github.com/skarayan/core_extensions
30
49
  licenses: []