minidbc 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e59101c087693aadb6c7fbcf3bd9f9d817a30b7a
4
+ data.tar.gz: 736919d3ea3376968990d995635f82ed6714bdd5
5
+ SHA512:
6
+ metadata.gz: 4fe68f105bb88ee61a928647b9e2cb6e86182b0200d0e7ded0fd52c15328073294dbb3e77715b265499e071a1b7033b4638e8e872ec6a77e6c2732d28f9b4c16
7
+ data.tar.gz: b287ff187068ecaffe2b4ac34cfd1168de1f0c6d3a8eb945ffbd8f9acde8d5d6c9e22dd81302be1c3cba58740c43b31b84548a8928d1e3bdfcd89853dcca445b
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .*.swp
11
+ .byebug_history
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.15.3
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in minidbc.gemspec
6
+ gemspec
@@ -0,0 +1,34 @@
1
+ # Minidbc
2
+
3
+ Minimalist version of Dbc for ruby. Only implement pre, post and invariant. Not consider a lot of case involve meta programming. Use it carefully and take your own risk.
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'minidbc'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install minidbc
21
+
22
+ ## Usage
23
+
24
+ TODO: Write usage instructions here
25
+
26
+ ## Development
27
+
28
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
29
+
30
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
31
+
32
+ ## Contributing
33
+
34
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/minidbc.
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "minidbc"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,176 @@
1
+ require "minidbc/version"
2
+
3
+ module Minidbc
4
+ class DesignContractViolationException < Exception
5
+ end
6
+
7
+ def self.included(clz)
8
+ clz.extend ClassMethods
9
+ clz.minidbc_init
10
+ end
11
+
12
+ def minidbc_check_condition( condition_list, *args )
13
+ condition_list.each do |cond|
14
+ if !instance_exec(*args, &cond.blk)
15
+ ( file, lineno ) = cond.location.split(":")
16
+ puts "Contract Violation at #{cond.location}"
17
+ if File.exist? file
18
+ puts File.open(file).readlines[lineno.to_i - 1]
19
+ end
20
+ raise DesignContractViolationException.new( "Contact failed at #{cond.location}" )
21
+ end
22
+ end
23
+ end
24
+
25
+ Cond = Struct.new( :blk, :location )
26
+
27
+ def minidbc_call_wrap( method_name, new_method_name, check_pre, check_post, pre_check_invariants, post_check_invariants, *arg )
28
+ minidbc_check_condition(minidbc_pre(method_name), *arg) if check_pre
29
+ minidbc_check_condition(minidbc_invariants) if pre_check_invariants
30
+ #TODO need to handle exception
31
+ ret = send( new_method_name, *arg )
32
+ minidbc_check_condition(minidbc_invariants) if post_check_invariants
33
+ minidbc_check_condition(minidbc_post(method_name), ret, *arg) if check_post
34
+ ret
35
+ end
36
+
37
+ def minidbc_pre(method_name)
38
+ self.class.preconds(method_name) || []
39
+ end
40
+
41
+ def minidbc_post(method_name)
42
+ self.class.postconds(method_name) || []
43
+ end
44
+
45
+ def minidbc_invariants
46
+ # byebug
47
+ self.class.invariants
48
+ end
49
+
50
+ module SubclassMethods
51
+ def preconds(method_name)
52
+ preconds = @minidbc_pres[method_name]
53
+ super_preconds = superclass.preconds(method_name)
54
+ if preconds
55
+ if !super_preconds.empty?
56
+ puts "Warnning: You have override precondition for #{method_name}. You can only lose them"
57
+ else
58
+ preconds
59
+ end
60
+ else
61
+ super_preconds
62
+ end
63
+ end
64
+
65
+ def postconds(method_name)
66
+ ( @minidbc_posts[method_name] || [] ).concat( superclass.postconds(method_name) )
67
+ end
68
+
69
+ def invariants
70
+ @invariants.concat( superclass.invariants )
71
+ end
72
+ end
73
+
74
+ module ClassMethods
75
+ def preconds(method_name)
76
+ @minidbc_pres[method_name]
77
+ end
78
+
79
+ def postconds(method_name)
80
+ @minidbc_posts[method_name]
81
+ end
82
+
83
+ def invariants
84
+ @invariants
85
+ end
86
+
87
+ def method_added(method_name)
88
+ # byebug if method_name == :initialize
89
+
90
+ if /\w+_without_dbc/ =~ method_name.to_s
91
+ return
92
+ elsif /(pre_|post_|check_minidbc_invariant)\w+/ =~ method_name.to_s
93
+ return
94
+ else
95
+
96
+ new_method_name = :"#{method_name}_#{self.to_s.gsub(/::/,"_")}_without_dbc"
97
+
98
+ if instance_methods(false).include?(new_method_name)
99
+ return
100
+ end
101
+
102
+ if method_name == :initialize && @hook_initialize
103
+ return
104
+ end
105
+
106
+
107
+ @minidbc_pres[method_name] = @pres
108
+ @minidbc_posts[method_name] = @posts
109
+
110
+ @pres = []
111
+ @posts = []
112
+
113
+ #TODO need to support client code call alias_method
114
+ alias_method new_method_name, method_name
115
+
116
+ if method_name == :initialize
117
+ @hook_initialize = true
118
+ create_method_call_wrap( method_name, new_method_name, true, true, false, true )
119
+ elsif private_methods.include? method_name
120
+ create_method_call_wrap( method_name, new_method_name, true, true, false, false )
121
+ else
122
+ create_method_call_wrap( method_name, new_method_name, true, true, true, true )
123
+ end
124
+ end
125
+ end
126
+
127
+ def create_method_call_wrap( method_name, new_method_name, check_pre, check_post, pre_check_invariants, post_check_invariants )
128
+ define_method method_name do |*arg|
129
+ # puts self.class
130
+ # puts method_name
131
+ # puts new_method_name
132
+ # byebug
133
+ minidbc_call_wrap( method_name, new_method_name, check_pre, check_post, pre_check_invariants, post_check_invariants, *arg )
134
+ end
135
+ end
136
+
137
+ def minidbc_init
138
+ @pres = []
139
+ @posts = []
140
+ @minidbc_pres = {}
141
+ @minidbc_posts = {}
142
+ @invariants = []
143
+ @hook_initialize = false
144
+
145
+ #TODO need to check invariants on initialize
146
+ # class << self
147
+ # alias_method :minidbc_new, :new
148
+ # def new(*arg)
149
+ # ret = __new__(*arg)
150
+ # ret.minidbc_check_condition(
151
+ # end
152
+ # end
153
+ end
154
+
155
+
156
+ def inherited(subclass)
157
+ subclass.minidbc_init
158
+ if subclass.superclass == self
159
+ puts subclass
160
+ subclass.extend(SubclassMethods)
161
+ end
162
+ end
163
+
164
+ def invariant(&blk)
165
+ @invariants << Cond.new( blk, caller[0] )
166
+ end
167
+
168
+ def pre(&blk)
169
+ @pres << Cond.new( blk, caller[0] )
170
+ end
171
+
172
+ def post(&blk)
173
+ @posts << Cond.new( blk, caller[0] )
174
+ end
175
+ end
176
+ end
@@ -0,0 +1,3 @@
1
+ module Minidbc
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "minidbc/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "minidbc"
8
+ spec.version = Minidbc::VERSION
9
+ spec.authors = ["Yang Chen"]
10
+ spec.email = ["yangchen@thinkmore.info"]
11
+
12
+ spec.summary = %q{A minimal implementation of dbc for ruby}
13
+ spec.description = %q{This is a simple dbc library for ruby. It emploies alias_method to redefine method, hence it will introduce a lot of method name in your class. And it should be slow. If you care this, don't use it}
14
+ spec.homepage = "https://github.com/wao/minidbc"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against " \
22
+ "public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ f.match(%r{^(test|spec|features)/})
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.15"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "minitest", "~> 5.0"
35
+ spec.add_development_dependency "minitest-reporters"
36
+ spec.add_development_dependency "shoulda-context"
37
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: minidbc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yang Chen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-11-14 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.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
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.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest-reporters
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: shoulda-context
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: This is a simple dbc library for ruby. It emploies alias_method to redefine
84
+ method, hence it will introduce a lot of method name in your class. And it should
85
+ be slow. If you care this, don't use it
86
+ email:
87
+ - yangchen@thinkmore.info
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - lib/minidbc.rb
100
+ - lib/minidbc/version.rb
101
+ - minidbc.gemspec
102
+ homepage: https://github.com/wao/minidbc
103
+ licenses: []
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.6.8
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: A minimal implementation of dbc for ruby
125
+ test_files: []