sinarey_support 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (28) hide show
  1. checksums.yaml +7 -0
  2. data/lib/sinarey_support/core_ext/array/access.rb +19 -0
  3. data/lib/sinarey_support/core_ext/array/extract_options.rb +15 -0
  4. data/lib/sinarey_support/core_ext/array/grouping.rb +49 -0
  5. data/lib/sinarey_support/core_ext/array/prepend_and_append.rb +6 -0
  6. data/lib/sinarey_support/core_ext/array/wrap.rb +12 -0
  7. data/lib/sinarey_support/core_ext/array.rb +5 -0
  8. data/lib/sinarey_support/core_ext/hash/deep_merge.rb +18 -0
  9. data/lib/sinarey_support/core_ext/hash/except.rb +11 -0
  10. data/lib/sinarey_support/core_ext/hash/keys.rb +67 -0
  11. data/lib/sinarey_support/core_ext/hash/reverse_merge.rb +11 -0
  12. data/lib/sinarey_support/core_ext/hash/slice.rb +19 -0
  13. data/lib/sinarey_support/core_ext/hash.rb +5 -0
  14. data/lib/sinarey_support/core_ext/object/acts_like.rb +5 -0
  15. data/lib/sinarey_support/core_ext/object/blank.rb +53 -0
  16. data/lib/sinarey_support/core_ext/object/to_param.rb +37 -0
  17. data/lib/sinarey_support/core_ext/object/to_query.rb +19 -0
  18. data/lib/sinarey_support/core_ext/object/try.rb +27 -0
  19. data/lib/sinarey_support/core_ext/object.rb +5 -0
  20. data/lib/sinarey_support/core_ext/string/access.rb +34 -0
  21. data/lib/sinarey_support/core_ext/string/behavior.rb +5 -0
  22. data/lib/sinarey_support/core_ext/string/exclude.rb +5 -0
  23. data/lib/sinarey_support/core_ext/string/filters.rb +28 -0
  24. data/lib/sinarey_support/core_ext/string/indent.rb +13 -0
  25. data/lib/sinarey_support/core_ext/string.rb +5 -0
  26. data/lib/sinarey_support/core_ext/struct.rb +6 -0
  27. data/lib/sinarey_support.rb +5 -0
  28. metadata +69 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4b1bb6ef862c14de85d7d67558e4ce1ee5432b76
