stub 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: aac8e9322e87430ca231220e9cf7ec77fcc03e77
4
+ data.tar.gz: 0cdf2b82494a9600c183ce1dbb28522908851921
5
+ SHA512:
6
+ metadata.gz: 372e56e33f60333cae130268911d28262a7ecb102f246089e033f4fa2a60a86e2f8809b9a8a3a06cb5bf3f42f9574b360de90da68599ed6e997f2021369984e9
7
+ data.tar.gz: c9434fee967ad34e8dde37e04b51732343b998a07fea77174e11a50157b2febb323d2b2326295ff0cbd904dd53b65015ddafeb887500ebfea8c9c24ead1d2339
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in stub.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Alexander Senko
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,61 @@
1
+ # Stub
2
+
3
+ Proxy objects with fallbacks.
4
+ Useful for creating stubs with partially defined objects.
5
+
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'stub'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install stub
20
+
21
+
22
+ ## Usage
23
+
24
+ Inherit `Stub::Abstract` (or `Stub::Template::Abstract`) and define `__stub__`
25
+ method:
26
+
27
+ class MyStub < Stub::Abstract
28
+ module Methods
29
+ def __stub__ method_name, *args, &block
30
+ # Do something when method fails on the proxied object
31
+ # or returns nil.
32
+ end
33
+ end
34
+ end
35
+
36
+ See also `Stub::Template::Prototype` as a working example.
37
+
38
+ ### `Stub::Template::Prototype`
39
+
40
+ Use a proxied object to generate placeholders in place of _absent_ attributes &
41
+ methods:
42
+
43
+ render Stub::Template::Prototype.new(MyModel.new)
44
+
45
+ Later you can use this `render`ed template in Prototype's `new Template()` and
46
+ `evaluate` it with data from `MyModel.find`. Example (don't forget escaping in
47
+ real-life code):
48
+
49
+ var template = '<%= render Stub::Template::Prototype.new(MyModel.new) %>';
50
+ var record = JSON.parse('<%= my_model.to_json %>');
51
+
52
+ text = new Template(template).evaluate(record);
53
+
54
+
55
+ ## Contributing
56
+
57
+ 1. Fork it
58
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
59
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
60
+ 4. Push to the branch (`git push origin my-new-feature`)
61
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
data/lib/stub.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'stub/version'
2
+
3
+ module Stub
4
+ autoload :Abstract, 'stub/abstract'
5
+ autoload :Template, 'stub/template'
6
+ end
@@ -0,0 +1,46 @@
1
+ module Stub
2
+ class Abstract
3
+ def self.inherited subclass
4
+ eval <<-RUBY
5
+ module #{subclass}::Methods
6
+ include #{self}::Methods
7
+ end
8
+ RUBY
9
+ end
10
+
11
+ def initialize object
12
+ @object = object
13
+ @stub_class = self.class
14
+
15
+ singleton_class = self.singleton_class
16
+
17
+ methods.each &singleton_class.method(:undef_method)
18
+ singleton_class.send :include, @stub_class.const_get(:Methods)
19
+ end
20
+
21
+ module Methods
22
+ def method_missing method_name, *args, &block
23
+ result = @object.send method_name, *args, &block
24
+
25
+ method_name = args.shift if [ :send, :__send__ ].include? method_name
26
+
27
+ case result
28
+ when nil
29
+ raise
30
+ when true, false, String, Numeric, Array, Hash
31
+ result
32
+ else
33
+ __wrap__ result, method_name, *args, &block
34
+ end
35
+ rescue => e
36
+ $stderr.puts "#{e.class}: #{e.message} in #{@object.class}##{method_name}(#{args.map(&:inspect)*','})" if e.message.present?
37
+
38
+ __stub__ method_name, *args, &block
39
+ end
40
+
41
+ def __wrap__ object, *context
42
+ @stub_class.new object
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,6 @@
1
+ module Stub
2
+ module Template
3
+ autoload :Abstract, 'stub/template/abstract'
4
+ autoload :Prototype, 'stub/template/prototype'
5
+ end
6
+ end
@@ -0,0 +1,21 @@
1
+ require 'stub/abstract'
2
+
3
+ module Stub::Template
4
+ class Abstract < Stub::Abstract
5
+ def initialize resource, call_chain = nil
6
+ @call_chain = call_chain
7
+
8
+ super resource
9
+ end
10
+
11
+ module Methods
12
+ def __wrap__ object, method_name, *args, &block
13
+ @stub_class.new object, __call_chain__(method_name)
14
+ end
15
+
16
+ def __call_chain__ method_name
17
+ [@call_chain, method_name].compact * '.'
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,11 @@
1
+ require 'stub/template/abstract'
2
+
3
+ module Stub::Template
4
+ class Prototype < Abstract
5
+ module Methods
6
+ def __stub__ method_name, *args, &block
7
+ "\#{#{__call_chain__ method_name}}"
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module Stub
2
+ VERSION = '0.1.0'
3
+ end
data/stub.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'stub/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'stub'
8
+ spec.version = Stub::VERSION
9
+ spec.authors = [ 'Alexander Senko' ]
10
+ spec.email = [ 'Alexander.Senko@gmail.com' ]
11
+ spec.description = 'Useful for creating stubs with partially defined objects.'
12
+ spec.summary = 'Proxy objects with fallbacks.'
13
+ spec.homepage = 'https://github.com/softpro/stub'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
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.3'
22
+ spec.add_development_dependency 'rake'
23
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stub
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Senko
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-03-26 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Useful for creating stubs with partially defined objects.
42
+ email:
43
+ - Alexander.Senko@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/stub.rb
54
+ - lib/stub/abstract.rb
55
+ - lib/stub/template.rb
56
+ - lib/stub/template/abstract.rb
57
+ - lib/stub/template/prototype.rb
58
+ - lib/stub/version.rb
59
+ - stub.gemspec
60
+ homepage: https://github.com/softpro/stub
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.0.3
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: Proxy objects with fallbacks.
84
+ test_files: []