ruby_ext 0.4.25 → 0.5.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 (50) hide show
  1. data/Rakefile +2 -0
  2. data/lib/rake_ext/project.rb +19 -19
  3. data/lib/rake_ext.rb +18 -18
  4. data/lib/rspec_ext/xhtml.rb +6 -6
  5. data/lib/rspec_ext.rb +40 -25
  6. data/lib/ruby_ext/core/array.rb +7 -7
  7. data/lib/ruby_ext/core/basic_object.rb +1 -1
  8. data/lib/ruby_ext/core/deep_clone.rb +2 -2
  9. data/lib/ruby_ext/core/enumerable.rb +1 -1
  10. data/lib/ruby_ext/core/hash.rb +4 -4
  11. data/lib/ruby_ext/core/module.rb +20 -30
  12. data/lib/ruby_ext/core/multiple_inheritance.rb +24 -24
  13. data/lib/ruby_ext/core/must.rb +39 -39
  14. data/lib/ruby_ext/core/object.rb +15 -3
  15. data/lib/ruby_ext/core/open_object.rb +22 -20
  16. data/lib/ruby_ext/core/string.rb +19 -19
  17. data/lib/ruby_ext/core/symbol.rb +1 -13
  18. data/lib/ruby_ext/core.rb +4 -8
  19. data/lib/ruby_ext/more/callbacks.rb +172 -0
  20. data/lib/ruby_ext/more/declarative_cache.rb +20 -22
  21. data/lib/ruby_ext/more/miscellaneous.rb +1 -46
  22. data/lib/ruby_ext/more/{observable2.rb → observable.rb} +8 -8
  23. data/lib/ruby_ext/more/open_constructor.rb +10 -10
  24. data/lib/ruby_ext/more/tuple.rb +1 -1
  25. data/lib/ruby_ext/more.rb +5 -3
  26. data/lib/ruby_ext.rb +0 -3
  27. data/lib/yaml_fix.rb +2 -2
  28. data/readme.md +51 -50
  29. data/spec/core/deep_clone_spec.rb +8 -8
  30. data/spec/core/module_spec.rb +29 -36
  31. data/spec/core/multiple_inheritance_spec.rb +32 -32
  32. data/spec/core/must_spec.rb +6 -6
  33. data/spec/core/object_spec.rb +15 -0
  34. data/spec/core/open_object_spec.rb +6 -6
  35. data/spec/more/callbacks_spec.rb +155 -0
  36. data/spec/more/declarative_cache_spec.rb +33 -33
  37. data/spec/more/{observable2_spec.rb → observable_spec.rb} +7 -7
  38. data/spec/more/open_constructor_spec.rb +5 -5
  39. metadata +7 -15
  40. data/lib/ruby_ext/core/class.rb +0 -0
  41. data/lib/ruby_ext/core/file.rb +0 -23
  42. data/lib/ruby_ext/core/kernel.rb +0 -69
  43. data/lib/ruby_ext/core/miscellaneous.rb +0 -14
  44. data/lib/ruby_ext/more/synchronize.rb +0 -26
  45. data/spec/core/kernel_spec/TheNamespace/ClassA.rb +0 -7
  46. data/spec/core/kernel_spec/another_class.rb +0 -5
  47. data/spec/core/kernel_spec/the_namespace/class_b.rb +0 -11
  48. data/spec/core/kernel_spec.rb +0 -51
  49. data/spec/more/miscellaneous_spec.rb +0 -14
  50. data/spec/more/synchronize_spec.rb +0 -79
@@ -1,24 +1,24 @@
1
- #
1
+ #
2
2
  # Fix for ruby's broken include.
3
3
  # Included modules doesn't propagated to it's children.
4
- #
4
+ #
5
5
  # Test case:
6
6
  # module A; end
7
7
  # module B
8
8
  # include A
9
9
  # end
10
- #
10
+ #
11
11
  # module Plugin; end
12
12
  # A.send(:include, Plugin)
13
- #
13
+ #
14
14
  # p "Ancestors of A: " + A.ancestors.join(', ') # => "Ancestors of A: A, Plugin"
15
15
  # p "Ancestors of B: " + B.ancestors.join(', ') # => "Ancestors of B: B, A" << NO PLUGIN!
