schema_reader 0.0.3
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 +7 -0
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +79 -0
- data/Rakefile +1 -0
- data/lib/schema_reader.rb +96 -0
- data/lib/schema_reader/version.rb +3 -0
- data/schema.rb +18 -0
- data/schema_reader.gemspec +28 -0
- data/spec/lib/schema_reader_spec.rb +79 -0
- metadata +128 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2138524a728c81f8778e9afa8d0758f558bd77da
|
4
|
+
data.tar.gz: d3fee34b1fe3a7f0bb2896cc58b19b9092358d8f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a441d6a528fd148912bb10a186d39526eab221eda00d34d8f2f9dd56d48708f5a04a989c542ae5a27d02d174492afdab6a4a81874d35a46c19ca19bbc55c5078
|
7
|
+
data.tar.gz: 5cf253b6f588644d3fcf4982627a11561a7e16dc1d7fc1129245d9af0c4556bfb0cb043afa85a46e8c276f359857a744768298ee5944d18d0154af3a7a4cddb3
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Dustin Zeisler
|
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,79 @@
|
|
1
|
+
# Schema Reader
|
2
|
+
|
3
|
+
Reads Rails Database schema.rb and creates a class from selected table with getters and setters.
|
4
|
+
|
5
|
+
This was created for unit testing Rails ActiveRecord. Instead of creating a mock that can
|
6
|
+
become out of date with real objects schema reader creates mocks form the true definition.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'schema_reader'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install schema_reader
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
# db/schema.rb
|
25
|
+
|
26
|
+
ActiveRecord::Schema.define(version: 20131226214224) do
|
27
|
+
|
28
|
+
create_table "users", force: true do |t|
|
29
|
+
t.string "name"
|
30
|
+
t.integer "age"
|
31
|
+
t.string "email"
|
32
|
+
end
|
33
|
+
|
34
|
+
create_table "comments", force: true do |t|
|
35
|
+
t.text "comment"
|
36
|
+
t.integer "user_id"
|
37
|
+
t.datetime "created_at"
|
38
|
+
t.datetime "updated_at"
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
# /spec/*
|
44
|
+
class User
|
45
|
+
include SchemaReader
|
46
|
+
attr_schema table: 'users', file: File.new('db/schema.rb', 'r')
|
47
|
+
end
|
48
|
+
|
49
|
+
user = User.new(name: 'Fred', age: 37, email: "fred@example.com")
|
50
|
+
|
51
|
+
user.name
|
52
|
+
=> "Fred"
|
53
|
+
user.name = "Jane"
|
54
|
+
user.name
|
55
|
+
=> "Jane"
|
56
|
+
|
57
|
+
user.update(age: 23)
|
58
|
+
user.age
|
59
|
+
=> 23
|
60
|
+
|
61
|
+
class Comment
|
62
|
+
include SchemaReader
|
63
|
+
attr_schema table: 'comments', file: File.new('db/schema.rb', 'r')
|
64
|
+
end
|
65
|
+
|
66
|
+
# Based off field names ending with _id it will create an association
|
67
|
+
|
68
|
+
comment = Comment.new(user: user)
|
69
|
+
comment.user.name
|
70
|
+
=> "Jane"
|
71
|
+
|
72
|
+
|
73
|
+
## Contributing
|
74
|
+
|
75
|
+
1. Fork it
|
76
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
77
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
78
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
79
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,96 @@
|
|
1
|
+
require "schema_reader/version"
|
2
|
+
|
3
|
+
module SchemaReader
|
4
|
+
def self.included(base)
|
5
|
+
base.extend(ClassMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
def initialize(options = {})
|
9
|
+
update(options)
|
10
|
+
end
|
11
|
+
|
12
|
+
def update(options = {})
|
13
|
+
options.each do |attribute, value|
|
14
|
+
send("#{attribute}=", value)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
alias_method :update_attributes, :update
|
18
|
+
|
19
|
+
module ClassMethods
|
20
|
+
|
21
|
+
def attr_schema(options = {})
|
22
|
+
table = options.fetch(:table)
|
23
|
+
file = options.fetch(:file)
|
24
|
+
self.send(:attr_accessor, *read_schema(table, file))
|
25
|
+
end
|
26
|
+
|
27
|
+
def read_schema(table_selected, path)
|
28
|
+
tables = parse_for_tables(path)
|
29
|
+
table = find_table(tables, table_selected)
|
30
|
+
field_names = get_table_field_name(table)
|
31
|
+
add_associations(field_names)
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def add_associations(field_names)
|
37
|
+
association_fields = field_names.select {|field| field =~ /_id/}
|
38
|
+
field_names += association_fields.map {|field| field.to_s.delete('_id').to_sym}
|
39
|
+
field_names
|
40
|
+
end
|
41
|
+
|
42
|
+
def parse_for_tables(path)
|
43
|
+
File.read(path).split(/^\s*create_/)[1..-1].map {|table_data| Table.parse table_data }
|
44
|
+
end
|
45
|
+
|
46
|
+
def find_table(tables, table_selected)
|
47
|
+
table = tables.select do |table|
|
48
|
+
table.name == table_selected
|
49
|
+
end
|
50
|
+
raise "Table Name not Found!" if table.first.nil?
|
51
|
+
table.first
|
52
|
+
end
|
53
|
+
|
54
|
+
def get_table_field_name(table)
|
55
|
+
table.attributes.map do |attribute|
|
56
|
+
attribute.name.to_sym
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
class Attribute
|
63
|
+
|
64
|
+
attr_reader :name, :type
|
65
|
+
|
66
|
+
def initialize(type, name)
|
67
|
+
@name, @type = name, type
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.parse(attribute)
|
71
|
+
match = attribute.match(/t\.(\w+)\s+"(\w+)"/)
|
72
|
+
if match
|
73
|
+
Attribute.new(*match.captures)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
class Table
|
80
|
+
|
81
|
+
attr_reader :attributes, :name
|
82
|
+
|
83
|
+
def initialize(name, attributes)
|
84
|
+
@name, @attributes = name, attributes
|
85
|
+
end
|
86
|
+
|
87
|
+
def self.parse(table_data)
|
88
|
+
return unless name = table_data[/table "(.+)"/]
|
89
|
+
name = $1
|
90
|
+
atts = table_data.lines.to_a.select {|line| line =~ /t\.\w+/ }.map {|att| Attribute.parse att }
|
91
|
+
Table.new(name, atts)
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
data/schema.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
ActiveRecord::Schema.define(version: 20131226214224) do
|
2
|
+
|
3
|
+
create_table "users", force: true do |t|
|
4
|
+
t.string "name"
|
5
|
+
t.integer "age"
|
6
|
+
t.string "email"
|
7
|
+
t.datetime "created_at"
|
8
|
+
t.datetime "updated_at"
|
9
|
+
end
|
10
|
+
|
11
|
+
create_table "comments", force: true do |t|
|
12
|
+
t.text "comment"
|
13
|
+
t.integer "user_id"
|
14
|
+
t.datetime "created_at"
|
15
|
+
t.datetime "updated_at"
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'schema_reader/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "schema_reader"
|
8
|
+
spec.version = SchemaReader::VERSION
|
9
|
+
spec.authors = ["Dustin Zeisler"]
|
10
|
+
spec.email = ["dustin@zive.me"]
|
11
|
+
spec.description = %q{Reads Rails Database schema and creates a class from selected table with getters and setters.}
|
12
|
+
spec.summary = %q{This was created for unit testing Rails ActiveRecord.
|
13
|
+
Instead of creating a mock that can become out of date with real objects schema reader creates mocks form the true definition.}
|
14
|
+
spec.homepage = "https://github.com/zeisler/schema_reader"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files`.split($/)
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(spec|spec|features)/})
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
23
|
+
spec.add_development_dependency "rake", "~>10.1"
|
24
|
+
spec.add_development_dependency "rspec", "~>2.14"
|
25
|
+
spec.add_development_dependency "debase", "~>0.0"
|
26
|
+
spec.add_development_dependency "ruby-debug-ide", "~>0.4"
|
27
|
+
|
28
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require_relative '../../lib/schema_reader'
|
3
|
+
|
4
|
+
describe SchemaReader do
|
5
|
+
|
6
|
+
context 'read_schema' do
|
7
|
+
|
8
|
+
before do
|
9
|
+
class SchemaHash
|
10
|
+
include SchemaReader
|
11
|
+
attr_accessor *read_schema('users', File.new('schema.rb', 'r'))
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'returns attributes from schema file' do
|
16
|
+
expect(SchemaHash.read_schema('users', File.new('schema.rb', 'r'))).to eq [:name, :age, :email, :created_at, :updated_at]
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'will raise an exception if table does not exist' do
|
20
|
+
-> {SchemaHash.read_schema('persons', File.new('schema.rb', 'r'))}.should raise_error('Table Name not Found!')
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
describe 'attr_schema' do
|
26
|
+
|
27
|
+
before do
|
28
|
+
class User
|
29
|
+
include SchemaReader
|
30
|
+
attr_schema table: 'users',
|
31
|
+
file: File.new('schema.rb', 'r')
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
let(:user) {User.new}
|
36
|
+
let(:attributes) { Hash.new(name: 'Fred', age: 37, email: "fred@example.com") }
|
37
|
+
|
38
|
+
it 'will set getters and setters for schema attributes on object' do
|
39
|
+
attributes.each_pair do |attribute, value|
|
40
|
+
user.send("#{attribute}=", value)
|
41
|
+
expect(user.send(attribute)).to eq value
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'will not respond to non-schema attributes' do
|
46
|
+
expect(user.respond_to? :birth_date).to eq(false)
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'update' do
|
50
|
+
user.update(name: "Jane")
|
51
|
+
expect(user.name).to eq('Jane')
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
describe 'associations' do
|
57
|
+
|
58
|
+
before do
|
59
|
+
class Comment
|
60
|
+
include SchemaReader
|
61
|
+
attr_schema table: 'comments',
|
62
|
+
file: File.new('schema.rb', 'r')
|
63
|
+
end
|
64
|
+
|
65
|
+
class User
|
66
|
+
include SchemaReader
|
67
|
+
attr_schema table: 'users',
|
68
|
+
file: File.new('schema.rb', 'r')
|
69
|
+
end
|
70
|
+
end
|
71
|
+
let(:comment) {Comment.new(user: User.new)}
|
72
|
+
|
73
|
+
it 'will create associations of attributes ending with _id' do
|
74
|
+
expect(comment.user.class).to eq User
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: schema_reader
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dustin Zeisler
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-01-08 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: '10.1'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.1'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.14'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.14'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: debase
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.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.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: ruby-debug-ide
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.4'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.4'
|
83
|
+
description: Reads Rails Database schema and creates a class from selected table with
|
84
|
+
getters and setters.
|
85
|
+
email:
|
86
|
+
- dustin@zive.me
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- .gitignore
|
92
|
+
- Gemfile
|
93
|
+
- LICENSE.txt
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- lib/schema_reader.rb
|
97
|
+
- lib/schema_reader/version.rb
|
98
|
+
- schema.rb
|
99
|
+
- schema_reader.gemspec
|
100
|
+
- spec/lib/schema_reader_spec.rb
|
101
|
+
homepage: https://github.com/zeisler/schema_reader
|
102
|
+
licenses:
|
103
|
+
- MIT
|
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.2.0
|
122
|
+
signing_key:
|
123
|
+
specification_version: 4
|
124
|
+
summary: This was created for unit testing Rails ActiveRecord. Instead of creating
|
125
|
+
a mock that can become out of date with real objects schema reader creates mocks
|
126
|
+
form the true definition.
|
127
|
+
test_files:
|
128
|
+
- spec/lib/schema_reader_spec.rb
|