bagatelle 0.1.0

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
+ SHA1:
3
+ metadata.gz: c7932623c64d329dc6f102a26553ed1a602e4748
4
+ data.tar.gz: 46af71c074dfd2cfb253c55ef0daf8d90dde1584
5
+ SHA512:
6
+ metadata.gz: 66d5bca56a60ef80bac323a2aa389a9f5348d83af5e881cdd578c90e00ea6d776dec1a598b3460f80150cb171662b3ee815e4e96c7e257c4b6f90c39a6ac73d6
7
+ data.tar.gz: cac68092b47727c57f084c78dd62d51e6cac9bf42d7dfa08d9adefc1c626b51c91c88255761ab33e9a31956a6ef32c70313ad5495f5ace9180ab2db886c22e87
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bagatelle.gemspec
4
+ gemspec
5
+
6
+ gem 'mysql2'
7
+ gem 'activesupport'
8
+ gem 'pry'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Alexander Clark
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.
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # Bagatelle
2
+
3
+ Bagatelle is a very simple ORM following the data mapper pattern. It makes it easy to query a relational database and map the results into an object graph of POROs.
4
+
5
+ No ActiveRecord necessary!
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'bagatelle'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install bagatelle
22
+
23
+ ## Usage
24
+
25
+ 1. Define a mapper for your parent object
26
+ 1. Instantiate a storage object
27
+ 1. Instantiate your mapper
28
+ 1. Run your query!
29
+
30
+ ```ruby
31
+ class UserMapper < Bagatelle::Mapper
32
+ children :pages, lists: :items
33
+ def find(id)
34
+ recursive_map('users', 'id', [id], associations)
35
+ end
36
+ end
37
+
38
+ storage = Bagatelle::MysqlStorage.new(:host => "localhost", :username => "user", :database => "yourdb")
39
+ um = UserMapper.new(storage)
40
+ user = um.find(1)
41
+ #=> [#<User id=1, name="Sam", pages=[#<Page id=1, user_id=1, name="Home", body="This is my home page">], lists=[#<List id=1, user_id=1, name="To Do", items=[#<Item id=1, list_id=1, name="Mow Lawn">]>]>]
42
+ ```
43
+
44
+ ## Development
45
+
46
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
47
+
48
+ 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
49
+
50
+ ## Contributing
51
+
52
+ 1. Fork it ( https://github.com/alexander-clark/bagatelle/fork )
53
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
54
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
55
+ 4. Push to the branch (`git push origin my-new-feature`)
56
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ t.pattern = "test/*_test.rb"
7
+ end
data/bagatelle.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bagatelle/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bagatelle"
8
+ spec.version = Bagatelle::VERSION
9
+ spec.authors = ["Alexander Clark"]
10
+ spec.email = ["sasha.jackal@gmail.com"]
11
+
12
+ spec.summary = %q{A simple data mappper gem}
13
+ spec.description = %q{Dead simple ORM based on the data mapper pattern}
14
+ spec.homepage = "https://github.com/alexander-clark/bagatelle"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.9"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "bagatelle"
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/lib/bagatelle.rb ADDED
@@ -0,0 +1,80 @@
1
+ require 'bagatelle/version'
2
+ require 'mysql2'
3
+ require 'active_support/inflector'
4
+
5
+ module Bagatelle
6
+ class MysqlStorage
7
+ attr_accessor :connection
8
+
9
+ def initialize(params)
10
+ @connection ||= Mysql2::Client.new(params)
11
+ end
12
+
13
+ def query(table, key, value)
14
+ connection.query(build_query(table, key, value))
15
+ end
16
+
17
+ def build_query(table, key, value)
18
+ wrapped_val = "IN(#{value.join(', ')})"
19
+ "SELECT * FROM #{table} WHERE #{key} #{wrapped_val}"
20
+ end
21
+ end
22
+
23
+ class LocalStorage
24
+ attr_accessor :data
25
+
26
+ def initialize(params)
27
+ @data = params
28
+ end
29
+
30
+ def query(table, key, value)
31
+ data[table].select { |entry| value.include?(entry[key]) }
32
+ end
33
+ end
34
+
35
+ class Mapper
36
+ attr_reader :client
37
+
38
+ def initialize(client)
39
+ @client = client
40
+ end
41
+
42
+ class << self
43
+ attr_reader :associations
44
+
45
+ def children(*children)
46
+ @associations = children
47
+ end
48
+ end
49
+
50
+ def associations
51
+ self.class.associations
52
+ end
53
+
54
+ def recursive_map(table, key, value, children=[])
55
+ ids = []
56
+ rel = []
57
+ fkey = table.singularize.foreign_key
58
+
59
+ # client.query(client.build_query(table, key, value)).each do |row|
60
+ client.query(table, key, value).each do |row|
61
+ ids << row['id']
62
+ child_hash = Hash.new
63
+
64
+ children.each do |child|
65
+ if child.respond_to?(:each_pair)
66
+ child.each_pair do |k, v|
67
+ child_hash[k] = recursive_map(k.to_s, fkey, ids, Array(v))
68
+ end
69
+ else
70
+ child_hash[child] = recursive_map(child.to_s, fkey, ids)
71
+ end
72
+ end
73
+
74
+ klass = table.classify.constantize
75
+ rel << klass.new(row.merge(child_hash))
76
+ end
77
+ rel
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,3 @@
1
+ module Bagatelle
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bagatelle
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Clark
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-29 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.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
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
+ description: Dead simple ORM based on the data mapper pattern
42
+ email:
43
+ - sasha.jackal@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".travis.yml"
50
+ - CODE_OF_CONDUCT.md
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - bagatelle.gemspec
56
+ - bin/console
57
+ - bin/setup
58
+ - lib/bagatelle.rb
59
+ - lib/bagatelle/version.rb
60
+ homepage: https://github.com/alexander-clark/bagatelle
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.4.6
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: A simple data mappper gem
84
+ test_files: []