16
- #
16
+ #
17
17
  class Module
18
18
  def directly_included_by
19
19
  @directly_included_by ||= Set.new
20
20
  end
21
-
21
+
22
22
  def include2 mod
23
23
  # unless mod.directly_included_by.include? self
24
24
  mod.directly_included_by.add self
@@ -32,58 +32,58 @@ class Module
32
32
  end
33
33
 
34
34
 
35
- #
35
+ #
36
36
  # Inheritance
37
- #
37
+ #
38
38
  class Module
39
39
  def class_prototype
40
40
  unless @class_prototype
41
41
  ancestor = ancestors[1]
42
42
  if(
43
- !const_defined?(:ClassMethods) or
43
+ !const_defined?(:ClassMethods) or
44
44
  (
45
- const_defined?(:ClassMethods) and ancestor and ancestor.const_defined?(:ClassMethods) and
45
+ const_defined?(:ClassMethods) and ancestor and ancestor.const_defined?(:ClassMethods) and
46
46
  const_get(:ClassMethods) == ancestor.const_get(:ClassMethods)
47
47
  )
48
48
  )
49
- class_eval "module ClassMethods; end", __FILE__, __LINE__
50
- end
49
+ class_eval "module ClassMethods; end", __FILE__, __LINE__
50
+ end
51
51
  @class_prototype = const_get :ClassMethods
52
-
52
+
53
53
  (class << self; self end).include2 @class_prototype
54
54
  end
55
55
  @class_prototype
56
56
  end
57
-
57
+
58
58
  def class_methods &block
59
- if block
60
- class_prototype.class_eval &block
59
+ if block
60
+ class_prototype.class_eval &block
61
61
  extend class_prototype
62
62
  else
63
63
  class_prototype.instance_methods
64
64
  end
65
65
  end
66
-
66
+
67
67
  def inherited &b
68
68
  @inherited = b if b
69
69
  @inherited
70
70
  end
71
-
71
+
72
72
  def inherit *modules
73
73
  modules.each do |mod|
74
74
  # Instance Methods
75
75
  include2 mod
76
-
76
+
77
77
  # Class Methods
78
78
  unless self.class_prototype == mod.class_prototype
79
- if self.class == Module
80
- # p self => self.class_prototype, mod => mod.class_prototype
79
+ if self.class == Module
80
+ # p self => self.class_prototype, mod => mod.class_prototype
81
81
  class_prototype.include2 mod.class_prototype
82
- else
82
+ else
83
83
  (class << self; self end).include2 mod.class_prototype
84
- end
84
+ end
85
85
  end
86
-
86
+
87
87
  # callback
88
88
  # mod.inherited self if mod.respond_to? :inherited
89
89
  self.instance_eval &mod.inherited if mod.inherited
@@ -2,36 +2,36 @@ class AssertionError < RuntimeError; end
2
2
 
3
3
  class MustAssertions < BasicObject
4
4
  attr_reader :obj
5
-
6
- def initialize obj
5
+
6
+ def initialize obj
7
7
  @obj = obj
8
8
  end
9
-
9
+
10
10
  def == o
11
11
  @obj == o
12
12
  end
13
-
13
+
14
14
  def =~ o
15
15
  @obj =~ o
16
16
  end
17
-
17
+
18
18
  def never_called
19
19
  false
20
20
  end
21
-
21
+
22
22
  def nil
23
23
  @obj.equal? nil
24
24
  end
25
-
25
+
26
26
  def defined
27
27
  @obj != nil
28
28
  end
29
-
29
+
30
30
  def respond_to method
31
31
  @obj.respond_to? method
32
32
  end
33
-
34
- def a *args
33
+
34
+ def a *args
35
35
  if args.class == ::Array
36
36
  args.any?{|k| @obj.is_a? k}
37
37
  else
@@ -39,7 +39,7 @@ class MustAssertions < BasicObject
39
39
  end
40
40
  end
41
41
  alias_method :an, :a
42
-
42
+
43
43
  # def be klass = nil
44
44
  # if klass.class == Array
45
45
  # klass.any?{|k| @obj.respond_to :is?, k}
@@ -47,13 +47,13 @@ class MustAssertions < BasicObject
47
47
  # @obj.respond_to :is?, klass
48
48
  # end
49
49
  # end
