core_utilities 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,6 +1,118 @@
1
1
  = CoreUtilities
2
2
 
3
- This project rocks and uses MIT-LICENSE.
3
+ Ruby-level extensions, utilities, convenience methods that I've found I'm constantly using in all my projects.
4
+
5
+ == Usage:
6
+
7
+ gem 'core_utilities', :git => "git://github.com/caleon/core_utilities.git"
8
+
9
+ == Selected Examples:
10
+
11
+ === Object
12
+
13
+ ==== Object#as
14
+
15
+ [ :donkey, { :class => 'list-item' } ].as { |arg, opts| link_to(arg, opts) }
16
+
17
+ ==== Object#is_included_in?
18
+
19
+ User.find(1).class.is_included_in?(User, Article, Authentication)
20
+
21
+ or alternatively:
22
+
23
+ @user.is_one_kind_of?(User, Article, Authentication)
24
+
25
+ which has the same effect as:
26
+
27
+ @user.is_one_kind_of?([User, Article, Authentication])
28
+
29
+ by virtue of:
30
+
31
+ === Array
32
+
33
+ ==== Array#flatten_splat!
34
+
35
+ #flatten_splat is for the common pattern of writing a method whose arguments are prepended by the splat operator, like the following:
36
+
37
+ def find(*types)
38
+ types = types.first if types.size == 1 and types.first.is_a?(Array)
39
+ # ...
40
+ end
41
+
42
+ ... which can now be written as:
43
+
44
+ def find(*types)
45
+ types.flatten_splat!
46
+ end
47
+
48
+ ==== array/base, array/filters
49
+
50
+ These contain further convenience methods, none of them really novel or anything.
51
+
52
+ ==== Array#extract_options(!)
53
+
54
+ Extending the behavior introduced by ActiveSupport, this one goes further to define Array#extract_options as the non-destructive analog, and also introduces Array#extract_options_with_merge. Previously you may have written:
55
+
56
+ def find(*args)
57
+ opts = args.extract_options!
58
+ opts[:user_id] = self.article.user_id
59
+ opts[:found_at] = Time.now
60
+ end
61
+
62
+ but now you can write:
63
+
64
+ def find(*args)
65
+ opts = args.extract_options_with_merge(:user_id => self.article.user_id, :found_at => Time.now)
66
+ end
67
+
68
+ Note: The inclusion of this gem makes the *_with_merge methods the default behavior for extract_options(!) via alias_method_chain.
69
+
70
+ ==== Array#merge_options(!)
71
+
72
+ If you find yourself extracting options as above only to re-inject that hash back into the original array to pass the arguments to a helper method (after some merging of the options hash), you may consider utilizing #merge_options:
73
+
74
+ def find(*args)
75
+ find_helper(*args.merge_options(:user_id => self.article.user_id, :found_at => Time.now))
76
+ end
77
+
78
+ ==== Array #arguments_and_options (a.k.a. #args_and_opts)
79
+
80
+ The above Array methods lead to this handy method whose utility, in essence, is in its shortening a common Ruby pattern into a single line. Previously you may have written:
81
+
82
+ def find(*arguments)
83
+ mandatory_opts = { :user_id => self.article.user_id, :found_at => Time.now }
84
+
85
+ opts = arguments.extract_options!
86
+ an_id = arguments.shift
87
+
88
+ opts.merge!(mandatory_opts)
89
+ end
90
+
91
+ which, with the help of #args_and_opts (and the help of of #method_missing for extending its functionality) you can write:
92
+
93
+ def find(*arguments)
94
+ an_id, opts = arguments.args_with_shift_and_opts_with_merge!(:uer_id => self.article.user_id, :found_at => Time.now)
95
+ end
96
+
97
+ Basically the format of the method call is arg(ument)?s(_with_[ARGMETHOD])?_and_opt(ion)?s(_with_[OPTSMETHOD])?!, where the ARGMETHOD is the name of the method which applies to the "args" part of the array, and OPTSMETHOD would be the name of the method which is called on the "opts" part of the array.
98
+
99
+ Additionally, arguments can be passed to #args_and_opts, but as of now, in the event that both ARGMETHOD and OPTSMETHOD are defined, the arguments will be passed to the OPTSMETHOD only. Some effort was made to utilize arity to determine where the arguments should be passed to, but to my recollection this is still an unfinished aspect.
100
+
101
+
102
+ === Enumerable
103
+
104
+ Has #map_select and #map_detect.
105
+
106
+ === Set
107
+
108
+ Has #not_subset?(set)
109
+
110
+ === Hash
111
+
112
+ Has #append_value which is similar to merging hashes, but in cases where you don't want to override the value of one of the hashes and would rather JOIN the values corresponding to the same key, (for example, wanting to add class names in a DOM element in Rails rather than overriding the original { :class => 'className' } hash):
113
+
114
+ { :id => "user_1-comments", :class => "user-comments" }.append_value!(:class, 'comments')
115
+ # => { :id => "user_1-comments", :class => "user-comments comments" }
4
116
 
