jactive_support 1.0.0-universal-java-1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (133) hide show
  1. data/.gitignore +4 -0
  2. data/.rvmrc +63 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +20 -0
  5. data/Rakefile +45 -0
  6. data/jactive_support.gemspec +25 -0
  7. data/lib/jactive_support/constantize.rb +19 -0
  8. data/lib/jactive_support/core_ext/enum.rb +43 -0
  9. data/lib/jactive_support/core_ext/hash/assert_valid_params.rb +19 -0
  10. data/lib/jactive_support/core_ext/hash/with_keys_values.rb +9 -0
  11. data/lib/jactive_support/core_ext/hash.rb +2 -0
  12. data/lib/jactive_support/core_ext/locale.rb +28 -0
  13. data/lib/jactive_support/core_ext/module.rb +9 -0
  14. data/lib/jactive_support/core_ext/numeric.rb +7 -0
  15. data/lib/jactive_support/core_ext/ordering.rb +17 -0
  16. data/lib/jactive_support/core_ext/to_java.rb +9 -0
  17. data/lib/jactive_support/core_ext/to_java_date.rb +25 -0
  18. data/lib/jactive_support/core_ext/to_java_list.rb +11 -0
  19. data/lib/jactive_support/core_ext/to_java_map.rb +11 -0
  20. data/lib/jactive_support/core_ext.rb +4 -0
  21. data/lib/jactive_support/java_ext/date/calculations.rb +43 -0
  22. data/lib/jactive_support/java_ext/date/conversions.rb +135 -0
  23. data/lib/jactive_support/java_ext/date.rb +37 -0
  24. data/lib/jactive_support/java_ext/enum.rb +19 -0
  25. data/lib/jactive_support/java_ext/iterable.rb +74 -0
  26. data/lib/jactive_support/java_ext/iterator.rb +8 -0
  27. data/lib/jactive_support/java_ext/list.rb +65 -0
  28. data/lib/jactive_support/java_ext/list_iterator.rb +8 -0
  29. data/lib/jactive_support/java_ext/locale.rb +32 -0
  30. data/lib/jactive_support/java_ext/map/constructor.rb +35 -0
  31. data/lib/jactive_support/java_ext/map/hash.rb +166 -0
  32. data/lib/jactive_support/java_ext/map.rb +6 -0
  33. data/lib/jactive_support/java_ext/number.rb +91 -0
  34. data/lib/jactive_support/java_ext/sql_date/conversions.rb +33 -0
  35. data/lib/jactive_support/java_ext/sql_date.rb +7 -0
  36. data/lib/jactive_support/java_ext.rb +4 -0
  37. data/lib/jactive_support/json/encoders/collection.rb +12 -0
  38. data/lib/jactive_support/json/encoders/locale.rb +6 -0
  39. data/lib/jactive_support/json/encoders/map.rb +55 -0
  40. data/lib/jactive_support/json/encoding.rb +5 -0
  41. data/lib/jactive_support/json.rb +1 -0
  42. data/lib/jactive_support/rescuable.rb +62 -0
  43. data/lib/jactive_support/version.rb +3 -0
  44. data/lib/jactive_support.rb +5 -0
  45. data/spec/constantize_spec.rb +57 -0
  46. data/spec/core_ext/hash_spec.rb +34 -0
  47. data/spec/core_ext/module_spec.rb +19 -0
  48. data/spec/core_ext/ordering_spec.rb +12 -0
  49. data/spec/enum_spec.rb +67 -0
  50. data/spec/java_ext/iterable/clear_spec.rb +56 -0
  51. data/spec/java_ext/iterable/delete_if_spec.rb +71 -0
  52. data/spec/java_ext/iterable/empty_spec.rb +10 -0
  53. data/spec/java_ext/iterable/reject_spec.rb +76 -0
  54. data/spec/java_ext/iterable/shared/enumeratorize.rb +12 -0
  55. data/spec/java_ext/iterable/shared/fixtures.rb +90 -0
  56. data/spec/java_ext/iterable/shift_spec.rb +160 -0
  57. data/spec/java_ext/list/clear_spec.rb +56 -0
  58. data/spec/java_ext/list/collect_spec.rb +11 -0
  59. data/spec/java_ext/list/last_spec.rb +87 -0
  60. data/spec/java_ext/list/map_spec.rb +11 -0
  61. data/spec/java_ext/list/push_spec.rb +44 -0
  62. data/spec/java_ext/list/shared/collect.rb +133 -0
  63. data/spec/java_ext/list/shared/fixtures.rb +34 -0
  64. data/spec/java_ext/map/assoc_spec.rb +43 -0
  65. data/spec/java_ext/map/clear_spec.rb +49 -0
  66. data/spec/java_ext/map/clone_spec.rb +13 -0
  67. data/spec/java_ext/map/compare_by_identity_spec.rb +111 -0
  68. data/spec/java_ext/map/constructor_spec.rb +54 -0
  69. data/spec/java_ext/map/delete_if_spec.rb +54 -0
  70. data/spec/java_ext/map/delete_spec.rb +38 -0
  71. data/spec/java_ext/map/each_key_spec.rb +29 -0
  72. data/spec/java_ext/map/each_pair_spec.rb +38 -0
  73. data/spec/java_ext/map/each_spec.rb +9 -0
  74. data/spec/java_ext/map/each_value_spec.rb +28 -0
  75. data/spec/java_ext/map/element_reference_spec.rb +119 -0
  76. data/spec/java_ext/map/element_set_spec.rb +7 -0
  77. data/spec/java_ext/map/empty_spec.rb +15 -0
  78. data/spec/java_ext/map/eql_spec.rb +19 -0
  79. data/spec/java_ext/map/equal_value_spec.rb +18 -0
  80. data/spec/java_ext/map/fetch_spec.rb +35 -0
  81. data/spec/java_ext/map/fixtures/classes.rb +36 -0
  82. data/spec/java_ext/map/flatten_spec.rb +64 -0
  83. data/spec/java_ext/map/has_key_spec.rb +8 -0
  84. data/spec/java_ext/map/has_value_spec.rb +8 -0
  85. data/spec/java_ext/map/hash_spec.rb +53 -0
  86. data/spec/java_ext/map/include_spec.rb +7 -0
  87. data/spec/java_ext/map/index_spec.rb +7 -0
  88. data/spec/java_ext/map/indexes_spec.rb +9 -0
  89. data/spec/java_ext/map/indices_spec.rb +9 -0
  90. data/spec/java_ext/map/initialize_copy_spec.rb +13 -0
  91. data/spec/java_ext/map/initialize_spec.rb +64 -0
  92. data/spec/java_ext/map/inspect_spec.rb +9 -0
  93. data/spec/java_ext/map/invert_spec.rb +27 -0
  94. data/spec/java_ext/map/keep_if_spec.rb +33 -0
  95. data/spec/java_ext/map/key_spec.rb +14 -0
  96. data/spec/java_ext/map/keys_spec.rb +39 -0
  97. data/spec/java_ext/map/length_spec.rb +7 -0
  98. data/spec/java_ext/map/member_spec.rb +7 -0
  99. data/spec/java_ext/map/merge_spec.rb +69 -0
  100. data/spec/java_ext/map/rassoc_spec.rb +39 -0
  101. data/spec/java_ext/map/reject_spec.rb +123 -0
  102. data/spec/java_ext/map/replace_spec.rb +7 -0
  103. data/spec/java_ext/map/select_spec.rb +98 -0
  104. data/spec/java_ext/map/shared/each.rb +77 -0
  105. data/spec/java_ext/map/shared/eql.rb +224 -0
  106. data/spec/java_ext/map/shared/equal.rb +94 -0
  107. data/spec/java_ext/map/shared/index.rb +29 -0
  108. data/spec/java_ext/map/shared/iteration.rb +28 -0
  109. data/spec/java_ext/map/shared/key.rb +43 -0
  110. data/spec/java_ext/map/shared/length.rb +12 -0
  111. data/spec/java_ext/map/shared/replace.rb +82 -0
  112. data/spec/java_ext/map/shared/store.rb +68 -0
  113. data/spec/java_ext/map/shared/to_s.rb +68 -0
  114. data/spec/java_ext/map/shared/update.rb +95 -0
  115. data/spec/java_ext/map/shared/value.rb +16 -0
  116. data/spec/java_ext/map/shared/values_at.rb +12 -0
  117. data/spec/java_ext/map/shift_spec.rb +44 -0
  118. data/spec/java_ext/map/size_spec.rb +7 -0
  119. data/spec/java_ext/map/sort_spec.rb +17 -0
  120. data/spec/java_ext/map/store_spec.rb +7 -0
  121. data/spec/java_ext/map/to_a_spec.rb +29 -0
  122. data/spec/java_ext/map/to_hash_spec.rb +11 -0
  123. data/spec/java_ext/map/to_s_spec.rb +20 -0
  124. data/spec/java_ext/map/try_convert_spec.rb +32 -0
  125. data/spec/java_ext/map/update_spec.rb +7 -0
  126. data/spec/java_ext/map/value_spec.rb +8 -0
  127. data/spec/java_ext/map/values_at_spec.rb +7 -0
  128. data/spec/java_ext/map/values_spec.rb +10 -0
  129. data/spec/locale_spec.rb +77 -0
  130. data/spec/shared/fixtures.rb +7 -0
  131. data/spec/shared/version.rb +123 -0
  132. data/spec/spec_helper.rb +50 -0
  133. metadata +332 -0
