better_struct 0.2.4 → 0.3.0

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
- !binary "U0hBMQ==":
3
- metadata.gz: 6dc03c111c603fad4d9d16ba6683b5a357dbbb55
4
- data.tar.gz: 48ebb2ba1af8a444ab8c15d463f31d8119c6efd6
2
+ SHA1:
3
+ metadata.gz: 769d8c773ce3b173db61b643690f593dfb5fecf3
4
+ data.tar.gz: 7647a81cd6a1b7b25a7f23dad153e3a5ebb3dfa4
5
5
  SHA512:
6
- metadata.gz: ad186e5220cd663d77719c255de8ce1575420ea564f2bb1c70e623984db6382c28bbf2322a8f8f9ee473e098eeffef7e333000b84ac5f2dfdf95f536166723a1
7
- data.tar.gz: 86dd02f3f9e1b5b11d4f8f78dd17bd8eb1a992a8150d7e515a80ba21f306a3c99d09ae3519712ff300407d2bf262cdae6f6a464beeb6207237076bac6ecc84d8
6
+ metadata.gz: 1dd5b1022f097f919d9b65883301950f643bda6d145cc6eee14e32ef3c8c8c0686b90f888193415ea3b5f1110a755227c152dbf8adb8d227c3dd4fa53cde54ce
7
+ data.tar.gz: d4fd8c5ed520589ee4d4981dbfb5dddfa3573202f629c8b483bec691c9cfb241b760b75c27f453f483fb461c67d1e892857c15da3832a7d624b67778f269f60e
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ Gemfile.lock
data/README.md CHANGED
@@ -7,11 +7,11 @@
7
7
  It behaves like an OpenStruct on steroids with monad.
8
8
 
9
9
  ```ruby
10
- hash = { "FooBar1" => { foo_bar2: "Hello World!" } }
10
+ hash = { "FooBar1" => { "FooBar2" => "Hello World!" } }
11
11
 
12
12
  # Instead of this:
13
- if hash["FooBar1"] && hash["FooBar1"][:foo_bar2] && hash["FooBar1"][:foo_bar2].respond_to?(:sub)
14
- hash["FooBar1"][:foo_bar2].sub("Hello ", "") # => "World!"
13
+ if hash["FooBar1"] && hash["FooBar1"]["FooBar2"] && hash["FooBar1"]["FooBar2"].respond_to?(:sub)
14
+ hash["FooBar1"]["FooBar2"].sub("Hello ", "") # => "World!"
15
15
  end
16
16
 
17
17
  # Simply use:
@@ -59,7 +59,7 @@ better_struct.all? { |i| i.is_a?(BetterStruct) } == BetterStruct.new(true) # =>
59
59
  #### Like an OpenStruct on steroids
60
60
 
61
61
  ```ruby
62
- some_hash = { "FooBar1" => { foo_bar2: "Hello World!" } }
62
+ some_hash = { foo_bar1: { foo_bar2: "Hello World!" } }
63
63
  better_struct = BetterStruct.new(some_hash)
64
64
 
65
65
  better_struct.foo_bar1.foo_bar2 == BetterStruct.new("Hello World!") # => true
@@ -70,7 +70,7 @@ better_struct.foo_bar1.foo_bar2 == BetterStruct.new("Hello World!") # => true
70
70
  ```ruby
71
71
  better_struct = BetterStruct.new("foobar")
72
72
 
73
- better_struct.gsub("foo", "super-").value == "super-foo" # => true
73
+ better_struct.gsub("foo", "super-").value == "super-bar" # => true
74
74
 
75
75
  ```
76
76
 
@@ -16,7 +16,8 @@ class BetterStruct
16
16
 
17
17
  private
18
18
 
19
- def __methodize(string)
19
+ def __methodize(value)
20
+ string = value.to_s
20
21
  return string if __methodized?(string)
21
22
 
22
23
  duplicated_string = string.dup
@@ -1,3 +1,3 @@
1
1
  class BetterStruct
2
- VERSION = "0.2.4".freeze
2
+ VERSION = "0.3.0".freeze
3
3
  end
data/lib/better_struct.rb CHANGED
@@ -4,12 +4,13 @@ require_relative "./better_struct/methodize"
4
4
 
5
5
  class BetterStruct
6
6
  EQUAL_SIGN = "=".freeze
7
- MAP_METHOD_NAMES = %i(map map!).to_set.freeze
7
+ MAP_METHOD_NAMES = %w(map map!).to_set.freeze
8
8
 
9
9
  attr_reader :value
10
10
 
11
11
  def initialize(value = nil)
12
12
  @value = value
13
+ __load_defined_methods
13
14
  end
14
15
 
15
16
  def ==(other)
@@ -25,31 +26,28 @@ class BetterStruct
25
26
  end
26
27
 
27
28
  def empty?
28
- value.respond_to?(:empty?) ? !!value.empty? : !value
29
+ value.respond_to?(:empty?) ? value.empty? : !value
29
30
  end
30
31
 
31
32
  private
32
33
 
33
- def __wrap(value)
34
- value.is_a?(self.class) ? self : self.class.new(value)
35
- end
34
+ def __load_defined_methods
35
+ @__defined_methods = {}
36
36
 
