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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
- metadata.gz: 262cd7ded259c467148ba2682fc571feec48a498
4
- data.tar.gz: 141737a24c6d7251e08c22ad26c28d222ba12d1f
3
+ metadata.gz: 3cc67117521966619def878e57d6df34ec17410e
4
+ data.tar.gz: ba91831def735866a96254a596284fca5b9111b0
5
5
  SHA512:
6
- metadata.gz: a52d2c6cdda7576685cc97ba9108121d5b9f8b60f817b8a1f8195614d1adc603ecb0e2b7e13fe60d6fbfc5d73b5848591f1d33feb2054133f8356dac1aed626e
7
- data.tar.gz: 0038c03c2694d9520e7d8f8749343fbb2179549076607eb3513ba6e66cf7de4896cdacef7ad82db4d7ba5285f906d34706ef6ff510eebdfb8968de561fc243a4
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(&block)
38
- return if block.nil?
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
- block.call(*wrapped_arguments)
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, value| result[methodize(key.to_s)] = value }
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
- UPCASE_REGEXP = /[A-Z]/.freeze
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
- UNDERSCORE_BEGIN_OR_END_REGEXP = /^#{ UNDERSCORE_SIGN }|#{ UNDERSCORE_SIGN }$/.freeze
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
- return string unless string =~ NOT_UNDERSCORED_REGEXP
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
- string = string.dup
32
+ transliterate!(duplicated_string)
33
+ underscore!(duplicated_string)
24
34
 
25
- transliterate!(string)
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!(UNDERSCORE_BEGIN_OR_END_REGEXP, EMPTY_STRING)
51
+ string.gsub!(UNDERSCORE_END_REGEXP, EMPTY_STRING)
44
52
  end
45
53
  end
46
54
  end
@@ -1,3 +1,3 @@
1
1
  class BetterStruct
2
- VERSION = "0.2.2".freeze
2
+ VERSION = "0.2.3".freeze
3
3
  end
@@ -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.word_with_space.value == 1
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.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-03-30 00:00:00.000000000 Z
11
+ date: 2015-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler