simple_mysql_api 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.
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/.rvmrc ADDED
@@ -0,0 +1,83 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
7
+ # Only full ruby name is supported here, for short names use:
8
+ # echo "rvm use 1.9.3" > .rvmrc
9
+ environment_id="ruby-1.9.3-p0@simple_mysql_api"
10
+
11
+ #
12
+ # Uncomment the following lines if you want to verify rvm version per project
13
+ #
14
+ # rvmrc_rvm_version="1.10.2" # 1.10.1 seams as a safe start
15
+ # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
16
+ # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
17
+ # return 1
18
+ # }
19
+ #
20
+
21
+ #
22
+ # Uncomment following line if you want options to be set only for given project.
23
+ #
24
+ # PROJECT_JRUBY_OPTS=( --1.9 )
25
+ #
26
+ # The variable PROJECT_JRUBY_OPTS requires the following to be run in shell:
27
+ #
28
+ # chmod +x ${rvm_path}/hooks/after_use_jruby_opts
29
+ #
30
+
31
+ #
32
+ # First we attempt to load the desired environment directly from the environment
33
+ # file. This is very fast and efficient compared to running through the entire
34
+ # CLI and selector. If you want feedback on which environment was used then
35
+ # insert the word 'use' after --create as this triggers verbose mode.
36
+ #
37
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
38
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
39
+ then
40
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
41
+
42
+ if [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]]
43
+ then
44
+ . "${rvm_path:-$HOME/.rvm}/hooks/after_use"
45
+ fi
46
+ else
47
+ # If the environment file has not yet been created, use the RVM CLI to select.
48
+ if ! rvm --create "$environment_id"
49
+ then
50
+ echo "Failed to create RVM environment '${environment_id}'."
51
+ return 1
52
+ fi
53
+ fi
54
+
55
+ #
56
+ # If you use an RVM gemset file to install a list of gems (*.gems), you can have
57
+ # it be automatically loaded. Uncomment the following and adjust the filename if
58
+ # necessary.
59
+ #
60
+ # filename=".gems"
61
+ # if [[ -s "$filename" ]]
62
+ # then
63
+ # rvm gemset import "$filename" | grep -v already | grep -v listed | grep -v complete | sed '/^$/d'
64
+ # fi
65
+
66
+ # If you use bundler, this might be useful to you:
67
+ # if [[ -s Gemfile ]] && ! command -v bundle >/dev/null
68
+ # then
69
+ # printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
70
+ # gem install bundler
71
+ # fi
72
+ # if [[ -s Gemfile ]] && command -v bundle
73
+ # then
74
+ # bundle install
75
+ # fi
76
+
77
+ if [[ $- == *i* ]] # check for interactive shells
78
+ then
79
+ echo "Using: $(tput setaf 2)$GEM_HOME$(tput sgr0)" # show the user the ruby and gemset they are using in green
80
+ else
81
+ echo "Using: $GEM_HOME" # don't use colors in interactive shells
82
+ fi
83
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in simple_mysql_api.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 jalendrabhanarkar
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,29 @@
1
+ # SimpleMysqlApi
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'simple_mysql_api'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install simple_mysql_api
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,95 @@
1
+ module SimpleMysqlApi
2
+ module SimpleMysqlApiMethods
3
+ module ClassMethods
4
+ def all_belongs_to_tables
5
+ self.reflect_on_all_associations(:belongs_to).inject({}) do |r, e|
6
+ r[e.class_name] = {:t_name=> e.table_name, :f_key=> e.foreign_key }
7
+ r
8
+ end
9
+ end
10
+
11
+ def attributes
12
+ self.columns.inject({}) do |r,e|
13
+ r[e.name.to_sym]= e.type.to_s
14
+ r
15
+ end
16
+ end
17
+
18
+ def pri_key
19
+ self.primary_key
20
+ end
21
+
22
+ def searchable_attributes
23
+ self.attributes.delete_if{|k,v| (self.primary_key==k.to_s || self.foreign_keys.include?(k.to_s)) }
24
+ end
25
+
26
+ def search_conditions(attributes,params,act_relation,t_type)
27
+ attributes.each do |attr, value|
28
+ case(value)
29
+ when "string","text"
30
+ act_relation = text_search(attr,params,act_relation,t_type) if params[attr]
31
+ when "integer","float"
32
+ act_relation = range_search(attr,params,act_relation,t_type) if params[attr]
33
+ when "boolean"
34
+ act_relation = boolean_search(attr,params,act_relation,t_type) if params[attr]
35
+ end
36
+ end
37
+ act_relation
38
+ end
39
+
40
+ def text_search(attr,params,act_relation,t_type)
41
+ obj = "%#{params[attr]}%"
42
+ act_relation.where(["LOWER(#{t_type.constantize.table_name}.#{attr}) like LOWER(?)",obj])
43
+ end
44
+
45
+ def range_search(attr,params,act_relation,t_type)
46
+ attr_opp = /[-,<,>]/.match(params[attr]).to_s
47
+ table_name = "#{t_type.constantize.table_name}"
48
+ case(attr_opp)
49
+ when "-"
50
+ attr_val = params[attr].split("-").inject([]){|r,e| r << e.to_f}
51
+ act_relation = act_relation.where(["CAST(#{table_name}.#{attr} AS DECIMAL) >= ? and CAST(#{table_name}.#{attr} AS DECIMAL) <= ?",attr_val[0],attr_val[1]]) if attr_val
52
+ when "<",">"
53
+ attr_val = params[attr].split(attr_opp)[1].to_f
54
+ act_relation = act_relation.where([" CAST(#{table_name}.#{attr} AS DECIMAL) #{attr_opp} ?",attr_val]) if attr_val
55
+ else
56
+ attr_val = params[attr].to_f
57
+ act_relation = act_relation.where(["CAST(#{table_name}.#{attr} AS DECIMAL) = ?",attr_val]) if attr_val
58
+ end if attr_opp
59
+ act_relation
60
+ end
61
+
62
+ def equal_search(attr,params,act_relation,t_type)
63
+ act_relation = act_relation.where(["#{t_type.constantize.table_name}.#{attr} = ?",params[attr]])
64
+ end
65
+
66
+ def boolean_search(attr,params,act_relation,t_type)
67
+ val = params[attr].match(/(true|t|yes|y|1)$/i) != nil ? 1 : 0
68
+ act_relation = act_relation.where(["CAST(#{t_type.constantize.table_name}.#{attr} AS CHAR) = ?",val.to_s])
69
+ end
70
+
71
+ def foreign_keys
72
+ self.reflect_on_all_associations.inject([]) do |r, e|
73
+ r << e.foreign_key
74
+ r
75
+ end
76
+ end
77
+
78
+ def search(params,search_params=nil)
79
+ act_relation = self
80
+ attributes = (search_params||self.attributes).delete_if{|k,v| ((self.foreign_keys.include? k.to_s)||self.primary_key==k.to_s) }
81
+ self.all_belongs_to_tables.each do |c_name,values|
82
+ act_relation = act_relation.joins("LEFT JOIN #{values[:t_name]} #{values[:t_name]} ON #{self.table_name}.#{values[:f_key]}=#{values[:t_name]}.#{c_name.constantize.pri_key}")
83
+ new_attributes = c_name.constantize.searchable_attributes
84
+ act_relation = search_conditions(new_attributes,params,act_relation,c_name)
85
+ end
86
+ act_relation = search_conditions(attributes,params,act_relation,self.to_s)
87
+ act_relation
88
+ end
89
+ end
90
+ def self.included(base)
91
+ base.extend ClassMethods
92
+ end
93
+ end
94
+ ActiveRecord::Base.send(:include, SimpleMysqlApiMethods)
95
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleMysqlApi
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +1,7 @@
1
+ # require File.join(File.expand_path(File.dirname(__FILE__)), 'simple_mysql_api/version')
2
+ # require File.join(File.expand_path(File.dirname(__FILE__)), 'simple_mysql_api/class_methods')
3
+ require "simple_mysql_api/version"
4
+ require "simple_mysql_api/class_methods"
5
+ module SimpleMysqlApi
6
+ # Your code goes here...
7
+ end
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'simple_mysql_api/version'
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "simple_mysql_api"
7
+ gem.version = SimpleMysqlApi::VERSION
8
+ gem.authors = ["jalendrabhanarkar"]
9
+ gem.email = ["jalendra@sapnasolutions.com"]
10
+ gem.description = "Used for basic api operations,like searching etc."
11
+ gem.summary = "First gem to check the functionality."
12
+ gem.homepage = ""
13
+
14
+ gem.files = `git ls-files`.split($/)
15
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
+ gem.require_paths = ["lib"]
18
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_mysql_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - jalendrabhanarkar
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-04 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Used for basic api operations,like searching etc.
15
+ email:
16
+ - jalendra@sapnasolutions.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - .rvmrc
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - lib/simple_mysql_api.rb
28
+ - lib/simple_mysql_api/class_methods.rb
29
+ - lib/simple_mysql_api/version.rb
30
+ - simple_mysql_api.gemspec
31
+ homepage: ''
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 1.8.15
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: First gem to check the functionality.
55
+ test_files: []