37
- def __wrap_block_args
38
- return unless block_given?
39
-
40
- Proc.new do |*args|
41
- wrapped_arguments = args.map! { |arg| __wrap(arg) }
42
- yield(*wrapped_arguments)
37
+ if value && value.respond_to?(:each_pair)
38
+ value.each_pair { |key, v| @__defined_methods[__methodize(key)] = v }
43
39
  end
44
40
  end
45
41
 
46
42
  def method_missing(method_name, *args, &block)
43
+ method_name = method_name.to_s
44
+
47
45
  if value.respond_to?(method_name)
48
46
  __delegate_method(method_name, *args, &block)
49
- elsif __assignment?(method_name) && __defined_methods
50
- @__defined_methods[__methodize(method_name[0...-1])] = args.first
47
+ elsif __assignment?(method_name)
48
+ @__defined_methods[method_name[0...-1]] = args.first
51
49
  else
52
- __wrap(__defined_methods[method_name.to_s])
50
+ __wrap(@__defined_methods[method_name])
53
51
  end
54
52
  end
55
53
 
@@ -57,33 +55,31 @@ private
57
55
  method_name[-1] == EQUAL_SIGN
58
56
  end
59
57
 
60
- def __defined_methods
61
- @__defined_methods ||= begin
62
- result = {}
58
+ def __delegate_method(method_name, *args, &block)
59
+ result = value.public_send(method_name, *__unwrap_items(args), &__wrap_block_args(&block))
60
+ result = __unwrap_items(result) if MAP_METHOD_NAMES.include?(method_name)
63
61
 
64
- if value && value.respond_to?(:each_pair)
65
- value.each_pair { |key, v| result[__methodize(key.to_s)] = v }
66
- end
62
+ __wrap(result)
63
+ end
67
64
 
68
- result
69
- end
65
+ def __wrap(value)
66
+ value.is_a?(self.class) ? self : self.class.new(value)
70
67
  end
71
68
 
72
- def __delegate_method(method_name, *args, &block)
73
- result = value.public_send(method_name, *__unwrap_items(args), &__wrap_block_args(&block))
69
+ def __wrap_block_args
70
+ return unless block_given?
74
71
 
75
- if MAP_METHOD_NAMES.include?(method_name)
76
- __wrap(__unwrap_items(result))
77
- else
78
- __wrap(result)
72
+ Proc.new do |*args|
73
+ wrapped_arguments = args.map! { |arg| __wrap(arg) }
74
+ yield(*wrapped_arguments)
79
75
  end
80
76
  end
81
77
 
82
78
  def __unwrap_items(items)
83
- if items.is_a?(Array)
84
- items.map! { |item| item.is_a?(self.class) ? item.value : item }
85
- else
86
- items
87
- end
79
+ items.is_a?(Array) ? items.map! { |i| __unwrap(i) } : items
80
+ end
81
+
82
+ def __unwrap(value)
83
+ value.is_a?(self.class) ? value.value : value
88
84
  end
89
85
  end
@@ -75,7 +75,7 @@ module BetterStructTest
75
75
  def test_operators
76
76
  better_struct = BetterStruct.new([1, 2, 3])
77
77
 
78
- assert better_struct.map { |i| i * i }.value == [1, 4, 9]
78
+ assert better_struct.map { |i| i * i * 2 }.value == [2, 8, 18]
79
79
  end
80
80
 
81
81
  def test_methods_underscoring
metadata CHANGED
@@ -1,97 +1,97 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: better_struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.3.0
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-05-20 00:00:00.000000000 Z
11
+ date: 2016-03-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.7'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.7'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
33
  version: '10.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ~>
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: minitest
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
47
  version: '5.5'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ~>
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '5.5'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: pry-byebug
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ~>
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
61
  version: '2.0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ~>
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '2.0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: benchmark-ips
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ~>
73
+ - - "~>"
74
74
  - !ruby/object:Gem::Version
75
75
  version: '2.1'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ~>
80
+ - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '2.1'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: stackprof
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - ! '>='
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - ! '>='
94
+ - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  description: Use your data without pain
@@ -101,7 +101,8 @@ executables: []
101
101
  extensions: []
102
102
  extra_rdoc_files: []
103
103
  files:
104
- - .travis.yml
104
+ - ".gitignore"
105
+ - ".travis.yml"
105
106
  - Gemfile
106
107
  - LICENSE.txt
107
108
  - README.md
@@ -121,20 +122,19 @@ require_paths:
121
122
  - lib
122
123
  required_ruby_version: !ruby/object:Gem::Requirement
123
124
  requirements:
124
- - - ! '>='
125
+ - - ">="
125
126
  - !ruby/object:Gem::Version
126
127
  version: '0'
127
128
  required_rubygems_version: !ruby/object:Gem::Requirement
128
129
  requirements:
129
- - - ! '>='
130
+ - - ">="
130
131
  - !ruby/object:Gem::Version
131
132
  version: '0'
132
133
  requirements: []
133
134
  rubyforge_project:
134
- rubygems_version: 2.2.2
135
+ rubygems_version: 2.4.5.1
135
136
  signing_key:
136
137
  specification_version: 4
137
138
  summary: Use your data without pain
138
139
  test_files:
139
140
  - test/better_struct_test.rb
140
- has_rdoc: