Skip to content

Commit

Permalink
Moved things around to package into yogo-framework gem.
Browse files Browse the repository at this point in the history
  • Loading branch information
irjudson committed Oct 6, 2010
1 parent ec9506c commit c5bedc0
Show file tree
Hide file tree
Showing 14 changed files with 145 additions and 190 deletions.
16 changes: 5 additions & 11 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,27 @@ gem 'activesupport' # For Datamapper

gem 'dm-core'
gem 'dm-migrations'
# gem 'dm-serializer'
gem 'dm-validations'

gem 'dm-sqlite-adapter', :require => nil
gem 'dm-sqlite-adapter', :require => nil
gem 'dm-postgres-adapter', :require => nil

gem 'yogo-operation', :git => 'git://github.com/yogo/yogo-operation.git',
:require => nil
gem 'yogo-datamapper', :git => 'git://github.com/yogo/yogo-datamapper.git',
gem 'yogo-datamapper', :git => 'git://github.com/yogo/yogo-datamapper.git',
:require => nil


gem 'json'

group :development, :test do
gem "rake"
gem "jeweler"

gem "yard"

gem 'sinatra-reloader', :require => 'sinatra/reloader'

gem 'sinatra-reloader', :require => 'sinatra/reloader'

platforms(:mri_19) do
gem 'ruby-debug19', :require => 'ruby-debug'
gem 'rack-debug19', :require => 'rack-debug'
end

platforms(:mri_18) do
gem "ruby-debug"
gem "rack-debug"
Expand Down
36 changes: 23 additions & 13 deletions config.ru
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
# require 'rubygems'
# require 'bundler/setup'
#
# Bundler.require(:default, ENV['RACK_ENV'])
#
# $:.unshift File.dirname(__FILE__)
#
# require 'lib/yogo/model_lookup'
#
# require 'app/yogo/schema_app'
# encoding: utf-8
# Standard requirements for Bundler management
require 'rubygems'
require 'bundler/setup'

require './config/requires'
# Load the bundler gemset
Bundler.require(:default, ENV['RACK_ENV'] || :development )

# Mess with load paths
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))

# Configure DataMapper
require 'config/datamapper'

# Load Application pieces
require 'yogo/rack/model_lookup'
require 'yogo/schema_app'
require 'yogo/data_app'

# Load the Application Version
APP_VERSION = IO.readlines(File.join(File.dirname(__FILE__), 'VERSION'))[0]

# Make the model lookup map
use Yogo::Rack::ModelLookup, :paths => ['schema', 'data']

use Yogo::SchemaApp
run Sinatra::Base
# Run the app
run Rack::Cascade.new([Yogo::SchemaApp, Yogo::DataApp])
4 changes: 2 additions & 2 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ development:
adapter: postgres
database: yogo_development
host: localhost

test:
adapter: postgres
database: yogo_test
Expand All @@ -13,7 +13,7 @@ test:
# development:
# adapter: sqlite3
# database: db/devlopment.sqlite3
#
#
# test:
# adapter: sqlite3
# database: db/test.sqlite3
15 changes: 0 additions & 15 deletions config/requires.rb

This file was deleted.

26 changes: 11 additions & 15 deletions lib/yogo/custom_ops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,26 @@
::DataMapper::Property.accept_options(:label)
end





module Operations
Yogo::DataMapper::Model::Operations['add/yogo_methods'] = Yogo::Op.on(::DataMapper::Model) do |model|

# model.define some properties

model.send(:include, Yogo::Data::DefaultMethods)

model

end
Yogo::DataMapper::Model::Operations['add/yogo_id_property'] =

Yogo::DataMapper::Model::Operations['add/yogo_id_property'] =
Yogo::DataMapper::Model::Operations['add/property'].partial(X, :yogo_id, 'Serial', {})
Yogo::DataMapper::Model::Operations['add/created_at_property'] =
Yogo::DataMapper::Model::Operations['add/created_at_property'] =
Yogo::DataMapper::Model::Operations['add/property'].partial(X, :created_at, 'DateTime', {})
Yogo::DataMapper::Model::Operations['add/updated_at_property'] =
Yogo::DataMapper::Model::Operations['add/updated_at_property'] =
Yogo::DataMapper::Model::Operations['add/property'].partial(X, :updated_at, 'DateTime', {})
Yogo::DataMapper::Model::Operations['add/default_properties'] =
Yogo::DataMapper::Model::Operations['add/yogo_id_property'] *
Yogo::DataMapper::Model::Operations['add/created_at_property'] *

