isuggest 0.0.1 → 0.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 11741a322159b22153a5469b613705cda55ad2e1
4
- data.tar.gz: 9b4fe5b9be05573cbfa00de4cb7ba6c817c7cd10
3
+ metadata.gz: 0d2dff803c09290e8eb19c9f1f1df45a685dc8eb
4
+ data.tar.gz: 9e2052704a22cb1a2e7c8f8312cf3bb946c73044
5
5
  SHA512:
6
- metadata.gz: 93c2641468d3d983566a4c66fa7b83c3934b0f7ac7b4df4da4e05cd6a7ac51ca1b3ed8c08d6306bd10f9b75778dd10ebf4ab29a47afe39e327e8da6a88910b15
7
- data.tar.gz: 2800a59d72f98a7011b0a74c9acb054f9c222f52c14a73b6bbd8de93ce9ebe20e5e9ee1b3db4b6a1bb8b73f1fe1263a4d5550b25f1c23de76bed249460c4f5bd
6
+ metadata.gz: 9bccc5b431dc968d3c0c62f81639e24621d25f5a5ba80046c4ee3c4f033ef2db4479e5ea61dbbb57d4826e4798c258188bbf12a518a1dca70c93dce9036c325a
7
+ data.tar.gz: 147cef5df56dffee4dc9028cac4a44f149335cf39983f6e27e8047712b57531c1cda6388fb237f7196b9b9e7846ed30655809e4b83b917e514fdf5b20928d376
data/.gitignore CHANGED
@@ -11,4 +11,4 @@
11
11
  *.so
12
12
  *.o
13
13
  *.a
14
- mkmf.log
14
+ mkmf.log
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # Isuggest
2
2
 
3
- TODO: Write a gem description
3
+ This gem generates a list of suggested name/ email if there already exists a value in the DB. This
4
+ could be user in signup pages when new user creates an account if the email/username is not unique we provide suggesstions
5
+
4
6
 
5
7
  ## Installation
6
8
 
@@ -20,12 +22,24 @@ Or install it yourself as:
20
22
 
21
23
  ## Usage
22
24
 
23
- TODO: Write usage instructions here
25
+ In the model just add the below line
26
+ ```ruby
27
+ suggest_me :on => ['name']
28
+ ```
29
+
30
+ Options available
31
+ ```ruby
32
+ suggest_me :on => ['name'], :seperator => '_', :total_suggestions => 5
33
+ ```
34
+
35
+
36
+ TODO:
37
+ 1. Allow users to specify multiple columns in the "on" option
24
38
 
25
39
  ## Contributing
26
40
 
