Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Small refactoring and add some specs
- Loading branch information
Showing
10 changed files
with
127 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,17 @@ | ||
require 'spik/models' | ||
require 'spik/execution' | ||
require 'spik/validators' | ||
|
||
module Spik | ||
include Models | ||
include Execution | ||
|
||
WHICH_NAMES = %w(all first) | ||
METHOD_NAMES = %w(find delete) | ||
include Validators | ||
|
||
def method_missing(method, *args) | ||
args.flatten! if args.is_a? Array | ||
method = method.to_s | ||
if model_names.include?(method) | ||
[method, args] | ||
elsif model_names.include?(method[0..-2]) | ||
[method[0..-2], args] | ||
elsif WHICH_NAMES.include?(method) | ||
if args[0] !~ /@/ | ||
[method, args].flatten | ||
else | ||
args[0] | ||
end | ||
elsif METHOD_NAMES.include?(method) | ||
if args[0] !~ /@/ | ||
execute_method(method, args) | ||
else | ||
instance_variable_get(args[0]) | ||
end | ||
elsif !attributes.select{ |k,v| v.include? method }.empty? | ||
if args[0] == 'like' | ||
method + ' LIKE "%' + args[1] + '%"' | ||
elsif args[0].is_a? String | ||
method + ' = \'' + args[0] + '\'' | ||
else | ||
method + ' = ' + args[0].to_s | ||
end | ||
elsif !class_methods.select{ |k,v| v.include? method.to_sym }.empty? | ||
execute_class_method(method, args[0], args[1]) | ||
elsif method == 'like' | ||
[method, args] | ||
elsif %w(with which has have who from).include? method | ||
args.flatten | ||
elsif method == 'as' | ||
['as__' + args[0], args[1..-1]] | ||
else | ||
[method, args] | ||
end | ||
|
||
# TODO: catch methods, which cannot be variable name | ||
spik_method(method, args) # or raise NoMethodError.new('There is no method "' + method + '"') | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
module Spik | ||
module Validators | ||
WHICH_NAMES = %w(all first) | ||
METHOD_NAMES = %w(find delete) | ||
|
||
def spik_method(method, args) | ||
if model_names.include?(method) | ||
return_value_of(:model, method, args) | ||
elsif model_names.include?(method[0..-2]) | ||
return_value_of(:model, method[0..-2], args) | ||
elsif WHICH_NAMES.include?(method) | ||
return_value_of(:which, method, args) | ||
elsif METHOD_NAMES.include?(method) | ||
return_value_of(:method, method, args) | ||
elsif !attributes.select{ |k,v| v.include? method }.empty? | ||
return_value_of(:attribute, method, args) | ||
elsif !class_methods.select{ |k,v| v.include? method.to_sym }.empty? | ||
return_value_of(:class_method, method, args) | ||
elsif method == 'like' | ||
return_value_of(:like, method, args) | ||
elsif %w(with which has have who from).include? method | ||
return_value_of(:keyword, method, args) | ||
elsif method == 'as' | ||
return_value_of(:alias, method, args) | ||
else | ||
return_value_of(:variable, method, args) | ||
end | ||
end | ||
|
||
def return_value_of(type, method, args) | ||
case type | ||
when :model, :like, :variable | ||
[method, args] | ||
when :which | ||
if args[0] !~ /@/ | ||
[method, args].flatten | ||
else | ||
args[0] | ||
end | ||
when :sql_method | ||
if args[0] !~ /@/ | ||
execute_method(method, args) | ||
else | ||
instance_variable_get(args[0]) | ||
end | ||
when :attribute | ||
if args[0] == 'like' | ||
method + ' LIKE "%' + args[1] + '%"' | ||
elsif args[0].is_a? String | ||
method + ' = \'' + args[0] + '\'' | ||
else | ||
method + ' = ' + args[0].to_s | ||
end | ||
when :class_method | ||
execute_class_method(method, args[0], args[1]) | ||
when :keyword | ||
args.flatten | ||
when :alias | ||
['as__' + args[0], args[1..-1]] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
# Configure Rails Environment | ||
ENV["RAILS_ENV"] = "test" | ||
|
||
require File.expand_path("../dummy/config/environment.rb", __FILE__) | ||
require File.expand_path("../../test/dummy/config/environment.rb", __FILE__) | ||
require "rails/test_help" | ||
|
||
Rails.backtrace_cleaner.remove_silencers! | ||
|
||
# Load support files | ||
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f } | ||
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# spik_spec_rb | ||
require 'spik' | ||
require 'spec_helper' | ||
|
||
describe Spik do | ||
before do | ||
@controller = HomeController.new | ||
end | ||
|
||
it 'should be included in HomeController' do | ||
HomeController.included_modules.include?(Spik).should == true | ||
end | ||
|
||
it 'should get all model_names from dummy application' do | ||
@controller.model_names.should include('post', 'notice') | ||
end | ||
|
||
it 'should get all attributes from models' do | ||
@controller.attributes['post'].should == Post.attribute_names | ||
@controller.attributes['notice'].should == Notice.attribute_names | ||
end | ||
|
||
it 'should get all class methods for model' do | ||
base_class_methods = ActiveRecord::Base.public_methods + [:original_table_name, :original_locking_column] | ||
@controller.class_methods['post'].should == Post.public_methods - base_class_methods | ||
end | ||
|
||
it 'should catch all keywords' do | ||
expect { | ||
@controller.find | ||
@controller.delete | ||
@controller.all | ||
@controller.delete | ||
@controller.which | ||
@controller.who | ||
@controller.from | ||
@controller.has | ||
@controller.have | ||
}.to_not raise_error | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.