wurst 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ Dir.glob('wurst/**/*') do |file|
2
+ require_relative file
3
+ end
@@ -0,0 +1,145 @@
1
+ require 'erb'
2
+
3
+ FRONTEND_MODELS = {}
4
+
5
+ class ZWRouter
6
+ attr_reader :routes
7
+
8
+ def initialize
9
+ @routes = {}
10
+ @active = false
11
+ end
12
+ def has_route? route_name
13
+ @routes.has_key? route_name
14
+ end
15
+ def switch new_val = true
16
+ @active = new_val
17
+ end
18
+ def method_missing method_name, *args, &block
19
+ if [:create, :read, :update, :destroy, :index].include? method_name
20
+ if @active
21
+ @routes[method_name].call args
22
+ else
23
+ @routes[method_name] = block
24
+ end
25
+ else
26
+ super
27
+ end
28
+ end
29
+ end
30
+
31
+ module ZWResources
32
+ # Collect resources for ember model definitions
33
+ def frontend_model model_name, model
34
+ FRONTEND_MODELS[model_name] = (
35
+ model.properties.to_a.map! do |property|
36
+ {name: property.name}
37
+ end
38
+ )
39
+ end
40
+
41
+ # Route to send model definitions to ember
42
+ def self.extended base
43
+ base.send :get, '/models.js' do
44
+ content_type :js, :charset => 'utf-8'
45
+ # @@models_js_template defined at bottom of module def
46
+ Tilt::ERBTemplate.new{@@models_js_template}.render
47
+ end
48
+
49
+ Dir.glob(File.join('..', 'assets')) do |file|
50
+ # TODO cross-platform? probably not.
51
+ base.send :get, file.split('/').last do
52
+ content_type :js, :charset => 'utf-8'
53
+ send_file file
54
+ end
55
+ end
56
+ end
57
+
58
+ def resource model_sym, &block
59
+ model_name_string = model_sym.to_s
60
+ model_name = model_name_string[0].upcase + model_name_string[1..-2]
61
+ model = Kernel.const_get(model_name)
62
+ frontend_model model_name, model # Sends model definition to ember
63
+
64
+ router = ZWRouter.new
65
+ if block_given?
66
+ block.call(router)
67
+ end
68
+ router.switch
69
+
70
+ puts "***"
71
+ puts router.routes.inspect
72
+
73
+ # Find
74
+ get "/#{model_name_string}/:id" do |id|
75
+ content_type :json, :charset => 'utf-8'
76
+ if router.has_route? :read
77
+ {post: router.read(id)}.to_json
78
+ else
79
+ {post: model.get(id)}.to_json
80
+ end
81
+ end
82
+
83
+ # Find All
84
+ get "/#{model_name_string}" do
85
+ content_type :json, :charset => 'utf-8'
86
+ if router.has_route? :index
87
+ {posts: router.index}.to_json
88
+ else
89
+ {posts: model.all}.to_json
90
+ end
91
+ end
92
+
93
+ # Update
94
+ put "/#{model_name_string}/:id" do |id|
95
+ content_type :json, :charset => 'utf-8'
96
+ if router.has_route? :update
97
+ {post: router.update(id, params)}.to_json
98
+ else
99
+ param_names = model.properties.select{ |prop| prop.class != DataMapper::Property::Serial }.map{ |prop| prop.name }
100
+ m = model.get(id)
101
+ m.update(params.select{ |k,v| param_names.include? k.to_sym })
102
+ {post: m}.to_json
103
+ end
104
+ end
105
+
106
+ # Create
107
+ post "/#{model_name_string}" do
108
+ content_type :json, :charset => 'utf-8'
109
+ if router.has_route? :create
110
+ {post: router.create(params)}.to_json
111
+ else
112
+ param_names = model.properties.select{ |prop| prop.class != DataMapper::Property::Serial }.map{ |prop| prop.name }
113
+ m = model.create(params.select{ |k,v| param_names.include? k.to_sym })
114
+ {post: m}.to_json
115
+ end
116
+ end
117
+
118
+ # Delete
119
+ delete "/#{model_name_string}/:id" do |id|
120
+ content_type :json, :charset => 'utf-8'
121
+ if router.has_route? :destroy
122
+ {post: router.destroy(params)}.to_json
123
+ else
124
+ model.get(id).destroy
125
+ {}.to_json
126
+ end
127
+ end
128
+ end
129
+
130
+ # ERB template for models.js file
131
+ @@models_js_template = <<'END'
132
+ window.App = Ember.Application.create();
133
+
134
+ var attr = DS.attr;
135
+ <% FRONTEND_MODELS.each do |model_name, model| %>
136
+ App.<%= model_name %> = DS.Model.extend({
137
+ <%= model.delete_if do |property|
138
+ property[:name] == :id
139
+ end.map do |property|
140
+ "#{property[:name]}: attr()"
141
+ end.join(",\n ") %>
142
+ });
143
+ <% end %>
144
+ END
145
+ end
@@ -0,0 +1,3 @@
1
+ module Wurst
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'wurst/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "wurst"
8
+ spec.version = Wurst::VERSION
9
+ spec.authors = ["cdn64", "jesuistouriste", "Lukas_Skywalker"]
10
+ spec.email = ["info@zeilenwerk.ch"]
11
+ spec.description = "Does things."
12
+ spec.summary = "Does things."
13
+ spec.homepage = "http://zeilenwerk.ch"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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.3'
22
+ spec.add_development_dependency 'rake', '~> 10'
23
+ spec.add_development_dependency 'tilt', '~> 1.4'
24
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wurst
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - cdn64
8
+ - jesuistouriste
9
+ - Lukas_Skywalker
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2014-12-16 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bundler
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - "~>"
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: '1.3'
29
+ - !ruby/object:Gem::Dependency
30
+ name: rake
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - "~>"
34
+ - !ruby/object:Gem::Version
35
+ version: '10'
36
+ type: :development
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: '10'
43
+ - !ruby/object:Gem::Dependency
44
+ name: tilt
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '1.4'
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - "~>"
55
+ - !ruby/object:Gem::Version
56
+ version: '1.4'
57
+ description: Does things.
58
+ email:
59
+ - info@zeilenwerk.ch
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - lib/assets/ember-data.js
70
+ - lib/assets/ember.js
71
+ - lib/assets/handlebars-v1.3.0.js
72
+ - lib/wurst.rb
73
+ - lib/wurst/resource.rb
74
+ - lib/wurst/version.rb
75
+ - wurst.gemspec
76
+ homepage: http://zeilenwerk.ch
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.4.3
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Does things.
100
+ test_files: []