4
+ data.tar.gz: f7cdef605e27116e65f5f87ddaec404758827afc
5
+ SHA512:
6
+ metadata.gz: 2bc65ae4d59af13eb5c7e5351ee3ea8baf629cf0fc0b473d472fc85a1a064fa765cf6570ed472de5501b568496131a6092d330850822cbfc44ab5b8ffdbbf7d4
7
+ data.tar.gz: 2ec5590ffcda475af839908addb45fbc57d9a7c69ded6a14b7c6495115d4b1707243f8f87cd2f8ffeb722b95977998d57e5025dee23fb5fd8c18cca60db410b6
@@ -0,0 +1,19 @@
1
+ class Array
2
+
3
+ def second
4
+ self[1]
5
+ end
6
+
7
+ def third
8
+ self[2]
9
+ end
10
+
11
+ def fourth
12
+ self[3]
13
+ end
14
+
15
+ def fifth
16
+ self[4]
17
+ end
18
+
19
+ end
@@ -0,0 +1,15 @@
1
+ class Hash
2
+ def extractable_options?
3
+ instance_of?(Hash)
4
+ end
5
+ end
6
+
7
+ class Array
8
+ def extract_options!
9
+ if last.is_a?(Hash) && last.extractable_options?
10
+ pop
11
+ else
12
+ {}
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,49 @@
1
+ class Array
2
+
3
+ def in_groups_of(number, fill_with = nil)
4
+ if fill_with == false
5
+ collection = self
6
+ else
7
+ padding = (number - size % number) % number
8
+ collection = dup.concat([fill_with] * padding)
9
+ end
10
+ if block_given?
11
+ collection.each_slice(number) { |slice| yield(slice) }
12
+ else
13
+ groups = []
14
+ collection.each_slice(number) { |group| groups << group }
15
+ groups
16
+ end
17
+ end
18
+
19
+ def in_groups(number, fill_with = nil)
20
+ division = size.div number
21
+ modulo = size % number
22
+ groups = []
23
+ start = 0
24
+ number.times do |index|
25
+ length = division + (modulo > 0 && modulo > index ? 1 : 0)
26
+ groups << last_group = slice(start, length)
27
+ last_group << fill_with if fill_with != false &&
28
+ modulo > 0 && length == division
29
+ start += length
30
+ end
31
+ if block_given?
32
+ groups.each { |g| yield(g) }
33
+ else
34
+ groups
35
+ end
36
+ end
37
+
38
+ def split(value = nil, &block)
39
+ inject([[]]) do |results, element|
40
+ if block && block.call(element) || value == element
41
+ results << []
42
+ else
43
+ results.last << element
44
+ end
45
+ results
46
+ end
47
+ end
48
+
49
+ end
@@ -0,0 +1,6 @@
1
+ class Array
2
+
3
+ alias_method :append, :<<
4
+
5
+ alias_method :prepend, :unshift
6
+ end
@@ -0,0 +1,12 @@
1
+ class Array
2
+ def self.wrap(object)
3
+ if object.nil?
4
+ []
5
+ elsif object.respond_to?(:to_ary)
6
+ object.to_ary || [object]
7
+ else
8
+ [object]
9
+ end
10
+ end
11
+ end
12
+
@@ -0,0 +1,5 @@
1
+ require 'sinarey_support/core_ext/array/wrap'
2
+ require 'sinarey_support/core_ext/array/access'
3
+ require 'sinarey_support/core_ext/array/extract_options'
4
+ require 'sinarey_support/core_ext/array/grouping'
5
+ require 'sinarey_support/core_ext/array/prepend_and_append'
@@ -0,0 +1,18 @@
1
+ class Hash
2
+
3
+ def deep_merge(other_hash, &block)
4
+ dup.deep_merge!(other_hash, &block)
5
+ end
6
+
7
+ def deep_merge!(other_hash, &block)
8
+ other_hash.each_pair do |k,v|
9
+ tv = self[k]
10
+ if tv.is_a?(Hash) && v.is_a?(Hash)
11
+ self[k] = tv.deep_merge(v, &block)
12
+ else
13
+ self[k] = block && tv ? block.call(k, tv, v) : v
14
+ end
15
+ end
16
+ self
17
+ end
18
+ end
@@ -0,0 +1,11 @@
1
+ class Hash
2
+
3
+ def except(*keys)
4
+ dup.except!(*keys)
5
+ end
6
+
7
+ def except!(*keys)
8
+ keys.each { |key| delete(key) }
9
+ self
10
+ end
11
+ end
@@ -0,0 +1,67 @@
1
+ class Hash
2
+
3
+ def transform_keys
4
+ result = {}
5
+ each_key do |key|
6
+ result[yield(key)] = self[key]
7
+ end
8
+ result
9
+ end
10
+
11
+ def transform_keys!
12
+ keys.each do |key|
13
+ self[yield(key)] = delete(key)
14
+ end
15
+ self
16
+ end
17
+
18
+ def stringify_keys
19
+ transform_keys{ |key| key.to_s }
20
+ end
21
+
22
+ def stringify_keys!
23
+ transform_keys!{ |key| key.to_s }
24
+ end
25
+
26
+ def symbolize_keys
27
+ transform_keys{ |key| key.to_sym rescue key }
28
+ end
29
+ alias_method :to_options, :symbolize_keys
30
+
31
+ def symbolize_keys!
32
+ transform_keys!{ |key| key.to_sym rescue key }
33
+ end
34
+ alias_method :to_options!, :symbolize_keys!
35
+
36
+ def deep_transform_keys(&block)
37
+ result = {}
38
+ each do |key, value|
39
+ result[yield(key)] = value.is_a?(Hash) ? value.deep_transform_keys(&block) : value
40
+ end
41
+ result
42
+ end
43
+
44
+ def deep_transform_keys!(&block)
45
+ keys.each do |key|
46
+ value = delete(key)
47
+ self[yield(key)] = value.is_a?(Hash) ? value.deep_transform_keys!(&block) : value
48
+ end
49
+ self
50
+ end
51
+
52
+ def deep_stringify_keys
53
+ deep_transform_keys{ |key| key.to_s }
54
+ end
55
+
56
+ def deep_stringify_keys!
57
+ deep_transform_keys!{ |key| key.to_s }
58
+ end
59
+
60
+ def deep_symbolize_keys
61
+ deep_transform_keys{ |key| key.to_sym rescue key }
62
+ end
63
+
64
+ def deep_symbolize_keys!
65
+ deep_transform_keys!{ |key| key.to_sym rescue key }
66
+ end
67
+ end
@@ -0,0 +1,11 @@
1
+ class Hash
2
+
3
+ def reverse_merge(other_hash)
4
+ other_hash.merge(self)
5
+ end
6
+
7
+ def reverse_merge!(other_hash)
8
+ merge!( other_hash ){|key,left,right| left }
9
+ end
10
+ alias_method :reverse_update, :reverse_merge!
11
+ end
@@ -0,0 +1,19 @@
1
+ class Hash
2
+
3
+ def slice(*keys)
4
+ keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true)
5
+ keys.each_with_object(self.class.new) { |k, hash| hash[k] = self[k] if has_key?(k) }
6
+ end
7
+
8
+ def slice!(*keys)
9
+ keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true)
10
+ omit = slice(*self.keys - keys)
11
+ hash = slice(*keys)
12
+ replace(hash)
13
+ omit
14
+ end
15
+
16
+ def extract!(*keys)
17
+ keys.each_with_object(self.class.new) { |key, result| result[key] = delete(key) if has_key?(key) }
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ require 'sinarey_support/core_ext/hash/deep_merge'
2
+ require 'sinarey_support/core_ext/hash/except'
3
+ require 'sinarey_support/core_ext/hash/keys'
4
+ require 'sinarey_support/core_ext/hash/reverse_merge'
5
+ require 'sinarey_support/core_ext/hash/slice'
@@ -0,0 +1,5 @@
1
+ class Object
2
+ def acts_like?(duck)
3
+ respond_to? :"acts_like_#{duck}?"
4
+ end
5
+ end
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ class Object
4
+ def blank?
5
+ respond_to?(:empty?) ? empty? : !self
6
+ end
7
+
8
+ def present?
9
+ !blank?
10
+ end
11
+
12
+ def presence
13
+ self if present?
14
+ end
15
+ end
16
+
17
+ class NilClass
18
+ def blank?
19
+ true
20
+ end
21
+ end
22
+
23
+ class FalseClass
24
+ def blank?
25
+ true
26
+ end
27
+ end
28
+
29
+ class TrueClass
30
+ def blank?
31
+ false
32
+ end
33
+ end
34
+
35
+ class Array
36
+ alias_method :blank?, :empty?
37
+ end
38
+
39
+ class Hash
40
+ alias_method :blank?, :empty?
41
+ end
42
+
43
+ class String
44
+ def blank?
45
+ self !~ /[^[:space:]]/
46
+ end
47
+ end
48
+
49
+ class Numeric
50
+ def blank?
51
+ false
52
+ end
53
+ end
@@ -0,0 +1,37 @@
1
+ class Object
2
+ def to_param
3
+ to_s
4
+ end
5
+ end
6
+
7
+ class NilClass
8
+ def to_param
9
+ self
10
+ end
11
+ end
12
+
13
+ class TrueClass
14
+ def to_param
15
+ self
16
+ end
17
+ end
18
+
19
+ class FalseClass
20
+ def to_param
21
+ self
22
+ end
23
+ end
24
+
25
+ class Array
26
+ def to_param
27
+ collect { |e| e.to_param }.join '/'
28
+ end
29
+ end
30
+
31
+ class Hash
32
+ def to_param(namespace = nil)
33
+ collect do |key, value|
34
+ value.to_query(namespace ? "#{namespace}[#{key}]" : key)
35
+ end.sort * '&'
36
+ end
37
+ end
@@ -0,0 +1,19 @@
1
+ require 'sinarey_support/core_ext/object/to_param'
2
+
3
+ class Object
4
+ def to_query(key)
5
+ require 'cgi' unless defined?(CGI) && defined?(CGI::escape)
6
+ "#{CGI.escape(key.to_param)}=#{CGI.escape(to_param.to_s)}"
7
+ end
8
+ end
9
+
10
+ class Array
11
+ def to_query(key)
12
+ prefix = "#{key}[]"
13
+ collect { |value| value.to_query(prefix) }.join '&'
14
+ end
15
+ end
16
+
17
+ class Hash
18
+ alias_method :to_query, :to_param
19
+ end
@@ -0,0 +1,27 @@
1
+ class Object
2
+ def try(*a, &b)
3
+ if a.empty? && block_given?
4
+ yield self
5
+ else
6
+ public_send(*a, &b) if respond_to?(a.first)
7
+ end
8
+ end
9
+
10
+ def try!(*a, &b)
11
+ if a.empty? && block_given?
12
+ yield self
13
+ else
14
+ public_send(*a, &b)
15
+ end
16
+ end
17
+ end
18
+
19
+ class NilClass
20
+ def try(*args)
21
+ nil
22
+ end
23
+
24
+ def try!(*args)
25
+ nil
26
+ end
27
+ end
@@ -0,0 +1,5 @@
1
+ require 'sinarey_support/core_ext/object/acts_like'
2
+ require 'sinarey_support/core_ext/object/blank'
3
+ require 'sinarey_support/core_ext/object/to_param'
4
+ require 'sinarey_support/core_ext/object/to_query'
5
+ require 'sinarey_support/core_ext/object/try'
@@ -0,0 +1,34 @@
1
+ class String
2
+
3
+ def at(position)
4
+ self[position]
5
+ end
6
+
7
+ def from(position)
8
+ self[position..-1]
9
+ end
10
+
11
+ def to(position)
12
+ self[0..position]
13
+ end
14
+
15
+ def first(limit = 1)
16
+ if limit == 0
17
+ ''
18
+ elsif limit >= size
19
+ self
20
+ else
21
+ to(limit - 1)
22
+ end
23
+ end
24
+
25
+ def last(limit = 1)
26
+ if limit == 0
27
+ ''
28
+ elsif limit >= size
29
+ self
30
+ else
31
+ from(-limit)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,5 @@
1
+ class String
2
+ def acts_like_string?
3
+ true
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class String
2
+ def exclude?(string)
3
+ !include?(string)
4
+ end
5
+ end
@@ -0,0 +1,28 @@
1
+ class String
2
+
3
+ def squish
4
+ dup.squish!
5
+ end
6
+
7
+ def squish!
8
+ gsub!(/\A[[:space:]]+/, '')
9
+ gsub!(/[[:space:]]+\z/, '')
10
+ gsub!(/[[:space:]]+/, ' ')
11
+ self
12
+ end
13
+
14
+ def truncate(truncate_at, options = {})
15
+ return dup unless length > truncate_at
16
+
17
+ options[:omission] ||= '...'
18
+ length_with_room_for_omission = truncate_at - options[:omission].length
19
+ stop = \
20
+ if options[:separator]
21
+ rindex(options[:separator], length_with_room_for_omission) || length_with_room_for_omission
22
+ else
23
+ length_with_room_for_omission
24
+ end
25
+
26
+ "#{self[0...stop]}#{options[:omission]}"
27
+ end
28
+ end
@@ -0,0 +1,13 @@
1
+ class String
2
+
3
+ def indent!(amount, indent_string=nil, indent_empty_lines=false)
4
+ indent_string = indent_string || self[/^[ \t]/] || ' '
5
+ re = indent_empty_lines ? /^/ : /^(?!$)/
6
+ gsub!(re, indent_string * amount)
7
+ end
8
+
9
+ def indent(amount, indent_string=nil, indent_empty_lines=false)
10
+ dup.tap {|_| _.indent!(amount, indent_string, indent_empty_lines)}
11
+ end
12
+
13
+ end
@@ -0,0 +1,5 @@
1
+ require 'sinarey_support/core_ext/string/access'
2
+ require 'sinarey_support/core_ext/string/behavior'
3
+ require 'sinarey_support/core_ext/string/exclude'
4
+ require 'sinarey_support/core_ext/string/filters'
5
+ require 'sinarey_support/core_ext/string/indent'
@@ -0,0 +1,6 @@
1
+ # Backport of Struct#to_h from Ruby 2.0
2
+ class Struct # :nodoc:
3
+ def to_h
4
+ Hash[members.zip(values)]
5
+ end
6
+ end unless Struct.instance_methods.include?(:to_h)
@@ -0,0 +1,5 @@
1
+ require 'sinarey_support/core_ext/string'
2
+ require 'sinarey_support/core_ext/array'
3
+ require 'sinarey_support/core_ext/hash'
4
+ require 'sinarey_support/core_ext/object'
5
+ require 'sinarey_support/core_ext/struct'
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinarey_support
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jeffrey Xu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: ''
14
+ email: jeffrey6052@163.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/sinarey_support/core_ext/array/access.rb
20
+ - lib/sinarey_support/core_ext/array/extract_options.rb
21
+ - lib/sinarey_support/core_ext/array/grouping.rb
22
+ - lib/sinarey_support/core_ext/array/prepend_and_append.rb
23
+ - lib/sinarey_support/core_ext/array/wrap.rb
24
+ - lib/sinarey_support/core_ext/hash/deep_merge.rb
25
+ - lib/sinarey_support/core_ext/hash/except.rb
26
+ - lib/sinarey_support/core_ext/hash/keys.rb
27
+ - lib/sinarey_support/core_ext/hash/reverse_merge.rb
28
+ - lib/sinarey_support/core_ext/hash/slice.rb
29
+ - lib/sinarey_support/core_ext/object/acts_like.rb
30
+ - lib/sinarey_support/core_ext/object/blank.rb
31
+ - lib/sinarey_support/core_ext/object/to_param.rb
32
+ - lib/sinarey_support/core_ext/object/to_query.rb
33
+ - lib/sinarey_support/core_ext/object/try.rb
34
+ - lib/sinarey_support/core_ext/string/access.rb
35
+ - lib/sinarey_support/core_ext/string/behavior.rb
36
+ - lib/sinarey_support/core_ext/string/exclude.rb
37
+ - lib/sinarey_support/core_ext/string/filters.rb
38
+ - lib/sinarey_support/core_ext/string/indent.rb
39
+ - lib/sinarey_support/core_ext/array.rb
40
+ - lib/sinarey_support/core_ext/hash.rb
41
+ - lib/sinarey_support/core_ext/object.rb
42
+ - lib/sinarey_support/core_ext/string.rb
43
+ - lib/sinarey_support/core_ext/struct.rb
44
+ - lib/sinarey_support.rb
45
+ homepage: http://rubygems.org/gems/sinarey_support
46
+ licenses:
47
+ - MIT
48
+ metadata: {}
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project:
65
+ rubygems_version: 2.0.14
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: sinarey_support!
69
+ test_files: []