better_struct 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/better_struct.rb +4 -4
- data/lib/better_struct/methodize.rb +16 -8
- data/lib/better_struct/version.rb +1 -1
- data/test/better_struct_test.rb +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3cc67117521966619def878e57d6df34ec17410e
|
4
|
+
data.tar.gz: ba91831def735866a96254a596284fca5b9111b0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 444de9218c1a00233e010b8d4515edb45ba2c3080b9d495847a08b564f65debd17aba08a59b2cd33001fd956ca31cc562a44fb97ba6408e809e6dd7e1ba2ac6a
|
7
|
+
data.tar.gz: 692de0583b3d1631f3417f259186615c017c3885202ca6dd6e762bd62aa17665b02b3230ba02c738f1235ced80105022a4333c4af394e1847bd5eae2ca3fd2b7
|
data/lib/better_struct.rb
CHANGED
@@ -34,12 +34,12 @@ private
|
|
34
34
|
value.is_a?(self.class) ? self : self.class.new(value)
|
35
35
|
end
|
36
36
|
|
37
|
-
def wrap_block_args
|
38
|
-
return
|
37
|
+
def wrap_block_args
|
38
|
+
return unless block_given?
|
39
39
|
|
40
40
|
Proc.new do |*args|
|
41
41
|
wrapped_arguments = args.map! { |arg| wrap(arg) }
|
42
|
-
|
42
|
+
yield(*wrapped_arguments)
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
@@ -62,7 +62,7 @@ private
|
|
62
62
|
result = {}
|
63
63
|
|
64
64
|
if value && value.respond_to?(:each_pair)
|
65
|
-
value.each_pair { |key,
|
65
|
+
value.each_pair { |key, v| result[methodize(key.to_s)] = v }
|
66
66
|
end
|
67
67
|
|
68
68
|
result
|
@@ -5,11 +5,11 @@ class BetterStruct
|
|
5
5
|
TRANSLITERATION_FROM = "ÀÁÂÃÄÅàáâãäåĀāĂ㥹ÇçĆćĈĉĊċČčÐðĎďĐđÈÉÊËèéêëĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħÌÍÎÏìíîïĨĩĪīĬĭĮįİıĴĵĶķĸĹĺĻļĽľĿŀŁłÑñŃńŅņŇňʼnŊŋÒÓÔÕÖØòóôõöøŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞşŠšſŢţŤťŦŧÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲųŴŵÝýÿŶŷŸŹźŻżŽž".freeze
|
6
6
|
TRANSLITERATION_TO = "AAAAAAaaaaaaAaAaAaCcCcCcCcCcDdDdDdEEEEeeeeEeEeEeEeEeGgGgGgGgHhHhIIIIiiiiIiIiIiIiIiJjKkkLlLlLlLlLlNnNnNnNnnNnOOOOOOooooooOoOoOoRrRrRrSsSsSsSssTtTtTtUUUUuuuuUuUuUuUuUuUuWwYyyYyYZzZzZz".freeze
|
7
7
|
|
8
|
-
|
8
|
+
DIGIT_REGEXP = /[0-9]/.freeze
|
9
9
|
NOT_UNDERSCORED_REGEXP = /[^a-z0-9_]/.freeze
|
10
10
|
NON_ENGLISH_REGEXP = /[#{ TRANSLITERATION_FROM }]/.freeze
|
11
11
|
UNDERSCORE_DUPLICATES_REGEXP = /#{ UNDERSCORE_SIGN }{2,}/.freeze
|
12
|
-
|
12
|
+
UNDERSCORE_END_REGEXP = /#{ UNDERSCORE_SIGN }$/.freeze
|
13
13
|
CAMELCASE_ABBREVIATION_REGEX = /([A-Z\d]+)([A-Z][a-z])/.freeze
|
14
14
|
CAMELCASE_REGEX = /([a-z\d])([A-Z])/.freeze
|
15
15
|
|
@@ -18,13 +18,21 @@ class BetterStruct
|
|
18
18
|
private
|
19
19
|
|
20
20
|
def methodize(string)
|
21
|
-
|
21
|
+
if string[0] =~ DIGIT_REGEXP
|
22
|
+
duplicated_string = string.dup
|
23
|
+
duplicated_string.prepend(UNDERSCORE_SIGN)
|
24
|
+
end
|
25
|
+
|
26
|
+
unless string =~ NOT_UNDERSCORED_REGEXP
|
27
|
+
return duplicated_string || string
|
28
|
+
end
|
29
|
+
|
30
|
+
duplicated_string = string.dup if duplicated_string.nil?
|
22
31
|
|
23
|
-
|
32
|
+
transliterate!(duplicated_string)
|
33
|
+
underscore!(duplicated_string)
|
24
34
|
|
25
|
-
|
26
|
-
underscore!(string)
|
27
|
-
string
|
35
|
+
duplicated_string
|
28
36
|
end
|
29
37
|
|
30
38
|
def transliterate!(string)
|
@@ -40,7 +48,7 @@ private
|
|
40
48
|
string.downcase!
|
41
49
|
string.gsub!(NOT_UNDERSCORED_REGEXP, UNDERSCORE_SIGN)
|
42
50
|
string.gsub!(UNDERSCORE_DUPLICATES_REGEXP, UNDERSCORE_SIGN)
|
43
|
-
string.gsub!(
|
51
|
+
string.gsub!(UNDERSCORE_END_REGEXP, EMPTY_STRING)
|
44
52
|
end
|
45
53
|
end
|
46
54
|
end
|
data/test/better_struct_test.rb
CHANGED
@@ -79,9 +79,9 @@ module BetterStructTest
|
|
79
79
|
end
|
80
80
|
|
81
81
|
def test_underscoring_methods
|
82
|
-
better_struct = BetterStruct.new({ "Word (With-Space)" => 1 })
|
82
|
+
better_struct = BetterStruct.new({ "1 Word (With-Space)" => 1 })
|
83
83
|
|
84
|
-
assert better_struct.
|
84
|
+
assert better_struct._1_word_with_space.value == 1
|
85
85
|
end
|
86
86
|
|
87
87
|
def test_transliteration
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: better_struct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Evgeny Li
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-05-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|