5
117
  == Additional information
6
118
 
@@ -1,4 +1,5 @@
1
1
  require 'active_support'
2
+ require 'active_support/core_ext'
2
3
  require 'core_utilities/core_ext'
3
4
 
4
5
  module CoreUtilities
@@ -1,7 +1,4 @@
1
- %w(object enumerable set array hash).each do |file|
2
- require File.expand_path("../core_ext/#{file}", __FILE__)
3
- end
4
-
1
+ require 'core_utilities/core_ext/class'
5
2
  require 'core_utilities/core_ext/object'
6
3
  require 'core_utilities/core_ext/enumerable'
7
4
  require 'core_utilities/core_ext/set'
@@ -0,0 +1,37 @@
1
+ require 'active_support/core_ext/class/attribute'
2
+ require 'active_support/core_ext/module/aliasing'
3
+
4
+ class Class
5
+ # Purpose is to be able to define a default value for a class_attribute when one isn't initially set.
6
+ def class_attribute_with_default(*attrs) # Actually, this IS being used currently by gems in lib/
7
+ hash = attrs.last.is_a?(Hash) ? attrs.pop : {}
8
+ my_default = hash.delete(:default)
9
+ instance_writer = hash.blank? || hash[:instance_writer]
10
+
11
+ attrs.each do |name| # By the way, FIXME: i think this should be broken because if i want to use a string as a default, that will not be reflected.
12
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
13
+ def self.#{name}() #{my_default || 'nil'} end
14
+ def self.#{name}?() !!#{name} end
15
+
16
+ def self.#{name}=(val)
17
+ singleton_class.class_eval do
18
+ remove_possible_method(:#{name})
19
+ define_method(:#{name}) { val }
20
+ end
21
+ val
22
+ end
23
+
24
+ def #{name}
25
+ defined?(@#{name}) ? @#{name} : singleton_class.#{name}
26
+ end
27
+
28
+ def #{name}?
29
+ !!#{name}
30
+ end
31
+ RUBY
32
+
33
+ attr_writer name if instance_writer
34
+ end
35
+ end
36
+ alias_method_chain :class_attribute, :default
37
+ end
@@ -1,3 +1,3 @@
1
1
  module CoreUtilities
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: core_utilities
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-16 00:00:00.000000000Z
12
+ date: 2011-11-17 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &70141262027680 !ruby/object:Gem::Requirement
16
+ requirement: &70095083659380 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.1.1
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70141262027680
24
+ version_requirements: *70095083659380
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activesupport
27
- requirement: &70141262025480 !ruby/object:Gem::Requirement
27
+ requirement: &70095083658880 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 3.1.1
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70141262025480
35
+ version_requirements: *70095083658880
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: sqlite3
38
- requirement: &70141262024300 !ruby/object:Gem::Requirement
38
+ requirement: &70095083658500 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70141262024300
46
+ version_requirements: *70095083658500
47
47
  description: Core extensions with lots of handy methods
48
48
  email:
49
49
  - caleon@gmail.com
@@ -58,6 +58,7 @@ files:
58
58
  - lib/core_utilities/core_ext/array/flatten_splat.rb
59
59
  - lib/core_utilities/core_ext/array/merge_options.rb
60
60
  - lib/core_utilities/core_ext/array.rb
61
+ - lib/core_utilities/core_ext/class.rb
61
62
  - lib/core_utilities/core_ext/enumerable.rb
62
63
  - lib/core_utilities/core_ext/hash.rb
63
64
  - lib/core_utilities/core_ext/object.rb