@@ -0,0 +1,74 @@
1
+ require 'jactive_support/java_ext/iterator'
2
+
3
+ module java::lang::Iterable
4
+ if RUBY_VERSION >= '1.9'
5
+ FrozenError = RuntimeError
6
+ else
7
+ FrozenError = TypeError
8
+ end
9
+
10
+ def iterate
11
+ return enum_for(:iterate) unless block_given?
12
+ it = iterator
13
+ while it.next?
14
+ ob = it.next
15
+ yield(it,ob)
16
+ end
17
+ end
18
+
19
+ def delete_if
20
+ raise FrozenError, "can't modify frozen iterable" if frozen?
21
+ return enum_for(:delete_if) unless block_given?
22
+ iterate do |it,ob|
23
+ it.remove if yield(ob)
24
+ end
25
+ self
26
+ end
27
+
28
+ def reject!
29
+ raise FrozenError, "can't modify frozen iterable" if frozen?
30
+ return enum_for(:reject!) unless block_given?
31
+ ret = nil
32
+ iterate do |it,ob|
33
+ if yield(ob)
34
+ it.remove
35
+ ret = self
36
+ end
37
+ end
38
+ ret
39
+ end
40
+
41
+ def shift(*args)
42
+ raise ArgumentError, "wrong # of arguments(#{args.size} for 1)" if args.size > 1
43
+ raise FrozenError, "can't modify frozen iterable" if frozen?
44
+ it = iterator
45
+ if args.size == 1
46
+ n = args.first
47
+ raise TypeError, "Can't convert #{n} into Integer" unless n.respond_to?(:to_int)
48
+ n = n.to_int
49
+ raise TypeError, "#to_int should return Integer" unless n.kind_of?(Integer)
50
+ raise ArgumentError, "negative array size" if n < 0
51
+ ret = []
52
+ while n > 0 && it.next?
53
+ ret << it.next
54
+ it.remove
55
+ n -= 1
56
+ end
57
+ ret
58
+ elsif it.next?
59
+ ret = it.next
60
+ it.remove
61
+ ret
62
+ end
63
+ end
64
+
65
+ def empty?
66
+ !iterator.next?
67
+ end
68
+
69
+ def clear
70
+ raise FrozenError, "can't modify frozen iterable" if frozen?
71
+ iterate {|it, o| it.remove}
72
+ self
73
+ end
74
+ end
@@ -0,0 +1,8 @@
1
+ module java::util::Iterator
2
+ def has_next?
3
+ hasNext
4
+ end
5
+ def next?
6
+ hasNext
7
+ end
8
+ end
@@ -0,0 +1,65 @@
1
+ require 'jactive_support/java_ext/list_iterator'
2
+
3
+ module java::util::List
4
+ if RUBY_VERSION >= '1.9'
5
+ FrozenError = RuntimeError
6
+ else
7
+ FrozenError = TypeError
8
+ end
9
+
10
+ def inspect
11
+ content = map{|e| e.inspect}.join(', ')
12
+ "JavaList[#{content}]"
13
+ end
14
+
15
+ def iterate
16
+ return enum_for(:iterate) unless block_given?
17
+ it = listIterator
18
+ while it.next?
19
+ ob = it.next
20
+ yield(it,ob)
21
+ end
22
+ end
23
+
24
+ def map!
25
+ raise FrozenError, "can't modify frozen iterable" if frozen?
26
+ return enum_for(:map!) unless block_given?
27
+ iterate{|it, ob| it.set(yield(ob))}
28
+ self
29
+ end
30
+ alias :collect! :map!
31
+
32
+ def push(*args)
33
+ raise FrozenError, "can't modify frozen iterable" if frozen? && ( RUBY_VERSION >= '1.9' || args.size > 0 )
34
+ args.each{|a| add(a)}
35
+ self
36
+ end
37
+
38
+ def last(*args)
39
+ raise ArgumentError, "wrong # of arguments(#{args.size} for 1)" if args.size > 1
40
+ if args.size == 1
41
+ n = args.first
42
+ raise TypeError, "Can't convert #{n} into Integer" unless n.respond_to?(:to_int)
43
+ n = n.to_int
44
+ raise TypeError, "#to_int should return Integer" unless n.kind_of?(Integer)
45
+ raise ArgumentError, "negative array size" if n < 0
46
+
47
+ it = listIterator(size)
48
+ ret = []
49
+ while n > 0 && it.previous?
50
+ ret.unshift it.previous
51
+ n -= 1
52
+ end
53
+ ret
54
+ elsif size > 0
55
+ self.get(size-1)
56
+ end
57
+ end
58
+
59
+ def clear
60
+ raise FrozenError, "can't modify frozen list" if frozen?
61
+ super
62
+ self
63
+ end
64
+
65
+ end
@@ -0,0 +1,8 @@
1
+ module java::util::ListIterator
2
+ def has_previous?
3
+ hasPrevious
4
+ end
5
+ def previous?
6
+ hasPrevious
7
+ end
8
+ end
@@ -0,0 +1,32 @@
1
+ class java::util::Locale
2
+ def self.current_locale
3
+ self.getDefault
4
+ end
5
+
6
+ def self.current_locale=(l)
7
+ self.setDefault(l.to_locale)
8
+ end
9
+
10
+ def to_locale
11
+ self
12
+ end
13
+
14
+ def human_name
15
+ l = Locale.current_locale
16
+ display_country = country.blank? ? "" : " (#{getDisplayCountry(l)})"
17
+ display_variant = variant.blank? ? "" : " - #{getDisplayVariant(l)}"
18
+ "#{getDisplayLanguage(l)}#{display_country}#{display_variant}"
19
+ end
20
+
21
+ def inspect
22
+ "Locale[#{human_name}]"
23
+ end
24
+
25
+ def to_str
26
+ to_s
27
+ end
28
+
29
+ def to_sym
30
+ to_s.to_sym
31
+ end
32
+ end
@@ -0,0 +1,35 @@
1
+ module JactiveSupport #:nodoc:
2
+ module JavaExtensions #:nodoc:
3
+ module Map #:nodoc:
4
+ module Constructor
5
+ def self.included(other_mod)
6
+ other_mod.extend(ClassExtensions)
7
+ end
8
+
9
+ module ClassExtensions
10
+ def [](*args)
11
+ r = self.new
12
+ if args.size == 1 && ( args.first.respond_to?(:to_ary) || args.first.respond_to?(:to_hash) )
13
+ args = args.first
14
+ if args.respond_to?(:to_ary)
15
+ args = args.to_ary
16
+ elsif args.respond_to?(:to_hash)
17
+ args = args.to_hash
18
+ end
19
+ args.each do |e|
20
+ next unless e.respond_to? :to_ary
21
+ e = e.to_ary
22
+ r.put(e.shift, e.shift) if (1..2).include?(e.size)
23
+ end
24
+ else
25
+ raise ArgumentError, "odd number of arguments for constructor" unless (args.size % 2) == 0
26
+ args.each_slice(2) {|key,val| r.put(key,val)}
27
+ end
28
+
29
+ r
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,166 @@
1
+ module JactiveSupport #:nodoc:
2
+ module JavaExtensions #:nodoc:
3
+ module Map #:nodoc:
4
+ module Hash
5
+ def self.included(other_mod)
6
+ other_mod.send :alias_method, :has_key?, :contains_key?
7
+ other_mod.send :alias_method, :key?, :has_key?
8
+ other_mod.send :alias_method, :include?, :has_key?
9
+ other_mod.send :alias_method, :member?, :has_key?
10
+ other_mod.send :alias_method, :has_value?, :contains_value?
11
+ other_mod.send :alias_method, :value?, :has_value?
12
+ other_mod.send :alias_method, :update, :merge!
13
+ end
14
+
15
+ def [](key)
16
+ get(key)
17
+ end
18
+
19
+ def []=(key,val)
20
+ put(key,val)
21
+ end
22
+
23
+ def each
24
+ entry_set.each do |e|
25
+ yield [e.key, e.value]
26
+ end
27
+ self
28
+ end
29
+
30
+ def each_pair
31
+ entry_set.each do |e|
32
+ yield e.key, e.value
33
+ end
34
+ self
35
+ end
36
+
37
+ def contains_key?(key)
38
+ containsKey(key)
39
+ end
40
+
41
+ def contains_value?(value)
42
+ containsValue(value)
43
+ end
44
+
45
+ def length
46
+ size
47
+ end
48
+
49
+ def delete_if(&block)
50
+ entry_set.iterate do |it, entry|
51
+ it.remove if yield(entry.key, entry.value)
52
+ end
53
+ self
54
+ end
55
+
56
+ def each_key(&block)
57
+ key_set.each(&block)
58
+ self
59
+ end
60
+
61
+ def each_value(&block)
62
+ values.each(&block)
63
+ self
64
+ end
65
+
66
+ def delete(key)
67
+ if block_given? && !key?(key)
68
+ yield key
69
+ else
70
+ remove(key)
71
+ end
72
+ end
73
+
74
+ def fetch(key,default=nil)
75
+ return get(key) if key?(key)
76
+ raise IndexError unless default || block_given?
77
+ return default if default
78
+ yield key
79
+ end
80
+
81
+ def index(value)
82
+ ret = entry_set.find {|entry| entry.value == value}
83
+ return nil unless ret
84
+ ret.key
85
+ end
86
+
87
+ def invert
88
+ ret = nil
89
+ begin
90
+ ret = dup
91
+ ret.clear
92
+ ret
93
+ rescue
94
+ ret = ::Hash.new
95
+ end
96
+ each_pair {|key,value| ret[value] = key}
97
+ ret
98
+ end
99
+
100
+ def keys
101
+ key_set.to_a
102
+ end
103
+
104
+ def merge(other_hash,&block)
105
+ dup.merge!(other_hash,&block)
106
+ end
107
+
108
+ def merge!(other_hash)
109
+ if block_given?
110
+ other_hash.each_pair do |key, newval|
111
+ if has_key?(key)
112
+ oldval = get(key)
113
+ newval = yield(key,oldval,newval)
114
+ end
115
+ put(key, newval)
116
+ end
117
+ else
118
+ if other_hash.respond_to? :to_java_map
119
+ other_hash = other_hash.to_java_map
120
+ elsif other_hash.respond_to? :to_hash
121
+ other_hash = other_hash.to_hash
122
+ end
123
+ put_all(other_hash)
124
+ end
125
+ self
126
+ end
127
+
128
+ def to_hash
129
+ ret = ::Hash.new
130
+ each_pair {|key,value| ret[key] = value}
131
+ ret
132
+ end
133
+
134
+ def reject(&block)
135
+ dup.delete_if(&block)
136
+ end
137
+
138
+ def reject!
139
+ ret = nil
140
+ entry_set.iterate do |it, entry|
141
+ if yield(entry.key, entry.value)
142
+ it.remove
143
+ ret = self
144
+ end
145
+ end
146
+ ret
147
+ end
148
+
149
+ def replace(other_hash)
150
+ clear
151
+ merge!(other_hash)
152
+ end
153
+
154
+ def values_at(*keys)
155
+ keys.inject([]) do |memo,key|
156
+ memo.push(get(key))
157
+ end
158
+ end
159
+
160
+ def dup
161
+ clone
162
+ end
163
+ end
164
+ end
165
+ end
166
+ end
@@ -0,0 +1,6 @@
1
+ require 'jactive_support/java_ext/map/hash'
2
+
3
+ module java::util::Map #:nodoc:
4
+ include JactiveSupport::JavaExtensions::Map::Hash
5
+ Hash.included_modules.find_all {|m| m.name.start_with?('ActiveSupport::CoreExtensions::') }.each {|m| include m}
6
+ end
@@ -0,0 +1,91 @@
1
+ class java::lang::Number
2
+ def self.number_format_instance(format=:default, locale=nil)
3
+ case format
4
+ when :number then ::Java::JavaText::NumberFormat.getNumberInstance(locale.to_locale)
5
+ when :currency then ::Java::JavaText::NumberFormat.getCurrencyInstance(locale.to_locale)
6
+ when :percent then ::Java::JavaText::NumberFormat.getPercentInstance(locale.to_locale)
7
+ when :scientific then ::Java::JavaText::NumberFormat.getScientificInstance(locale.to_locale)
8
+ when :integer then ::Java::JavaText::NumberFormat.getIntegerInstance(locale.to_locale)
9
+ when :default then ::Java::JavaText::NumberFormat.getInstance(locale.to_locale)
10
+ end
11
+ end
12
+
13
+ NUMBER_FORMATS = {
14
+ :number => lambda {|value, locale| number_format_instance(:number, locale).format(value.to_primitive)},
15
+ :currency => lambda {|value, locale| number_format_instance(:currency, locale).format(value.to_primitive)},
16
+ :percent => lambda {|value, locale| number_format_instance(:percent, locale).format(value.to_primitive)},
17
+ :scientific => lambda {|value, locale| number_format_instance(:scientific, locale).format(value.to_primitive)},
18
+ :integer => lambda {|value, locale| number_format_instance(:integer, locale).format(value.to_primitive)},
19
+ :default => lambda {|value, locale| number_format_instance(:default, locale).format(value.to_primitive)},
20
+ }
21
+
22
+ def to_formatted_s(format=:default, locale=nil)
23
+ return to_default_s unless formatter = self.class::NUMBER_FORMATS[format]
24
+ formatter.respond_to?(:call) ? (formatter.arity==2 ? formatter.call(self, locale) : formatter.call(self)).to_s : format_number(formatter)
25
+ end
26
+ alias :to_default_s :to_s
27
+ alias :to_s :to_formatted_s
28
+
29
+ def format_number(format, locale=nil)
30
+ symbols = ::Java::JavaText::DecimalFormatSymbols.new(locale.to_locale)
31
+ formatter = ::Java::JavaText::DecimalFormat.new(format, symbols)
32
+ formatter.format(self)
33
+ end
34
+
35
+ def to_primitive
36
+ self
37
+ end
38
+ end
39
+
40
+ class java::lang::Byte
41
+ #alias :to_default_s :to_s
42
+ #alias :to_s :to_formatted_s
43
+
44
+ alias :to_primitive :byte_value
45
+ end
46
+
47
+ class java::lang::Short
48
+ #alias :to_default_s :to_s
49
+ #alias :to_s :to_formatted_s
50
+
51
+ alias :to_primitive :short_value
52
+ end
53
+
54
+ class java::lang::Integer
55
+ #alias :to_default_s :to_s
56
+ #alias :to_s :to_formatted_s
57
+
58
+ alias :to_primitive :int_value
59
+ end
60
+
61
+ class java::lang::Long
62
+ #alias :to_default_s :to_s
63
+ #alias :to_s :to_formatted_s
64
+
65
+ alias :to_primitive :long_value
66
+ end
67
+
68
+ class java::lang::Float
69
+ #alias :to_default_s :to_s
70
+ #alias :to_s :to_formatted_s
71
+
72
+ alias :to_primitive :float_value
73
+ end
74
+
75
+ class java::lang::Double
76
+ #alias :to_default_s :to_s
77
+ #alias :to_s :to_formatted_s
78
+
79
+ alias :to_primitive :double_value
80
+ end
81
+
82
+
83
+ #class Java::JavaMath::BigDecimal
84
+ # alias :to_default_s :to_s
85
+ # alias :to_s :to_formatted_s
86
+ #end
87
+
88
+ #class Java::JavaMath::BigInteger
89
+ # alias :to_default_s :to_s
90
+ # alias :to_s :to_formatted_s
91
+ #end
@@ -0,0 +1,33 @@
1
+ module JactiveSupport #:nodoc:
2
+ module CoreExtensions #:nodoc:
3
+ module SqlDate #:nodoc:
4
+ # Converting dates to formatted strings, times, and datetimes.
5
+ module Conversions
6
+ DATE_FORMATS = {
7
+ :db => "yyyy-MM-dd",
8
+ :i18n => lambda { |clazz, locale|
9
+ format = I18n.translate(:"formats.date", :default=>'')
10
+ !format.blank? ? clazz.pattern_formatter(format) : clazz.date_instance(:default, locale)
11
+ },
12
+ :number => "YYYMMdd",
13
+ :full => lambda { |clazz, locale| clazz.date_instance(:full, locale) },
14
+ :long => lambda { |clazz, locale| clazz.date_instance(:long, locale) },
15
+ :medium => lambda { |clazz, locale| clazz.date_instance(:medium, locale) },
16
+ :short => lambda { |clazz, locale| clazz.date_instance(:short, locale) },
17
+ :default => lambda { |clazz, locale| clazz.date_instance(:default, locale) },
18
+ :long_ordinal => lambda { |clazz| clazz.pattern_formatter("%B #{time.day.ordinalize}, %Y %H:%M") },
19
+ :rfc822 => lambda { |clazz| clazz.pattern_formatter("%a, %d %b %Y %H:%M:%S #{time.formatted_offset(false)}") },
20
+ :httpdate => lambda { |clazz| clazz.pattern_formatter("EEE, dd MMM yyyy HH:mm:ss z", "GMT") }
21
+ }
22
+
23
+ def to_java_sqldate
24
+ self
25
+ end
26
+
27
+ def acts_like_time?
28
+ false
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,7 @@
1
+ require 'active_support/core_ext/date/behavior'
2
+ require 'jactive_support/java_ext/sql_date/conversions'
3
+
4
+ class java::sql::Date
5
+ include ActiveSupport::CoreExtensions::Date::Behavior
6
+ include JactiveSupport::JavaExtensions::SqlDate::Conversions
7
+ end
@@ -0,0 +1,4 @@
1
+ Dir[File.dirname(__FILE__) + "/java_ext/*.rb"].sort.each do |path|
2
+ filename = File.basename(path)
3
+ require "jactive_support/java_ext/#{filename}"
4
+ end
@@ -0,0 +1,12 @@
1
+ module java::util::Collection #:nodoc:
2
+
3
+ # Returns a JSON string representing the Array. +options+ are passed to each element.
4
+ def to_json(options = nil) #:nodoc:
5
+ "[#{map { |value| ActiveSupport::JSON.encode(value, options) } * ','}]"
6
+ end
7
+
8
+ def as_json(options = nil) #:nodoc:
9
+ self
10
+ end
11
+
12
+ end
@@ -0,0 +1,6 @@
1
+ class java::util::Locale
2
+
3
+ def to_json(options = {}) #:nodoc:
4
+ ActiveSupport::JSON.encode(to_s)
5
+ end
6
+ end
@@ -0,0 +1,55 @@
1
+ module java::util::Map #:nodoc:
2
+ # Returns a JSON string representing the hash.
3
+ #
4
+ # Without any +options+, the returned JSON string will include all
5
+ # the hash keys. For example:
6
+ #
7
+ # { :name => "Konata Izumi", 'age' => 16, 1 => 2 }.to_json
8
+ # # => {"name": "Konata Izumi", 1: 2, "age": 16}
9
+ #
10
+ # The keys in the JSON string are unordered due to the nature of hashes.
11
+ #
12
+ # The <tt>:only</tt> and <tt>:except</tt> options can be used to limit the
13
+ # attributes included, and will accept 1 or more hash keys to include/exclude.
14
+ #
15
+ # { :name => "Konata Izumi", 'age' => 16, 1 => 2 }.to_json(:only => [:name, 'age'])
16
+ # # => {"name": "Konata Izumi", "age": 16}
17
+ #
18
+ # { :name => "Konata Izumi", 'age' => 16, 1 => 2 }.to_json(:except => 1)
19
+ # # => {"name": "Konata Izumi", "age": 16}
20
+ #
21
+ # The +options+ also filter down to any hash values. This is particularly
22
+ # useful for converting hashes containing ActiveRecord objects or any object
23
+ # that responds to options in their <tt>to_json</tt> method. For example:
24
+ #
25
+ # users = User.find(:all)
26
+ # { :users => users, :count => users.size }.to_json(:include => :posts)
27
+ #
28
+ # would pass the <tt>:include => :posts</tt> option to <tt>users</tt>,
29
+ # allowing the posts association in the User model to be converted to JSON
30
+ # as well.
31
+ def to_json(options = {}) #:nodoc:
32
+ hash = as_json(options)
33
+ return ActiveSupport::JSON.encode(hash, options) if hash != self
34
+
35
+ result = '{'
36
+ result << hash.map do |key, value|
37
+ "#{ActiveSupport::JSON.encode(key.to_s)}:#{ActiveSupport::JSON.encode(value, options)}"
38
+ end * ','
39
+ result << '}'
40
+ end
41
+
42
+ def as_json(options = nil) #:nodoc:
43
+ if options
44
+ if attrs = options[:except]
45
+ except(*Array.wrap(attrs))
46
+ elsif attrs = options[:only]
47
+ slice(*Array.wrap(attrs))
48
+ else
49
+ self
50
+ end
51
+ else
52
+ self
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,5 @@
1
+ require 'active_support/json/encoding'
2
+ Dir["#{File.dirname(__FILE__)}/encoders/**/*.rb"].each do |file|
3
+ basename = File.basename(file, '.rb')
4
+ require "jactive_support/json/encoders/#{basename}"
5
+ end
@@ -0,0 +1 @@
1
+ require 'jactive_support/json/encoding'
@@ -0,0 +1,62 @@
1
+ require 'active_support/rescuable'
2
+
3
+ module ActiveSupport
4
+ module Rescuable
5
+ module ClassMethods
6
+ def rescue_from(*klasses, &block)
7
+ options = klasses.extract_options!
8
+
9
+ unless options.has_key?(:with)
10
+ if block_given?
11
+ options[:with] = block
12
+ else
13
+ raise ArgumentError, "Need a handler. Supply an options hash that has a :with key as the last argument."
14
+ end
15
+ end
16
+
17
+ klasses.each do |klass|
18
+ key = if klass.is_a?(Class) && ( klass <= Exception || klass <= ::Java::JavaLang::Exception )
19
+ klass.name
20
+ elsif klass.is_a?(String)
21
+ klass
22
+ else
23
+ raise ArgumentError, "#{klass} is neither an Exception nor a String"
24
+ end
25
+
26
+ # put the new handler at the end because the list is read in reverse
27
+ self.rescue_handlers += [[key, options[:with]]]
28
+ end
29
+ end
30
+ end
31
+
32
+ def handler_for_rescue(exception)
33
+ # We go from right to left because pairs are pushed onto rescue_handlers
34
+ # as rescue_from declarations are found.
35
+ _, rescuer = self.class.rescue_handlers.reverse.detect do |klass_name, handler|
36
+ # The purpose of allowing strings in rescue_from is to support the
37
+ # declaration of handler associations for exception classes whose
38
+ # definition is yet unknown.
39
+ #
40
+ # Since this loop needs the constants it would be inconsistent to
41
+ # assume they should exist at this point. An early raised exception
42
+ # could trigger some other handler and the array could include
43
+ # precisely a string whose corresponding constant has not yet been
44
+ # seen. This is why we are tolerant to unknown constants.
45
+ #
46
+ # Note that this tolerance only matters if the exception was given as
47
+ # a string, otherwise a NameError will be raised by the interpreter
48
+ # itself when rescue_from CONSTANT is executed.
49
+ klass = self.class.const_get(klass_name) rescue nil
50
+ klass ||= klass_name.constantize rescue nil
51
+ exception.is_a?(klass) || (exception.is_a?(NativeException) && exception.cause.is_a?(klass)) if klass
52
+ end
53
+
54
+ case rescuer
55
+ when Symbol
56
+ method(rescuer)
57
+ when Proc
58
+ rescuer.bind(self)
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,3 @@
1
+ module JActiveSupport
2
+ VERSION = "1.0.0"
3
+ end