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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
- metadata.gz: 3cc67117521966619def878e57d6df34ec17410e
4
- data.tar.gz: ba91831def735866a96254a596284fca5b9111b0
3
+ metadata.gz: 6dc03c111c603fad4d9d16ba6683b5a357dbbb55
4
+ data.tar.gz: 48ebb2ba1af8a444ab8c15d463f31d8119c6efd6
5
5
  SHA512:
6
- metadata.gz: 444de9218c1a00233e010b8d4515edb45ba2c3080b9d495847a08b564f65debd17aba08a59b2cd33001fd956ca31cc562a44fb97ba6408e809e6dd7e1ba2ac6a
7
- data.tar.gz: 692de0583b3d1631f3417f259186615c017c3885202ca6dd6e762bd62aa17665b02b3230ba02c738f1235ced80105022a4333c4af394e1847bd5eae2ca3fd2b7
6
+ metadata.gz: ad186e5220cd663d77719c255de8ce1575420ea564f2bb1c70e623984db6382c28bbf2322a8f8f9ee473e098eeffef7e333000b84ac5f2dfdf95f536166723a1
7
+ data.tar.gz: 86dd02f3f9e1b5b11d4f8f78dd17bd8eb1a992a8150d7e515a80ba21f306a3c99d09ae3519712ff300407d2bf262cdae6f6a464beeb6207237076bac6ecc84d8
@@ -30,56 +30,56 @@ class BetterStruct
30
30
 
31
31
  private
32
32
 
33
- def wrap(value)
33
+ def __wrap(value)
34
34
  value.is_a?(self.class) ? self : self.class.new(value)
35
35
  end
36
36
 
37
- def wrap_block_args
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| wrap(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
- delegate_method(method_name, *args, &block)
49
- elsif assignment?(method_name) && defined_methods
50
- @defined_methods[methodize(method_name[0...-1])] = args.first
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
- wrap(defined_methods[method_name.to_s])
52
+ __wrap(__defined_methods[method_name.to_s])
53
53
  end
54
54
  end
55
55
 
56
- def assignment?(method_name)
56
+ def __assignment?(method_name)
57
57
  method_name[-1] == EQUAL_SIGN
58
58
  end
59
59
 
60
- def defined_methods
61
- @defined_methods ||= begin
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[methodize(key.to_s)] = v }
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 delegate_method(method_name, *args, &block)
73
- result = value.public_send(method_name, *unwrap_items(args), &wrap_block_args(&block))
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
- wrap(unwrap_items(result))
76
+ __wrap(__unwrap_items(result))
77
77
  else
78
- wrap(result)
78
+ __wrap(result)
79
79
  end
80
80
  end
81
81
 
82
- def unwrap_items(items)
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
- DIGIT_REGEXP = /[0-9]/.freeze
9
- NOT_UNDERSCORED_REGEXP = /[^a-z0-9_]/.freeze
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 methodize(string)
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
19
+ def __methodize(string)
20
+ return string if __methodized?(string)
29
21
 
30
- duplicated_string = string.dup if duplicated_string.nil?
22
+ duplicated_string = string.dup
31
23
 
32
- transliterate!(duplicated_string)
33
- underscore!(duplicated_string)
24
+ __transliterate!(duplicated_string)
25
+ __underscore!(duplicated_string)
34
26
 
35
27
  duplicated_string
36
28
  end
37
29
 
38
- def transliterate!(string)
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 underscore!(string)
45
- if string =~ NOT_UNDERSCORED_REGEXP
46
- string.gsub!(CAMELCASE_ABBREVIATION_REGEX, UNDERSCORE_MASK)
47
- string.gsub!(CAMELCASE_REGEX, UNDERSCORE_MASK)
48
- string.downcase!
49
- string.gsub!(NOT_UNDERSCORED_REGEXP, UNDERSCORE_SIGN)
50
- string.gsub!(UNDERSCORE_DUPLICATES_REGEXP, UNDERSCORE_SIGN)
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
@@ -1,3 +1,3 @@
1
1
  class BetterStruct
2
- VERSION = "0.2.3".freeze
2
+ VERSION = "0.2.4".freeze
3
3
  end
@@ -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 test_underscoring_methods
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
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: better_struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evgeny Li