friendly_extensions 0.2.5 → 0.2.6
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.
- checksums.yaml +4 -4
- data/app/helpers/friends_labeled_form_helper.rb +7 -1
- data/lib/array.rb +82 -73
- metadata +5 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6354af79708e832401f28a6f2900e405f780e7b2
|
4
|
+
data.tar.gz: 048844ed29ed97517b3c405bd3a8505ecd51f204
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b4fc1d9724f7905989ef102edd37a7ae59f778185c40fbb3052279091cf8535ac76ad25354ce64669df280a77ffed5deb02c702531584838029c39b5cb9e1664
|
7
|
+
data.tar.gz: 1213e7066c6ff69c76efa17c209c8864d3497193d43d3e7dd64e2c0890d7fdbcf023136e3fcdf8eef9d348fa0f5f4dcd77f8a912c47511a4a3ff0ee0376fde64
|
@@ -53,7 +53,13 @@ module FriendsLabeledFormHelper
|
|
53
53
|
label_options = options
|
54
54
|
end
|
55
55
|
|
56
|
-
|
56
|
+
if options[:label_as_placeholder].present?
|
57
|
+
html = ""
|
58
|
+
options[:placeholder] = options[:label_as_placeholder]
|
59
|
+
else
|
60
|
+
html = label_tag_for(object_name, method, label_options)
|
61
|
+
end
|
62
|
+
|
57
63
|
if options[:password] && options.delete(:password)
|
58
64
|
html += password_field(object_name, method, options)
|
59
65
|
else
|
data/lib/array.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
# Add the methods to the Array Class
|
3
3
|
module ArrayExt
|
4
4
|
Array.class_eval do
|
5
|
-
|
5
|
+
|
6
6
|
# Use sum from Activesupport
|
7
7
|
# Sum is not a default in Ruby
|
8
8
|
def sum(identity = 0, &block)
|
@@ -12,7 +12,7 @@ module ArrayExt
|
|
12
12
|
inject { |sum, element| sum + element } || identity
|
13
13
|
end
|
14
14
|
end
|
15
|
-
|
15
|
+
|
16
16
|
# Get avg. value from, either from numeric values or from
|
17
17
|
# values from a object included in array
|
18
18
|
def avg(attribute = nil)
|
@@ -31,27 +31,27 @@ module ArrayExt
|
|
31
31
|
value.size == 0 ? size = 1 : size = value.size
|
32
32
|
value.sum / size
|
33
33
|
end
|
34
|
-
|
35
|
-
|
36
|
-
|
34
|
+
|
35
|
+
|
36
|
+
|
37
37
|
# Generate a hash with the given array elements as keys and 'init_value' as value
|
38
38
|
def array_to_hash(init_val = 0)
|
39
39
|
h = {}
|
40
40
|
self.each {|x| h.merge!(x => init_val) unless x.nil? }
|
41
41
|
return h
|
42
42
|
end
|
43
|
-
|
44
|
-
|
43
|
+
|
44
|
+
|
45
45
|
def to_structured_hash(attribute, options = {})
|
46
46
|
data = {}
|
47
47
|
|
48
48
|
self.each do |item|
|
49
49
|
if attribute.is_a?(Symbol) && options[:load_from_hash] != true
|
50
50
|
key = item.send(attribute) rescue ""
|
51
|
-
elsif attribute.is_a?(Symbol) && options[:load_from_hash] == true
|
51
|
+
elsif attribute.is_a?(Symbol) && options[:load_from_hash] == true
|
52
52
|
key = item[attribute] rescue ""
|
53
53
|
elsif attribute.is_a?(String)
|
54
|
-
attribute_methods = attribute.split(".")
|
54
|
+
attribute_methods = attribute.split(".")
|
55
55
|
key = item.send(attribute_methods[0])
|
56
56
|
attribute_methods[1..-1].each do |x|
|
57
57
|
key = key.send(x) rescue ""
|
@@ -62,13 +62,13 @@ module ArrayExt
|
|
62
62
|
data.merge!(key => [item])
|
63
63
|
else
|
64
64
|
data[key] << item
|
65
|
-
end
|
65
|
+
end
|
66
66
|
end
|
67
67
|
|
68
|
-
return data
|
69
|
-
end
|
68
|
+
return data
|
69
|
+
end
|
70
|
+
|
70
71
|
|
71
|
-
|
72
72
|
# Sum up an array of objects with given attirbute
|
73
73
|
# Attributes can be given as symbol (:my_value will return obj.my_value)
|
74
74
|
# or as string ("user.my_value" will return my_value from user object which belongs to object in array)
|
@@ -79,26 +79,26 @@ module ArrayExt
|
|
79
79
|
else
|
80
80
|
values = []
|
81
81
|
if arg.is_a?(Symbol)
|
82
|
-
self.map {|i| ((opt.nil? ? i.send(arg) : i.send(arg, opt)) rescue 0) }.sum
|
82
|
+
self.map {|i| ((opt.nil? ? i.send(arg) : i.send(arg, opt)) rescue 0) }.sum
|
83
83
|
elsif arg.is_a?(String)
|
84
84
|
self.each do |v|
|
85
|
-
tag_methods = arg.split(".")
|
85
|
+
tag_methods = arg.split(".")
|
86
86
|
tagx = v.send(tag_methods[0])
|
87
87
|
tag_methods[1..-1].each do |x|
|
88
88
|
# Use options if last method in chain called
|
89
89
|
if tag_methods[1..-1].last == x && !opt.nil?
|
90
90
|
tagx = tagx.send(x,opt)
|
91
|
-
else
|
91
|
+
else
|
92
92
|
tagx = tagx.send(x)
|
93
|
-
end
|
93
|
+
end
|
94
94
|
end
|
95
|
-
values << tagx
|
95
|
+
values << tagx
|
96
96
|
end
|
97
97
|
return values.compact.sum
|
98
98
|
end
|
99
99
|
end
|
100
100
|
end
|
101
|
-
|
101
|
+
|
102
102
|
|
103
103
|
# check the number of items included in the array
|
104
104
|
def count_for(item)
|
@@ -106,32 +106,32 @@ module ArrayExt
|
|
106
106
|
self.each {|x| count += 1 if x == item}
|
107
107
|
return count
|
108
108
|
end
|
109
|
-
|
110
|
-
|
109
|
+
|
110
|
+
|
111
111
|
# return the item after the given val in array
|
112
112
|
# returns val if no item found or val is not included
|
113
113
|
# toggle :cycle => true to continue search at beginn of array if end is reached
|
114
114
|
def next(val, options = {})
|
115
115
|
i = self.index(val)
|
116
116
|
return val if i.nil?
|
117
|
-
|
117
|
+
|
118
118
|
i == self.size-1 ?
|
119
|
-
(options[:cycle] == true ? self.first : val) : self[i+1]
|
120
|
-
end
|
121
|
-
|
122
|
-
|
119
|
+
(options[:cycle] == true ? self.first : val) : self[i+1]
|
120
|
+
end
|
121
|
+
|
122
|
+
|
123
123
|
# Schnittmenge / Intersection zwischen 2 Arrays
|
124
124
|
def isec(b)
|
125
125
|
raise ArgumentError, "#{b.inspect} is not an array" if !b.is_a?(Array)
|
126
126
|
(self- (self-b))
|
127
|
-
end
|
128
|
-
|
127
|
+
end
|
128
|
+
|
129
129
|
# like except for hash
|
130
130
|
def except(*args)
|
131
131
|
excluded = *args
|
132
132
|
return self - excluded
|
133
|
-
end
|
134
|
-
|
133
|
+
end
|
134
|
+
|
135
135
|
# break array into n arrays
|
136
136
|
def seperate(n = 8)
|
137
137
|
f = n
|
@@ -147,7 +147,7 @@ module ArrayExt
|
|
147
147
|
#arrays.last.pop if (arrays.last.last.empty? || arrays.last.last.blank?)
|
148
148
|
return arrays
|
149
149
|
end
|
150
|
-
|
150
|
+
|
151
151
|
# [1,2,3,5,6,7].stack(2)
|
152
152
|
# will return [[1, 2], [3, 5], [6, 7]]
|
153
153
|
def stack(n = 4)
|
@@ -155,10 +155,10 @@ module ArrayExt
|
|
155
155
|
i = (self.size.to_f/n).ceil
|
156
156
|
i.times do |x|
|
157
157
|
arr << self[x*n..(((x+1)*n)-1)]
|
158
|
-
end
|
158
|
+
end
|
159
159
|
return arr
|
160
|
-
end
|
161
|
-
|
160
|
+
end
|
161
|
+
|
162
162
|
|
163
163
|
# Prepare array for use with mysql IN operator
|
164
164
|
def to_sql
|
@@ -167,12 +167,12 @@ module ArrayExt
|
|
167
167
|
return "(#{self.compact.join(',')})"
|
168
168
|
else
|
169
169
|
return "(#{self.map{|i| "'#{i.to_s}'"}.join(',')})"
|
170
|
-
end
|
171
|
-
end
|
172
|
-
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
173
|
#== Untested / deprecated functions below, should be moved somewhere else!
|
174
174
|
# Ignore these functions, there are neither tested nor documented, use them for fun if you like but dont ask me ;-)
|
175
|
-
|
175
|
+
|
176
176
|
def fill_with_sth(sth, size)
|
177
177
|
ary_length = self.size
|
178
178
|
diff = size - ary_length
|
@@ -196,85 +196,94 @@ module ArrayExt
|
|
196
196
|
def to_text(sep = "<br />")
|
197
197
|
if Rails.env == "development"
|
198
198
|
raise "#REMOVED - use .join() - (17.12.2013, 15:18, Florian Eck)"
|
199
|
-
else
|
199
|
+
else
|
200
200
|
self.join(sep)
|
201
|
-
end
|
202
|
-
end
|
203
|
-
|
204
|
-
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
|
205
205
|
def interpolate(filtered_value = nil, options = {})
|
206
206
|
begin
|
207
|
-
|
207
|
+
|
208
208
|
int_start_value = nil
|
209
209
|
int_end_value = nil
|
210
210
|
int_from = 0
|
211
|
-
int_to = 0
|
212
|
-
int_steps = 0
|
213
|
-
|
211
|
+
int_to = 0
|
212
|
+
int_steps = 0
|
213
|
+
|
214
214
|
array_with_interpolation = []
|
215
|
-
|
216
|
-
self.each_with_index do |x,i|
|
215
|
+
|
216
|
+
self.each_with_index do |x,i|
|
217
217
|
if i > 0
|
218
218
|
if x != filtered_value
|
219
219
|
# Wenn der letze ein fehler war ist die strecke zuende
|
220
220
|
if self[i-1] == filtered_value
|
221
|
-
|
221
|
+
|
222
222
|
if i-1 == 0
|
223
223
|
# Berechnung anstieg der linearfunktion zwischen Start-und Endpunkt
|
224
|
-
#
|
224
|
+
#
|
225
225
|
m = (self[i+1]-x)
|
226
|
-
|
226
|
+
|
227
227
|
array_with_interpolation[0] = x-m
|
228
228
|
array_with_interpolation << x
|
229
|
-
|
230
|
-
else
|
229
|
+
|
230
|
+
else
|
231
231
|
int_end_value = x
|
232
232
|
int_to = i
|
233
|
-
|
233
|
+
|
234
234
|
# Berechnung anstieg der linearfunktion zwischen Start-und Endpunkt
|
235
235
|
m = (int_end_value-int_start_value).fdiv((int_to-int_from))
|
236
|
-
|
236
|
+
|
237
237
|
# Fülle fehlende werte mit werten (y = mx + b)
|
238
238
|
int_steps.times {|mi| array_with_interpolation << (int_start_value+((mi+1)*m)) }
|
239
239
|
array_with_interpolation << int_end_value
|
240
|
-
|
240
|
+
|
241
241
|
# Reset all values
|
242
242
|
int_start_value = int_end_value
|
243
243
|
int_end_value = nil
|
244
244
|
int_from = 0
|
245
|
-
int_to = 0
|
246
|
-
int_steps = 0
|
245
|
+
int_to = 0
|
246
|
+
int_steps = 0
|
247
247
|
puts array_with_interpolation.inspect
|
248
|
-
end
|
249
|
-
else
|
248
|
+
end
|
249
|
+
else
|
250
250
|
int_start_value = x
|
251
251
|
array_with_interpolation << x
|
252
252
|
end
|
253
|
-
# Wenn letzer wert fehlerhaft
|
253
|
+
# Wenn letzer wert fehlerhaft
|
254
254
|
elsif i == self.size-1 && x == filtered_value
|
255
255
|
# Berechnung anstieg der linearfunktion zwischen Start-und Endpunkt
|
256
256
|
# Berechnung über gesamtes array
|
257
257
|
m = (array_with_interpolation.last-array_with_interpolation.first).fdiv(array_with_interpolation.size-1)
|
258
258
|
array_with_interpolation << (array_with_interpolation.last+m)
|
259
|
-
else
|
259
|
+
else
|
260
260
|
int_steps += 1
|
261
261
|
int_from = i-int_steps
|
262
|
-
end
|
263
|
-
|
262
|
+
end
|
263
|
+
|
264
264
|
else
|
265
265
|
int_start_value = x
|
266
266
|
array_with_interpolation << x
|
267
|
-
end
|
268
|
-
|
269
|
-
end
|
270
|
-
|
267
|
+
end
|
268
|
+
|
269
|
+
end
|
270
|
+
|
271
271
|
return array_with_interpolation
|
272
272
|
rescue
|
273
273
|
puts "WARNING: Interpolation not possible!".red
|
274
274
|
return self
|
275
|
-
end
|
276
|
-
|
277
|
-
end
|
275
|
+
end
|
276
|
+
|
277
|
+
end
|
278
|
+
|
279
|
+
|
280
|
+
def odd_values
|
281
|
+
self.values_at(* self.each_index.select {|i| i.odd?})
|
282
|
+
end
|
283
|
+
|
284
|
+
def even_values
|
285
|
+
self.values_at(* self.each_index.select {|i| i.even?})
|
286
|
+
end
|
278
287
|
|
279
|
-
end
|
288
|
+
end
|
280
289
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: friendly_extensions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Florian Eck
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-06-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -72,14 +72,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
72
72
|
version: '0'
|
73
73
|
requirements: []
|
74
74
|
rubyforge_project:
|
75
|
-
rubygems_version: 2.
|
75
|
+
rubygems_version: 2.6.8
|
76
76
|
signing_key:
|
77
77
|
specification_version: 4
|
78
78
|
summary: Collection of useful features for Ruby/Rails Classes
|
79
79
|
test_files:
|
80
|
-
- test/numeric_test.rb
|
81
80
|
- test/array_test.rb
|
82
81
|
- test/dummy_class.rb
|
83
|
-
- test/string_test.rb
|
84
82
|
- test/hash_test.rb
|
85
|
-
|
83
|
+
- test/numeric_test.rb
|
84
|
+
- test/string_test.rb
|