better_struct 0.2.3 → 0.2.4
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 +16 -16
- data/lib/better_struct/methodize.rb +19 -25
- data/lib/better_struct/version.rb +1 -1
- data/test/better_struct_test.rb +13 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6dc03c111c603fad4d9d16ba6683b5a357dbbb55
|
4
|
+
data.tar.gz: 48ebb2ba1af8a444ab8c15d463f31d8119c6efd6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ad186e5220cd663d77719c255de8ce1575420ea564f2bb1c70e623984db6382c28bbf2322a8f8f9ee473e098eeffef7e333000b84ac5f2dfdf95f536166723a1
|
7
|
+
data.tar.gz: 86dd02f3f9e1b5b11d4f8f78dd17bd8eb1a992a8150d7e515a80ba21f306a3c99d09ae3519712ff300407d2bf262cdae6f6a464beeb6207237076bac6ecc84d8
|
data/lib/better_struct.rb
CHANGED
@@ -30,56 +30,56 @@ class BetterStruct
|
|
30
30
|
|
31
31
|
private
|
32
32
|
|
33
|
-
def
|
33
|
+
def __wrap(value)
|
34
34
|
value.is_a?(self.class) ? self : self.class.new(value)
|
35
35
|
end
|
36
36
|
|
37
|
-
def
|
37
|
+
def __wrap_block_args
|
38
38
|
return unless block_given?
|
39
39
|
|
40
40
|
Proc.new do |*args|
|
41
|
-
wrapped_arguments = args.map! { |arg|
|
41
|
+
wrapped_arguments = args.map! { |arg| __wrap(arg) }
|
42
42
|
yield(*wrapped_arguments)
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
46
|
def method_missing(method_name, *args, &block)
|
47
47
|
if value.respond_to?(method_name)
|
48
|
-
|
49
|
-
elsif
|
50
|
-
@
|
48
|
+
__delegate_method(method_name, *args, &block)
|
49
|
+
elsif __assignment?(method_name) && __defined_methods
|
50
|
+
@__defined_methods[__methodize(method_name[0...-1])] = args.first
|
51
51
|
else
|
52
|
-
|
52
|
+
__wrap(__defined_methods[method_name.to_s])
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
56
|
-
def
|
56
|
+
def __assignment?(method_name)
|
57
57
|
method_name[-1] == EQUAL_SIGN
|
58
58
|
end
|
59
59
|
|
60
|
-
def
|
61
|
-
@
|
60
|
+
def __defined_methods
|
61
|
+
@__defined_methods ||= begin
|
62
62
|
result = {}
|
63
63
|
|
64
64
|
if value && value.respond_to?(:each_pair)
|
65
|
-
value.each_pair { |key, v| result[
|
65
|
+
value.each_pair { |key, v| result[__methodize(key.to_s)] = v }
|
66
66
|
end
|
67
67
|
|
68
68
|
result
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
72
|
-
def
|
73
|
-
result = value.public_send(method_name, *
|
72
|
+
def __delegate_method(method_name, *args, &block)
|
73
|
+
result = value.public_send(method_name, *__unwrap_items(args), &__wrap_block_args(&block))
|
74
74
|
|
75
75
|
if MAP_METHOD_NAMES.include?(method_name)
|
76
|
-
|
76
|
+
__wrap(__unwrap_items(result))
|
77
77
|
else
|
78
|
-
|
78
|
+
__wrap(result)
|
79
79
|
end
|
80
80
|
end
|
81
81
|
|
82
|
-
def
|
82
|
+
def __unwrap_items(items)
|
83
83
|
if items.is_a?(Array)
|
84
84
|
items.map! { |item| item.is_a?(self.class) ? item.value : item }
|
85
85
|
else
|
@@ -5,11 +5,10 @@ class BetterStruct
|
|
5
5
|
TRANSLITERATION_FROM = "ÀÁÂÃÄÅàáâãäåĀāĂ㥹ÇçĆćĈĉĊċČčÐðĎďĐđÈÉÊËèéêëĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħÌÍÎÏìíîïĨĩĪīĬĭĮįİıĴĵĶķĸĹĺĻļĽľĿŀŁłÑñŃńŅņŇňʼnŊŋÒÓÔÕÖØòóôõöøŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞşŠšſŢţŤťŦŧÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲųŴŵÝýÿŶŷŸŹźŻżŽž".freeze
|
6
6
|
TRANSLITERATION_TO = "AAAAAAaaaaaaAaAaAaCcCcCcCcCcDdDdDdEEEEeeeeEeEeEeEeEeGgGgGgGgHhHhIIIIiiiiIiIiIiIiIiJjKkkLlLlLlLlLlNnNnNnNnnNnOOOOOOooooooOoOoOoRrRrRrSsSsSsSssTtTtTtUUUUuuuuUuUuUuUuUuUuWwYyyYyYZzZzZz".freeze
|
7
7
|
|
8
|
-
|
9
|
-
|
8
|
+
NON_UNDERSCORED_BEGIN_OR_END_REGEXP = /^[^a-z0-9_]+|[^a-z0-9_]+$/.freeze
|
9
|
+
BEFORE_FIRST_DIGIT_OR_NON_UNDERSCORED_REGEXP = /^(.)?(?=[0-9])|[^a-z0-9_]/.freeze
|
10
10
|
NON_ENGLISH_REGEXP = /[#{ TRANSLITERATION_FROM }]/.freeze
|
11
11
|
UNDERSCORE_DUPLICATES_REGEXP = /#{ UNDERSCORE_SIGN }{2,}/.freeze
|
12
|
-
UNDERSCORE_END_REGEXP = /#{ UNDERSCORE_SIGN }$/.freeze
|
13
12
|
CAMELCASE_ABBREVIATION_REGEX = /([A-Z\d]+)([A-Z][a-z])/.freeze
|
14
13
|
CAMELCASE_REGEX = /([a-z\d])([A-Z])/.freeze
|
15
14
|
|
@@ -17,38 +16,33 @@ class BetterStruct
|
|
17
16
|
|
18
17
|
private
|
19
18
|
|
20
|
-
def
|
21
|
-
|
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
|
19
|
+
def __methodize(string)
|
20
|
+
return string if __methodized?(string)
|
29
21
|
|
30
|
-
duplicated_string = string.dup
|
22
|
+
duplicated_string = string.dup
|
31
23
|
|
32
|
-
|
33
|
-
|
24
|
+
__transliterate!(duplicated_string)
|
25
|
+
__underscore!(duplicated_string)
|
34
26
|
|
35
27
|
duplicated_string
|
36
28
|
end
|
37
29
|
|
38
|
-
def
|
30
|
+
def __methodized?(string)
|
31
|
+
!string =~ BEFORE_FIRST_DIGIT_OR_NON_UNDERSCORED_REGEXP && !string =~ UNDERSCORE_DUPLICATES_REGEXP
|
32
|
+
end
|
33
|
+
|
34
|
+
def __transliterate!(string)
|
39
35
|
if string =~ NON_ENGLISH_REGEXP
|
40
36
|
string.tr!(TRANSLITERATION_FROM, TRANSLITERATION_TO)
|
41
37
|
end
|
42
38
|
end
|
43
39
|
|
44
|
-
def
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
string.gsub!(UNDERSCORE_END_REGEXP, EMPTY_STRING)
|
52
|
-
end
|
40
|
+
def __underscore!(string)
|
41
|
+
string.gsub!(CAMELCASE_ABBREVIATION_REGEX, UNDERSCORE_MASK)
|
42
|
+
string.gsub!(CAMELCASE_REGEX, UNDERSCORE_MASK)
|
43
|
+
string.downcase!
|
44
|
+
string.gsub!(NON_UNDERSCORED_BEGIN_OR_END_REGEXP, EMPTY_STRING)
|
45
|
+
string.gsub!(BEFORE_FIRST_DIGIT_OR_NON_UNDERSCORED_REGEXP, UNDERSCORE_SIGN)
|
46
|
+
string.gsub!(UNDERSCORE_DUPLICATES_REGEXP, UNDERSCORE_SIGN)
|
53
47
|
end
|
54
48
|
end
|
data/test/better_struct_test.rb
CHANGED
@@ -78,12 +78,24 @@ module BetterStructTest
|
|
78
78
|
assert better_struct.map { |i| i * i }.value == [1, 4, 9]
|
79
79
|
end
|
80
80
|
|
81
|
-
def
|
81
|
+
def test_methods_underscoring
|
82
|
+
better_struct = BetterStruct.new({ "! CHAMPION !" => 1 })
|
83
|
+
|
84
|
+
assert better_struct.champion.value == 1
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_methods_underscoring_started_with_digit
|
82
88
|
better_struct = BetterStruct.new({ "1 Word (With-Space)" => 1 })
|
83
89
|
|
84
90
|
assert better_struct._1_word_with_space.value == 1
|
85
91
|
end
|
86
92
|
|
93
|
+
def test_methods_underscoring_with_underscores_around
|
94
|
+
better_struct = BetterStruct.new({ "_Abc__Def_" => 1 })
|
95
|
+
|
96
|
+
assert better_struct._abc_def_.value == 1
|
97
|
+
end
|
98
|
+
|
87
99
|
def test_transliteration
|
88
100
|
better_struct = BetterStruct.new({ "Título" => 1 })
|
89
101
|
|