yaml2sql 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b50a6ca2723a3898ce8da5c554b57acc9f766e49
4
+ data.tar.gz: 3c3def3186162ea23bd556d55b7816413ad2d701
5
+ SHA512:
6
+ metadata.gz: c93c92edd459d22764b8db654c0081cde7699893b2568c75d7028b3a3881d03ba49664ef5b5597c611a19d037e60d45c46deb4b61a4886309fc7babeabd54860
7
+ data.tar.gz: 3fa3456cfedcf1c1a417bd89d4ce22c20392dc8da6265f1e7ffb40ba43055747e3568049d19f279a81f1d6610a11ea1096c90c5680493492f8f69d6efee69a99
@@ -0,0 +1,50 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ # Gemfile.lock
46
+ # .ruby-version
47
+ # .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.4
5
+ before_install: gem install bundler -v 1.15.4
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 yaml2sql.gemspec
6
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Nasr Maswood
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 nmaswood
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,39 @@
1
+ # Yaml2sql
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/yaml2sql`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'yaml2sql'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install yaml2sql
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/yaml2sql.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "yaml2sql"
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,101 @@
1
+ require 'yaml'
2
+
3
+ module Create
4
+ def create_single_insert(insert_obj)
5
+ name, type = insert_obj['name'], insert_obj['type']
6
+ "#{name} #{type} #{'unique' if insert_obj.key?('unique')}"
7
+ end
8
+
9
+ def create_single_foreign(foreign_obj)
10
+ name, table, key = ['name', 'table', 'key'].map{ |x| foreign_obj[x] }
11
+ "foreign key (#{name}) references #{table}(#{key})"
12
+ end
13
+
14
+ def create_single_index(index_obj)
15
+ name, table, key = ['name', 'table', 'key'].map{ |x| index_obj[x] }
16
+ "create index if not exists #{name} on #{table + '(' + key + ');'}"
17
+ end
18
+
19
+ def create_insert_str(columns_obj, foreign_obj, indexes_obj)
20
+ insert_statements = ['id integer primary key autoincrement']
21
+ insert_statements += columns_obj.map{ |x| create_single_insert(x) }
22
+ insert_statements += foreign_obj.map{ |x| create_single_foreign(x) } if foreign_obj
23
+
24
+ insert_statements = "(#{insert_statements.join(',')});"
25
+
26
+ insert_statements += indexes_obj.map { |x| create_single_index(x) }.join(" ") if indexes_obj
27
+
28
+ insert_statements
29
+ end
30
+
31
+ def build_create_query(obj)
32
+ name = obj['name']
33
+ create = "create table if not exists #{name}"
34
+ insert = create_insert_str(obj['columns'], obj['foreign'], obj['indexes'])
35
+ create + insert
36
+ end
37
+
38
+ def create_tables(obj)
39
+ obj["tables"].map{ |x| build_create_query(x) }
40
+ end
41
+ end
42
+
43
+ module Insert
44
+
45
+ def create_insert_lambda(obj)
46
+ table = obj['name']
47
+ names = obj['columns'].map{ |x| x["name"] }
48
+
49
+ lambda do |*args|
50
+ "insert into #{table} VALUES (#{args.join(",")});"
51
+ end
52
+ end
53
+
54
+ def create_select_id_lambda(obj)
55
+ table = obj['name']
56
+ select_on = obj['select_on']
57
+ lambda do |key|
58
+ "select id from #{table} where #{select_on} = #{key};"
59
+ end
60
+ end
61
+
62
+ def create_lambdas(obj)
63
+ obj["tables"].map do |x|
64
+ {
65
+ name: x["name"],
66
+ insert: create_insert_lambda(x),
67
+ select: create_select_id_lambda(x)
68
+ }
69
+ end
70
+ end
71
+ end
72
+
73
+ class Query
74
+ extend Create
75
+ extend Insert
76
+
77
+ def initialize(yaml_path)
78
+ yaml = YAML.load_file('schema.yaml')
79
+
80
+ self.class.create_tables(yaml)
81
+ create_methods(yaml)
82
+ end
83
+
84
+ def create_methods(yaml)
85
+ lambdas = self.class.create_lambdas(yaml)
86
+
87
+ lambdas.each do |x|
88
+ insert_name = ("insert_" + x[:name]).to_sym
89
+
90
+ self.class.send(:define_method, insert_name) do |*args|
91
+ x[:insert].call(*args)
92
+ end
93
+
94
+ select_name = ("select_" + x[:name]).to_sym
95
+
96
+ self.class.send(:define_method, select_name) do |val|
97
+ x[:select].call(val)
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,3 @@
1
+ module Yaml2sql
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,50 @@
1
+ name: poetry_project
2
+ tables:
3
+ - name: authors
4
+
5
+ columns:
6
+ - name: name
7
+ type: text
8
+ - name: url
9
+ type: text
10
+ unique: true
11
+
12
+ indexes:
13
+ - name: author_url_index
14
+ table: authors
15
+ key: url
16
+ - name: author_name_index
17
+ table: authors
18
+ key: name
19
+
20
+ select_from:
21
+ url
22
+
23
+ - name: poems
24
+
25
+ columns:
26
+ - name: name
27
+ type: text
28
+ - name: url
29
+ type: text
30
+ unique: true
31
+ - name: byline
32
+ type: text
33
+ - name: author_id
34
+ type: integer
35
+
36
+ indexes:
37
+ - name: poem_name_index
38
+ table: poems
39
+ key: name
40
+ - name: poem_url_index
41
+ table: poems
42
+ key: url
43
+
44
+ foreign:
45
+ - name: author_id
46
+ table: authors
47
+ key: id
48
+
49
+ select_from:
50
+ url
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "yaml2sql/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "yaml2sql"
8
+ spec.version = Yaml2sql::VERSION
9
+ spec.authors = ["nmaswood"]
10
+ spec.email = ["nasrmaswood@gmail"]
11
+
12
+ spec.summary = "A simple module that lets you write sql with yaml"
13
+ spec.homepage = "https://github.com/nmaswood/yaml2sql"
14
+ spec.license = "MIT"
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 "rspec", "~> 3.0"
35
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yaml2sql
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - nmaswood
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-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.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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - nasrmaswood@gmail
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/yaml2sql.rb
73
+ - lib/yaml2sql/version.rb
74
+ - schema.yaml
75
+ - yaml2sql.gemspec
76
+ homepage: https://github.com/nmaswood/yaml2sql
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.6.13
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: A simple module that lets you write sql with yaml
100
+ test_files: []