50
-
50
+
51
51
  def include o
52
52
  @obj.include? o
53
53
  end
54
-
54
+
55
55
  def in *args
56
- if args.size == 1
56
+ if args.size == 1
57
57
  obj = args.first
58
58
  if obj.is_a?(::Array) or obj.is_a?(::Range)
59
59
  obj.include? @obj
@@ -73,61 +73,61 @@ class MustAssertions < BasicObject
73
73
  !@obj
74
74
  end
75
75
 
76
- def empty
76
+ def empty
77
77
  @obj.empty?
78
78
  end
79
-
79
+
80
80
  def blank
81
81
  @obj.blank?
82
82
  end
83
-
83
+
84
84
  def present
85
85
  !@obj.blank?
86
86
  end
87
-
87
+
88
88
  def > o
89
89
  @obj > o
90
90
  end
91
-
91
+
92
92
  def < o
93
93
  @obj < o
94
94
  end
95
-
95
+
96
96
  def >= o
97
97
  @obj >= o
98
98
  end
99
-
99
+
100
100
  def <= o
101
101
  @obj <= o
102
102
  end
103
-
103
+
104
104
  def exist *args
105
105
  @obj.exist? *args
106
106
  end
107
-
108
- def be
107
+
108
+ def be
109
109
  @prefix = 'be'
110
110
  self
111
111
  end
112
-
112
+
113
113
  def have
114
114
  @prefix = 'have'
115
115
  self
116
116
  end
117
-
117
+
118
118
  def self.assertions
119
119
  special = [:be, :have]
120
- public_instance_methods.select{|m| m !~ /^__/ and !special.include?(m)}
120
+ public_instance_methods.select{|m| m !~ /^__/ and !special.include?(m)}
121
121
  end
122
-
122
+
123
123
  protected
124
124
  # def method_missing m, *args, &b
125
125
  # raise ::RuntimeError, "Assertion '#{m}' is unknown!", caller[1..-1]
126
126
  # end
127
-
127
+
128
128
  def failed method, negative, *args
129
129
  stack = ::Object.send(:caller).sfilter('must.rb')
130
-
130
+
131
131
  unless args.empty?
132
132
  ::Object.send :raise, ::AssertionError, "
