Skip to content

Commit

Permalink
Small refactoring and add some specs
Browse files Browse the repository at this point in the history
  • Loading branch information
gazay committed Oct 27, 2011
1 parent 19da767 commit 809fcbe
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 117 deletions.
4 changes: 4 additions & 0 deletions Gemfile
Expand Up @@ -15,3 +15,7 @@ gem "jquery-rails"

# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'

group :test do
gem 'rspec'
end
12 changes: 11 additions & 1 deletion Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
spik (0.0.1)
spik (0.0.2)

GEM
remote: http://rubygems.org/
Expand Down Expand Up @@ -36,6 +36,7 @@ GEM
multi_json (~> 1.0)
arel (2.2.1)
builder (3.0.0)
diff-lcs (1.1.2)
erubis (2.7.0)
hike (1.2.1)
i18n (0.6.0)
Expand Down Expand Up @@ -77,6 +78,14 @@ GEM
rake (0.9.2.2)
rdoc (3.11)
json (~> 1.4)
rspec (2.6.0)
rspec-core (~> 2.6.0)
rspec-expectations (~> 2.6.0)
rspec-mocks (~> 2.6.0)
rspec-core (2.6.3)
rspec-expectations (2.6.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.6.0)
sprockets (2.0.3)
hike (~> 1.2)
rack (~> 1.0)
Expand All @@ -95,5 +104,6 @@ PLATFORMS
DEPENDENCIES
jquery-rails
rails (~> 3.1.1)
rspec
spik!
sqlite3
43 changes: 5 additions & 38 deletions lib/spik.rb
@@ -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
50 changes: 0 additions & 50 deletions lib/spik/tmp.rb

This file was deleted.

63 changes: 63 additions & 0 deletions lib/spik/validators.rb
@@ -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
4 changes: 2 additions & 2 deletions test/test_helper.rb → spec/spec_helper.rb
@@ -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 }
41 changes: 41 additions & 0 deletions spec/spik/spik_spec.rb
@@ -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
2 changes: 1 addition & 1 deletion spik.gemspec
Expand Up @@ -9,7 +9,7 @@ Gem::Specification.new do |s|
s.version = Spik::VERSION
s.authors = ["Alexey Gaziev"]
s.email = ["alex.gaziev@gmail.com"]
# s.homepage = "TODO"
s.homepage = "https://github.com/gazay/spik"
s.summary = "Imperative speaking with Rails"
s.description = "You can say something and controller understands you"

Expand Down
12 changes: 0 additions & 12 deletions test/integration/missing_method_test.rb

This file was deleted.

13 changes: 0 additions & 13 deletions test/spik_test.rb

This file was deleted.

0 comments on commit 809fcbe

Please sign in to comment.