Yogo::DataMapper::Model::Operations['add/default_properties'] =
Yogo::DataMapper::Model::Operations['add/yogo_id_property'] *
Yogo::DataMapper::Model::Operations['add/created_at_property'] *
Yogo::DataMapper::Model::Operations['add/updated_at_property']
end
31 changes: 9 additions & 22 deletions app/yogo/data_app.rb → lib/yogo/data_app.rb
Original file line number Diff line number Diff line change
@@ -1,63 +1,50 @@
require 'sinatra'
# require 'json'

module Yogo
class DataApp < ::Sinatra::Base

# configure(:development) do
# register ::Sinatra::Reloader
# also_reload "app/models/*.rb"
# # dont_reload "lib/**/*.rb"
# end

before do
content_type :json
end

# I don't think this should return anything.
# get '/data/?' do
# { :content => Schema.all }.to_json
# end

get '/data/:model_id/?' do
{ :content => env['yogo.resource'].all }.to_json
end

get '/data/:model_id/:id' do
resource = env['yogo.resource']
item = resource.get(params[:id])

{ :content => item }.to_json
end

post '/data/:model_id' do
resource = env['yogo.resource']
opts = resource.parse_json(request.body.read) rescue nil

halt(401, 'Invalid Format') if opts.nil?

item = resource.new(opts)

halt(500, 'Could not save item') unless item.save

response['Location'] = item.to_url
response.status = 201
end

put '/schema/:model_id/:id' do
resource = env['yogo.resource']
item = resource.get(params[:id])
opts = resource.parse_json(request.body.read) rescue nil
halt(401, 'Invalid Format') if opts.nil?

halt(500, 'Could not update schema') unless item.update(opts)

{ :content => item }.to_json
end

delete '/schema/:model_id/:id' do
env['yogo.resource'].get(params[:id]).destroy
end

end
end
File renamed without changes.
10 changes: 5 additions & 5 deletions lib/yogo/rack/model_lookup.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# encoding: utf-8
require 'dm-core'
require 'model/schema'
require 'yogo/model/schema'

module Yogo
module Rack
Expand All @@ -17,20 +17,20 @@ def call(env)
env['yogo.schema'], env['yogo.resource'] = get_model(model_id)
return [ 404, {'Content-Type' => 'text/plain'}, ["#{model_id} not found"] ] if env['yogo.schema'].nil?
end

@app.call(env)
end

private

def get_model(model_id)
config = Schema.first(:name => model_id)

unless config.nil?
return config, config.data_model
end
end

end # ModelLookup
end # Rack
end # Yogo
22 changes: 7 additions & 15 deletions app/yogo/schema_app.rb → lib/yogo/schema_app.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
require 'sinatra'
# require 'json'

module Yogo
class SchemaApp < ::Sinatra::Base

# configure(:development) do
# register ::Sinatra::Reloader
# also_reload "app/models/*.rb"
# # dont_reload "lib/**/*.rb"
# end

before do
content_type :json
end

get '/schema/?' do
{ :content => Schema.all }.to_json
end

get '/schema/:model_id/?' do
{ :content => env['yogo.schema'] }.to_json
end

post '/schema' do
opts = Schema.parse_json(request.body.read) rescue nil
halt(401, 'Invalid Format') if opts.nil?
Expand All @@ -32,20 +24,20 @@ class SchemaApp < ::Sinatra::Base
response['Location'] = "/schema/#{schema.name}"
response.status = 201
end

put '/schema/:model_id' do
schema = env['yogo.schema']
opts = Schema.parse_json(request.body.read) rescue nil
halt(401, 'Invalid Format') if opts.nil?

halt(500, 'Could not update schema') unless schema.update(opts)

{ :content => schema }.to_json
end

delete '/schema/:model_id' do
env['yogo.schema'].destroy
end

end
end
Loading

0 comments on commit c5bedc0

Please sign in to comment.