27
- 1. Fork it ( https://github.com/[my-github-username]/isuggest/fork )
41
+ 1. Fork it ( https://github.com/roshandevadiga/mycodes/isuggest/fork )
28
42
  2. Create your feature branch (`git checkout -b my-new-feature`)
29
43
  3. Commit your changes (`git commit -am 'Add some feature'`)
30
44
  4. Push to the branch (`git push origin my-new-feature`)
31
- 5. Create a new Pull Request
45
+ 5. Create a new Pull Request
data/equation.rb ADDED
@@ -0,0 +1,110 @@
1
+ # Equation consists of characters '+', '-', 'x', '=' and integers.
2
+ # Characters '+' and '=' never occur at the beginning nor at the end of the equation, the sign '-' is never placed at the end of the
3
+ # equation, two identical characters can't be put together (pairs '-','+' and '=','+' as well).
4
+
5
+ # Input
6
+ # The first line of the input contains number of tests t (t≤5000). Each test consists of a single line with equation
7
+ # which doesn't contain any spaces. Length of the equation doesn't exceed 1000 characters.
8
+
9
+ # Output
10
+ # For each test print the solution of the given equation (the solution is always an integer) or NO if the equation doesn't have
11
+ # exactly one solution.
12
+
13
+ # Input:
14
+ # 2
15
+ # 24-x+3x-2x=16-x
16
+ # 16-4x+2x-12=-2x+10
17
+ # Output:
18
+ # -8
19
+ # NO
20
+
21
+ class InvalidTestSizeError < StandardError; end
22
+ class EquationInvalidError < StandardError; end
23
+ class InvalidInputSize < StandardError; end
24
+
25
+ class Equation
26
+ def initialize(input)
27
+ @equ = input
28
+ @solution = nil
29
+ end
30
+
31
+ def validate
32
+ raise InvalidInputSize if @equ.length > 1000
33
+ raise EquationInvalidError if !syntax_valid?
34
+ end
35
+
36
+ def solve
37
+ validate
38
+ equ_parts = @equ.split('=')
39
+ lhs = equ_parts.first
40
+ rhs = equ_parts.last
41
+ variable = variable_part(lhs, rhs)
42
+ constant = variable.zero? ? nil : constant_part(rhs, lhs)
43
+
44
+ @solution = constant.fdiv(variable).round(3) if !variable.zero?
45
+ @solution = 0 if !@solution.nil? && @solution.zero? #ignore sign -ve / +ve
46
+ end
47
+
48
+ def display
49
+ puts(@solution.nil? ? 'NO' : @solution)
50
+ end
51
+
52
+ private
53
+ def syntax_valid?
54
+ return (first_char_valid? && last_char_valid? && signs_valid?)
55
+ end
56
+
57
+ def first_char_valid?
58
+ !['+', '='].include?(@equ[0])
59
+ end
60
+
61
+ def last_char_valid?
62
+ !['-','+', '='].include?(@equ[@equ.length - 1])
63
+ end
64
+
65
+ def signs_valid?
66
+ @equ.scan(/(xx|\s|--|\+\+|==|\+=)/).length == 0
67
+ end
68
+
69
+ def constant_part(rhs, lhs)
70
+ regex = /(\+[0-9]+|-[0-9]+|[0-9]+)/
71
+ variable_regex = /(\+[0-9]+|-[0-9]+|[0-9]+)x/
72
+ lhs_part = lhs.scan(regex).flatten.uniq - lhs.scan(variable_regex).flatten.uniq
73
+ rhs_part = rhs.scan(regex).flatten.uniq - rhs.scan(variable_regex).flatten.uniq
74
+
75
+ return (sum(rhs_part) - sum(lhs_part))
76
+ end
77
+
78
+ def variable_part(lhs, rhs)
79
+ regex = /([0-9]*|\+[0-9]*|-[0-9]*)x/
80
+ lhs_part = lhs.scan(regex).flatten.map{|item| item.empty? ? '1' : item}
81
+ rhs_part = rhs.scan(regex).flatten.map{|item| item.empty? ? '1' : item}
82
+
83
+ return ( sum(lhs_part) - sum(rhs_part) )
84
+ end
85
+
86
+ def sum(val)
87
+ return val.collect(&:to_i).inject(&:+) || 0
88
+ end
89
+ end
90
+
91
+ begin
92
+ no_of_test = gets.chomp.to_i
93
+ raise InvalidTestSizeError if no_of_test > 5000
94
+
95
+ equations = []
96
+ no_of_test.times do
97
+ equations << Equation.new(gets.chomp)
98
+ end
99
+
100
+ for itr in 0..(no_of_test - 1) do
101
+ equations[itr].solve
102
+ equations[itr].display
103
+ end
104
+ rescue InvalidInputSize => e
105
+ puts 'Input size should be less than 5000 characters'
106
+ rescue EquationInvalidError => e
107
+ puts 'Equation is not valid, please check the defined rules'
108
+ rescue InvalidTestSizeError => e
109
+ puts 'Number of tests should be less than 1000'
110
+ end
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/isuggest/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in isuggest.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 roshandevadiga
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.
@@ -0,0 +1,31 @@
1
+ # Isuggest
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'isuggest'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install isuggest
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/isuggest/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/isuggest/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/isuggest/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'isuggest'
2
+ puts 'Requiring isuggest .................................'
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'isuggest/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "isuggest"
8
+ spec.version = Isuggest::VERSION
9
+ spec.authors = ["roshandevadiga"]
10
+ spec.email = ["roshan.devadiga@gmail.com"]
11
+ spec.summary = %q{This gem will give suggestions for the fields that should be unique.}
12
+ spec.description = %q{Include this in your model and pass the column name that should be unique. Based on the column if the column is already present the gem will provide you suggesstions}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
@@ -0,0 +1,16 @@
1
+ require "isuggest/version"
2
+ require "isuggest/finder"
3
+
4
+ module Isuggest
5
+ def suggest_me(options={})
6
+ class_attribute :isuggest_options
7
+ raise ArgumentError, "Hash expected, got #{options.class.name}" if !options.empty? && !options.is_a?(Hash)
8
+ raise ArgumentError, 'No column provides' if options[:on].blank?
9
+ raise ArgumentError, 'No column provides' if options[:on].present? && !options[:on].is_a?(Array)
10
+ self.isuggest_options = {total_suggestions: 5, seperator: '' }
11
+ self.isuggest_options.merge!(options)
12
+ include Isuggest::Finder
13
+ end
14
+
15
+ ActiveRecord::Base.send :extend, Isuggest
16
+ end
@@ -0,0 +1,46 @@
1
+ module Isuggest
2
+ module Finder
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ base.include InstanceMethods
6
+ end
7
+
8
+ end
9
+
10
+ module ClassMethods
11
+
12
+ end
13
+
14
+ module InstanceMethods
15
+ def is_unique?
16
+ column_name = isuggest_columns.first.to_sym
17
+ return !self.class.exists?(column_name => self.send(column_name))
18
+ end
19
+
20
+ def suggestions
21
+ me_suggests = []
22
+ number = 10
23
+ while me_suggests.length < self.class.isuggest_options[:total_suggestions].to_i
24
+ me_suggests = filter_suggestions(me_suggests, number)
25
+ number = number * 10
26
+ end
27
+ return me_suggests
28
+ end
29
+
30
+ def filter_suggestions(me_suggests, number)
31
+ column_name = isuggest_columns.first
32
+ base_value = self.send(column_name)
33
+ while(me_suggests.length < 6) do
34
+ me_suggests << "#{base_value}#{self.class.isuggest_options[:seperator]}#{rand(number)}"
35
+ me_suggests.uniq!
36
+ end
37
+ results = self.class.where(["#{column_name} in (#{me_suggests.map{|s| '"'+s+'"'}.join(',')})"]).select(column_name).collect(&:"#{column_name}")
38
+
39
+ return (results.length == 0) ? me_suggests : (me_suggests - results)
40
+ end
41
+
42
+ def isuggest_columns
43
+ return self.class.isuggest_options[:on]
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ module Isuggest
2
+ VERSION = "0.0.2"
3
+ end
@@ -8,7 +8,13 @@ module Isuggest
8
8
  end
9
9
 
10
10
  module ClassMethods
11
+ def isuggest_seperator
12
+ isuggest_options[:seperator]
13
+ end
11
14
 
15
+ def total_results
16
+ isuggest_options[:total_suggestions].to_i
17
+ end
12
18
  end
13
19
 
14
20
  module InstanceMethods
@@ -19,28 +25,46 @@ module Isuggest
19
25
 
20
26
  def suggestions
21
27
  me_suggests = []
22
- number = 10
23
- while me_suggests.length < self.class.isuggest_options[:total_suggestions].to_i
24
- me_suggests = filter_suggestions(me_suggests, number)
25
- number = number * 10
28
+ radix = 10
29
+ while me_suggests.length < self.class.total_results
30
+ me_suggests = filter_suggestions(me_suggests, radix)
31
+ radix = radix * 10
26
32
  end
27
33
  return me_suggests
28
34
  end
29
35
 
30
- def filter_suggestions(me_suggests, number)
36
+ def filter_suggestions(me_suggests, num)
31
37
  column_name = isuggest_columns.first
32
- base_value = self.send(column_name)
33
- while(me_suggests.length < 6) do
34
- me_suggests << "#{base_value}#{self.class.isuggest_options[:seperator]}#{rand(number)}"
35
- me_suggests.uniq!
38
+
39
+ #Considering totol_results count is relatively small < 500, doubling it this should reduce the DB hits
40
+ while(me_suggests.length < (self.class.total_results * 2)) do
41
+ me_suggests << create_suggestion(self.send(column_name), num)
42
+ me_suggests.uniq!
36
43
  end
37
- results = self.class.where(["#{column_name} in (#{me_suggests.map{|s| '"'+s+'"'}.join(',')})"]).select(column_name).collect(&:"#{column_name}")
44
+
45
+ db_set = self.class.where(["#{column_name} in (#{me_suggests.map{|item| '"'+item+'"'}.join(',')})"]).select(column_name).collect(&:"#{column_name}")
38
46
 
39
- return (results.length == 0) ? me_suggests : (me_suggests - results)
47
+ results = (db_set.length == 0) ? me_suggests : (me_suggests - db_set)
48
+ #return only the number of results set in configuration
49
+ return ((results.length > self.class.total_results) ? results[0..(self.class.total_results - 1)] : results)
40
50
  end
41
51
 
42
52
  def isuggest_columns
43
53
  return self.class.isuggest_options[:on]
44
54
  end
55
+
56
+ def is_email?
57
+ regex = /([\w]+@[\w]+.[\w]+[.\w]*)/i
58
+ self.send(isuggest_columns.first).match(regex).present?
59
+ end
60
+
61
+ def create_suggestion(base_value, num)
62
+ if is_email?
63
+ base_value = base_value.split('@')
64
+ return "#{base_value.first}#{self.class.isuggest_seperator}#{rand(num)}@#{base_value.last}"
65
+ else
66
+ return "#{base_value}#{self.class.isuggest_seperator}#{rand(num)}"
67
+ end
68
+ end
45
69
  end
46
70
  end
@@ -1,3 +1,3 @@
1
1
  module Isuggest
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: isuggest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - roshandevadiga
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-20 00:00:00.000000000 Z
11
+ date: 2014-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -51,8 +51,19 @@ files:
51
51
  - LICENSE.txt
52
52
  - README.md
53
53
  - Rakefile
54
+ - equation.rb
54
55
  - init.rb
55
56
  - isuggest.gemspec
57
+ - isuggest/.gitignore
58
+ - isuggest/Gemfile
59
+ - isuggest/LICENSE.txt
60
+ - isuggest/README.md
61
+ - isuggest/Rakefile
62
+ - isuggest/init.rb
63
+ - isuggest/isuggest.gemspec
64
+ - isuggest/lib/isuggest.rb
65
+ - isuggest/lib/isuggest/finder.rb
66
+ - isuggest/lib/isuggest/version.rb
56
67
  - lib/isuggest.rb
57
68
  - lib/isuggest/finder.rb
58
69
  - lib/isuggest/version.rb