133
133
  ASSERTION FAILED (#{stack.first}):
@@ -147,18 +147,18 @@ class Must < MustAssertions
147
147
  desition = "_#{m}"
148
148
  alias_method desition, m
149
149
  define_method m do |*args|
150
- failed m, false, *args unless __send__(desition, *args)
150
+ failed m, false, *args unless __send__(desition, *args)
151
151
  @obj
152
152
  end
153
153
  end
154
-
154
+
155
155
  def inspect
156
156
  "<#Must>"
157
157
  end
158
158
  end
159
159
 
160
160
  class MustNot < MustAssertions
161
- assertions.each do |m|
161
+ assertions.each do |m|
162
162
  desition = "_#{m}".to_sym
163
163
  alias_method desition, m
164
164
  define_method m do |*args|
@@ -168,27 +168,27 @@ class MustNot < MustAssertions
168
168
  end
169
169
  end
170
170
 
171
- Object.class_eval do
171
+ Object.class_eval do
172
172
  def must
173
173
  Must.new(self)
174
174
  end
175
-
175
+
176
176
  def must_not
177
177
  MustNot.new(self)
178
178
  end
179
-
179
+
180
180
  def must_be
181
181
  must.be
182
182
  end
183
-
183
+
184
184
  def must_not_be
185
185
  must_not.be
186
186
  end
187
-
187
+
188
188
  # def must_have
189
189
  # must.have
190
190
  # end
191
- #
191
+ #
192
192
  # def must_not_have
193
193
  # must_not.have
194
194
  # end
@@ -1,7 +1,19 @@
1
1
  Object.class_eval do
2
- def self; self end
3
-
4
- public :extend
2
+ def self; self end
3
+
4
+ def metaclass &block
5
+ (class << self; self; end)
6
+ end
7
+ def metaclass_eval &block
8
+ metaclass.class_eval(&block)
9
+ self
10
+ end
11
+
12
+ def respond_to method, *args
13
+ respond_to?(method) ? send(method, *args) : nil
14
+ end
15
+
16
+ public :extend
5
17
  # def copy
6
18
  # Marshal.load(Marshal.dump self) # Stub
7
19
  # end
@@ -1,15 +1,13 @@
1
- raise "You shouldn't use OpenObject defined in facets, it's incopatible with this version!" if defined?(OpenObject)
2
-
3
- class OpenObject < Hash
1
+ class OpenObject < Hash
4
2
  #
5
3
  # delete methods
6
4
  #
7
5
  PUBLIC_METHODS = %w(
8
6
  as send each each_pair size is_a? clone dup empty? blank? present? merge merge! stringify_keys stringify_keys! symbolize_keys symbolize_keys! to_query
9
7
  ).collect{|m| m.to_sym}
10
- PUBLIC_METHODS_RE = /(^__|^object_|^must|^stub)/
8
+ PUBLIC_METHODS_RE = /(^__|^object_|^must|^stub)/
11
9
  protected(*public_instance_methods.select{|m| !PUBLIC_METHODS.include?(m) and m !~ PUBLIC_METHODS_RE})
12
-
10
+
13
11
  def inspect
14
12
  # "#<#{self.class}:#{self.object_id} #{super}>"
15
13
  "<#{super}>"
@@ -32,7 +30,7 @@ class OpenObject < Hash
32
30
  end
33
31
  end
34
32
  r
35
- end
33
+ end
36
34
  end
37
35
  alias_method :to_oo, :to_openobject
38
36
 
@@ -46,7 +44,7 @@ class OpenObject < Hash
46
44
  # d.send(:update, other)
47
45
  # d
48
46
  end
49
-
47
+
50
48
  def merge! other
51
49
  other.each{|k, v| self[k] = v}
52
50
  end
@@ -62,7 +60,7 @@ class OpenObject < Hash
62
60
 
63
61
  def == other
64
62
  return false unless other.respond_to?(:each) and other.respond_to?(:size) and other.respond_to?(:[]) and self.size == other.size
65
- other.each do |k, v|
63
+ other.each do |k, v|
66
64
  return false unless self[k] == v
67
65
  end
68
66
  true
@@ -75,11 +73,11 @@ class OpenObject < Hash
75
73
  def [] k
76
74
  super k.to_sym
77
75
  end
78
-
76
+
79
77
  def include? k
80
78
  super k.to_sym
81
79
  end
82
-
80
+
83
81
  def to_hash deep = false
84
82
  unless deep
85
83
  {}.update(self)
@@ -96,22 +94,22 @@ class OpenObject < Hash
96
94
  end
97
95
  end
98
96
  alias_method :to_h, :to_hash
99
-
97
+
100
98
  def to_json *args
101
99
  to_hash.to_json *args
102
100
  end
103
-
101
+
104
102
  # hack to works well with RSpec
105
103
  def should; super end
106
104
  def should_not; super end
107
-
105
+
108
106
  def respond_to? m
109
107
  true
110
108
  end
111
-
109
+
112
110
  def self.initialize_from hash, deep = false
113
111
  unless deep
114
- ::OpenObject.new.update hash
112
+ ::OpenObject.new.update hash
115
113
  else
116
114
  r = ::OpenObject.new
117
115
  hash.each do |k, v|
@@ -124,13 +122,17 @@ class OpenObject < Hash
124
122
  r
125
123
  end
126
124
  end
125
+
126
+ # support :extract_options for OpenObject (Rails integration)
127
+ def extractable_options?; true end
127
128
 
128
129
  protected
129
- def method_missing m, arg = nil, &block
130
+ def method_missing m, arg = nil, &block
130
131
  type = m[-1,1]
131
132
  if type == '='
132
133
  self[m[0..-2]] = arg
133
- elsif type == '!'
134
+ elsif type == '!'
135
+ warn 'deprecated'
134
136
  self[m[0..-2]]
135
137
  elsif type == '?'
136
138
  !self[m[0..-2]].blank?
@@ -141,9 +143,9 @@ class OpenObject < Hash
141
143
  end
142
144
 
143
145
 
144
- #
146
+ #
145
147
  # Core Extensions
146
- #
148
+ #
147
149
  class NilClass
148
150
  def to_openobject deep = false
149
151
  OpenObject.new
@@ -156,7 +158,7 @@ class Hash
156
158
  OpenObject.initialize_from self, deep
157
159
  end
158
160
  alias_method :to_oo, :to_openobject
159
-
161
+
160
162
  alias_method :oo_eql, :==
161
163
  def == other
162
164
  true if self.equal? other
@@ -2,40 +2,40 @@ String.class_eval do
2
2
  def dirname
3
3
  File.expand_path(File.dirname(self))
4
4
  end
5
-
5
+
6
6
  def expand_path
7
7
  File.expand_path(self)
8
8
  end
9
-
9
+
10
10
  # def to_reader
11
11
  # self.to_sym
12
12
  # end
13
-
13
+
14
14
  # def to_writer
15
15
  # "#{self}=".to_sym
16
16
  # end
17
-
17
+
18
18
  # def to_iv
19
19
  # "@#{self}"
20
20
  # end
21
-
21
+
22
22
  def to_a
23
23
  [self]
24
24
  end
25
-
26
- def interpolate binding
27
- binding.must_be.a Binding
28
- return gsub(/\#\{.+?\}/) do |term|
29
- identifier = term.slice(2 .. term.size-2)
30
- binding.eval identifier
31
- end
32
- end
33
-
25
+
26
+ # def interpolate binding
27
+ # binding.must_be.a Binding
28
+ # return gsub(/\#\{.+?\}/) do |term|
29
+ # identifier = term.slice(2 .. term.size-2)
30
+ # binding.eval identifier
31
+ # end
32
+ # end
33
+
34
34
  def self.secure_token
35
35
  original = [Time.now, (1..10).map{ rand.to_s }]
36
36
  Digest::SHA1.hexdigest(original.flatten.join('--'))
37
37
  end
38
-
38
+
39
39
  def underscore
40
40
  word = self.dup
41
41
  word.gsub!(/::/, '/')
@@ -45,7 +45,7 @@ String.class_eval do
45
45
  word.downcase!
46
46
  word
47
47
  end
48
-
48
+
49
49
  def camelize first_letter_in_uppercase = true
50
50
  if first_letter_in_uppercase
51
51
  gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
@@ -53,7 +53,7 @@ String.class_eval do
53
53
  self[0].chr.downcase + camelize(lower_case_and_underscored_word)[1..-1]
54
54
  end
55
55
  end
56
-
56
+
57
57
  def constantize
58
58
  names = self.split('::')
59
59
  names.shift if names.empty? || names.first.empty?
@@ -64,7 +64,7 @@ String.class_eval do
64
64
  end
65
65
  constant
66
66
  end
67
-
67
+
68
68
  class String
69
69
  def substitute(*args)
70
70
  gsub(*args){yield Regexp.last_match.captures}
@@ -74,6 +74,6 @@ String.class_eval do
74
74
  gsub!(*args){yield Regexp.last_match.captures}
75
75
  end
76
76
  end
77
-
77
+
78
78
  alias_method :blank?, :empty?
79
79
  end
@@ -2,22 +2,10 @@ Symbol.class_eval do
2
2
  def <=> other
3
3
  self.to_s <=> other.to_s
4
4
  end
5
-
6
- # def to_reader
7
- # self
8
- # end
9
- #
10
- # def to_writer
11
- # "#{self}=".to_sym
12
- # end
13
- #
14
- # def to_iv
15
- # "@#{self}"
16
- # end
17
5
 
18
6
  def + other
19
7
  (self.to_s + other.to_s).to_sym
20
8
  end
21
-
9
+
22
10
  def blank?; to_s.blank? end
23
11
  end
data/lib/ruby_ext/core.rb CHANGED
@@ -6,20 +6,16 @@ require 'ruby_ext/fixes'
6
6
  %w(
7
7
  basic_object
8
8
  nil_class
9
- file
10
9
  enumerable
11
10
  array
12
11
  hash
13
- kernel
14
- object
12
+ object
15
13
  module
16
- not_defined
17
- class
14
+ not_defined
18
15
  string
19
- symbol
16
+ symbol
20
17
  must
21
18
  open_object
22
19
  deep_clone
23
20
  multiple_inheritance
24
- miscellaneous
25
- ).each{|f| require "ruby_ext/core/#{f}"}
21
+ ).each{|f| require "ruby_ext/core/#{f}"}