better_struct 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: e0fc6242d89b02de6ec2d288148e4081e11f355a
4
+ data.tar.gz: 9b8b1340921eb515841cb59524bffe739713ae9c
5
+ SHA512:
6
+ metadata.gz: 665dc045be6a41c18f80e32b510bc3c3db71307e37c132b5867d8bb7c1c140b0e2b7f3dedd7b31973be83929c0ea00ba8c904d07fbf79df1345f1f23acc97fdf
7
+ data.tar.gz: fb9d7a520a729da768b644446a20c9514e12b8aa801cb8ebd8fdc2e2c86069c5e76225ff184f3f620aa5e0d9ecd58a573bd28793162c7d43b193e393f3923a14
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in better_struct.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 exAspArk
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # BetterStruct
2
+
3
+ Use your data without pain.
4
+ It behaves like OpenStruct with monads. Look at the examples below.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'better_struct'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install better_struct
21
+
22
+ ## Usage
23
+
24
+ #### Maybe monad
25
+
26
+ ```ruby
27
+ BetterStruct.new(nil) == BetterStruct.new(nil).some_method # => true
28
+ ```
29
+
30
+ #### Everything is wrapped
31
+
32
+ ```ruby
33
+ better_struct = BetterStruct.new("foobar")
34
+
35
+
36
+ better_struct[0..2].is_a?(BetterStruct) # => true
37
+ better_struct[0..2] == BetterStruct.new("foo") # => true
38
+ ```
39
+
40
+ ```ruby
41
+ better_struct = BetterStruct.new([1, 2, 3])
42
+ result = better_struct.all? { |i| i.is_a?(BetterStruct) }
43
+
44
+ result.is_a?(BetterStruct) # => true
45
+ result == BetterStruct.new(true) # => true
46
+
47
+ ```
48
+
49
+ #### Like OpenStruct
50
+
51
+ ```ruby
52
+ better_struct = BetterStruct.new({ "FooBar1" => { foo_bar2: "Hello World!" } })
53
+
54
+ better_struct.foo_bar1.foo_bar2 == BetterStruct.new("Hello World!") # => true
55
+ ```
56
+
57
+ #### Leaving the monad
58
+
59
+ ```ruby
60
+ better_struct = BetterStruct.new("foobar")
61
+ new_better_struct = better_struct.gsub("foo", "super-")
62
+
63
+ new_better_struct.value == "super-foo" # => true
64
+
65
+ ```
66
+
67
+ ## Testing
68
+
69
+ $ ruby -Ilib:test test/better_struct_test.rb
70
+
71
+ ## Contributing
72
+
73
+ 1. Fork it ( https://github.com/[my-github-username]/better_struct/fork )
74
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
75
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
76
+ 4. Push to the branch (`git push origin my-new-feature`)
77
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'better_struct/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "better_struct"
8
+ spec.version = BetterStruct::VERSION
9
+ spec.authors = ["Evgeny Li"]
10
+ spec.email = ["exaspark@gmail.com"]
11
+ spec.summary = %q{Use your data without pain}
12
+ spec.description = %q{Use your data without pain}
13
+ spec.homepage = "https://github.com/exAspArk/better_struct"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "minitest", "~> 5.5"
24
+ spec.add_development_dependency "pry", "~> 0.10"
25
+ end
@@ -0,0 +1,3 @@
1
+ class BetterStruct
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,64 @@
1
+ require "better_struct/version"
2
+ require "forwardable"
3
+
4
+ class BetterStruct
5
+ extend Forwardable
6
+
7
+ attr_reader :value
8
+ def_delegator :value, :to_s
9
+
10
+ def initialize(value)
11
+ @value = value
12
+ @defined_methods = {}
13
+
14
+ if value && value.respond_to?(:each_pair)
15
+ value.each_pair { |key, value| @defined_methods[underscore(key.to_s)] = value }
16
+ end
17
+ end
18
+
19
+ def ==(other)
20
+ value == other.value
21
+ end
22
+
23
+ def inspect
24
+ "#{ self.class }<#{ @value.inspect }>"
25
+ end
26
+
27
+ private
28
+
29
+ def wrap(value)
30
+ value.is_a?(self.class) ? self : self.class.new(value)
31
+ end
32
+
33
+ def wrap_block_arguments(*args, &block)
34
+ Proc.new do |*args|
35
+ wrapped_arguments = args.map { |arg| wrap(arg) }
36
+ block.call(*wrapped_arguments)
37
+ end
38
+ end
39
+
40
+ def underscore(camel_cased_word)
41
+ return camel_cased_word unless camel_cased_word =~ /[A-Z-]|::/
42
+
43
+ word = camel_cased_word.to_s.gsub(/::/, '/')
44
+ word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
45
+ word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
46
+ word.tr!("-", "_")
47
+ word.downcase!
48
+ word
49
+ end
50
+
51
+ def method_missing(*args, &block)
52
+ method_name = args.first.to_s
53
+
54
+ if value.respond_to?(method_name)
55
+ wrap(value.public_send(*args, &wrap_block_arguments(*args, &block)))
56
+ elsif @defined_methods[method_name] || value.nil?
57
+ wrap(@defined_methods[method_name])
58
+ else
59
+ error = NoMethodError.new "undefined method `#{ method_name }' for #{ self }", args
60
+ error.set_backtrace caller(1)
61
+ raise error
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,42 @@
1
+ require "better_struct"
2
+ require "minitest/autorun"
3
+ require "pry"
4
+
5
+ class BetterStructTest < Minitest::Test
6
+ def test_maybe_monad
7
+ better_struct = BetterStruct.new(nil)
8
+
9
+ assert_equal better_struct.some_method, better_struct
10
+ end
11
+
12
+ def test_result_wrapping
13
+ assert_equal BetterStruct.new("foobar")[0..2], BetterStruct.new("foo")
14
+ end
15
+
16
+ def test_getting_value
17
+ better_struct = BetterStruct.new("foobar")
18
+
19
+ assert_equal better_struct.value, "foobar"
20
+ end
21
+
22
+ def test_unwrapped_to_s_method
23
+ better_struct = BetterStruct.new(1)
24
+
25
+ assert_equal better_struct.to_s, "1"
26
+ end
27
+
28
+ def test_block_argument_wrapping
29
+ better_struct = BetterStruct.new([3, 2, 1])
30
+
31
+ better_struct.each_with_index do |value, index|
32
+ assert_equal value.class, BetterStruct
33
+ assert_equal index.class, BetterStruct
34
+ end
35
+ end
36
+
37
+ def test_hash_methods
38
+ better_struct = BetterStruct.new({ "FooBar1" => { foo_bar2: "Hello World!" } })
39
+
40
+ assert_equal better_struct.foo_bar1.foo_bar2, BetterStruct.new("Hello World!")
41
+ end
42
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: better_struct
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Evgeny Li
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '5.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '5.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '0.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '0.10'
69
+ description: Use your data without pain
70
+ email:
71
+ - exaspark@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - Gemfile
77
+ - LICENSE.txt
78
+ - README.md
79
+ - Rakefile
80
+ - better_struct.gemspec
81
+ - lib/better_struct.rb
82
+ - lib/better_struct/version.rb
83
+ - test/better_struct_test.rb
84
+ homepage: https://github.com/exAspArk/better_struct
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.2.2
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: Use your data without pain
108
+ test_files:
109
+ - test/better_struct_test.rb
110
+ has_rdoc: