yamlade 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ pkg
2
+
data/MIT_LICENCE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2009 - 2010 Mickael Riga
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,119 @@
1
+ YAMLADE - Use YAML files to cook configuration Marmelade for a website
2
+ ----------------------------------------------------------------------
3
+
4
+ WHAT DOES IT DO ?
5
+ -----------------
6
+
7
+ Yamlade is made for config files of a web application.
8
+ Things that should be unique but still editable in a CMS.
9
+ Basically, Yamlade helps you with the CMS part of it, building the forms for you
10
+ and dealing with the updates.
11
+
12
+ HOW TO INSTALL IT ?
13
+ -------------------
14
+
15
+ # sudo gem install yamlade
16
+
17
+ And then require it:
18
+
19
+ require 'yamlade'
20
+
21
+ HOW TO USE IT ?
22
+ ---------------
23
+
24
+ Just create your YAML file with keys followed by 2 underscores and the type of field.
25
+ ex: options.yml
26
+
27
+ ___
28
+ publish__boolean:
29
+ full_title__string:
30
+ content__text:
31
+ banner__file: desired_file_name.jpeg
32
+ logo__data:
33
+
34
+ And then you're ready to go.
35
+
36
+ require 'yamlade'
37
+ @conf = Yamlade.new('options.yml')
38
+
39
+ If you want to get the html form for your config file:
40
+
41
+ @conf.to_form(action_url)
42
+
43
+ Or just this if you only need the fields to wrap them in your own form:
44
+
45
+ @conf.to_fields
46
+
47
+ And when the POST request arrives, your object is in the 'yaml' key. So use it for updating your file:
48
+
49
+ @conf.update(request['yaml'])
50
+
51
+ That's it, only three methods for the moment.
52
+ See examples if you're not sure.
53
+ I'll try to put examples for as many frameworks as i can.
54
+
55
+ HOW ARE MY FILES HANDLED WITH 'FILE' TYPE ?
56
+ -------------------------------------------
57
+
58
+ If you use the file type, the uploaded file is loaded on the file system.
59
+ So in that case, it might be important to specify the second argument of the constructor : public root
60
+ If you don't, Yamlade consider the folder called 'public' next to your YAML file as the public root.
61
+
62
+ conf = Yamlade.new('conf.yml', ::File.expand_path('../public/', 'conf.yml'))
63
+
64
+ The other thing to know, is that you have to give a name when building Yaml file the first time.
65
+ Basicaly you force the name of the file instead of keeping the original file name.
66
+ It helps Yamlade to overwrite the previously recorded file.
67
+ That way also allows you to give paths deeper than public root if necessary:
68
+
69
+ header__file: layout/header.jpeg
70
+
71
+
72
+ HOW ARE MY FILES HANDLED WITH 'DATA' TYPE ?
73
+ -------------------------------------------
74
+
75
+ Well, in fact the file will be included in your YAML if you use the data type.
76
+ The main reason for that is that it's cool to have everything in one file.
77
+ The way it's recorded is with the handy data URI scheme : http://en.wikipedia.org/wiki/Data_URI_scheme
78
+ But you don't have to think about that because Yamlade works for you.
79
+ Just use the value like if it was a URL:
80
+
81
+ <% @options = YAML.load_file('options.yml') %>
82
+ <img src="<%= @options.logo %>" />
83
+
84
+ !!! You don't use Yamlade class here. Reading is made through a classic YAML loading and reading.
85
+
86
+ !!! At this day, data scheme doesn't work on IE<=7 and is limited to 32kb on IE8
87
+
88
+ WHAT IF I WANT TO USE 'DATA' TYPE ANYWAY ?
89
+ ------------------------------------------
90
+
91
+ The best thing to do might be to do some MHTML trick:
92
+ http://www.phpied.com/data-uris-mhtml-ie7-win7-vista-blues/
93
+
94
+ An other way is possible if you really want to keep everything in one file.
95
+ Dynamically serve those images.
96
+ This is not recommended because it produces useless computing (maybe acceptable for a couple of pics)
97
+
98
+ Basicaly, you need to have an action that reads the information, set the right mime-type and decoded content.
99
+ Here is a Ramaze example:
100
+
101
+ def data(file, var)
102
+ @conf = YAML.load_file(::File.expand_path("../../private/#{file}.yml", __FILE__))
103
+ mime, data = @conf[var].split(/,/)
104
+ response['Content-Type'] = mime[/[^:]*\/[^;]*/] # remove what is around the mime-type
105
+ response.status = 200
106
+ response.write data.unpack("m")[0] # decode base64
107
+ throw(:respond, response)
108
+ end
109
+
110
+ And then you can have your pictures with urls like : /data/conf/logo__data
111
+
112
+ I removed the preview when editing this kind of file in order to allow that trick.
113
+ Maybe I'll try to find a way to deal both situations...
114
+
115
+ (C)
116
+ ---
117
+
118
+ Copyright (c) 2009 - 2010 Mickael Riga - See MIT_LICENCE for more details
119
+
@@ -0,0 +1,6 @@
1
+ ---
2
+ banner__file: my_picture.jpeg
3
+ publish__boolean: "false"
4
+ logo__data:
5
+ box_content__text:
6
+ title__string:
File without changes
data/examples/rack.ru ADDED
@@ -0,0 +1,33 @@
1
+ # Start me with : rackup rack.ru
2
+ # Default port is 9292
3
+
4
+ require File::expand_path('../yamlade', File.dirname(__FILE__)) # When installed, just use : require 'yamlade'
5
+
6
+ # PUBLIC ROOT
7
+
8
+ map '/' do
9
+ run Rack::File.new(File.expand_path('./public', File.dirname(__FILE__)))
10
+ end
11
+
12
+ # EDIT
13
+
14
+ map '/edit' do
15
+ run lambda { |env|
16
+ request = Rack::Request.new(env)
17
+
18
+ @conf = Yamlade.new("options.yml")
19
+ if request.post?
20
+ @conf.update(request['yaml'])
21
+ end
22
+
23
+ [ 200, {'Content-Type' => 'text/html'}, @conf.to_form(request.path_info) + "<br /><br /><a href='/inspect'>&gt;&gt; INSPECT</a>" ]
24
+ }
25
+ end
26
+
27
+ # INSPECT
28
+
29
+ map '/inspect' do
30
+ run lambda { |env|
31
+ [ 200, {'Content-Type' => 'text/html'}, YAML.load_file("options.yml").inspect ]
32
+ }
33
+ end
@@ -0,0 +1,27 @@
1
+ # Start me with : rackup ramaze.ru
2
+ # Default port is 9292
3
+
4
+ require 'rubygems'
5
+ require 'ramaze'
6
+ require File::expand_path('../yamlade', File.dirname(__FILE__)) # When installed, just use : require 'yamlade'
7
+
8
+ class MainController < Ramaze::Controller
9
+
10
+ # /
11
+ def index
12
+ @conf = Yamlade.new("options.yml")
13
+ if request.post?
14
+ @conf.update(request['yaml'])
15
+ end
16
+ @conf.to_form(request.path_info) + "<br /><br /><a href='/inspect'>&gt;&gt; INSPECT</a>"
17
+ end
18
+
19
+ # /inspect
20
+ def inspect
21
+ YAML.load_file("options.yml").inspect
22
+ end
23
+
24
+ end
25
+
26
+ Ramaze.start(:root => __DIR__, :started => true, :mode => :dev)
27
+ run Ramaze
@@ -0,0 +1,28 @@
1
+ # Start me with : rackup rack.ru
2
+ # Default port is 9292
3
+
4
+ require 'sinatra/base'
5
+ require File::expand_path('../yamlade', File.dirname(__FILE__)) # When installed, just use : require 'yamlade'
6
+
7
+ class SinatrApp < Sinatra::Base
8
+ set :app_file, __FILE__
9
+ set :root, File.dirname(__FILE__)
10
+
11
+ get '/?' do
12
+ @conf = Yamlade.new("options.yml")
13
+ @conf.to_form(request.path_info) + "<br /><br /><a href='/inspect'>&gt;&gt; INSPECT</a>"
14
+ end
15
+
16
+ post '/?' do
17
+ @conf = Yamlade.new("options.yml")
18
+ @conf.update(request['yaml'])
19
+ @conf.to_form(request.path_info) + "<br /><br /><a href='/inspect'>&gt;&gt; INSPECT</a>"
20
+ end
21
+
22
+ get '/inspect' do
23
+ YAML.load_file("options.yml").inspect
24
+ end
25
+
26
+ end
27
+
28
+ run SinatrApp
data/yamlade.gemspec ADDED
@@ -0,0 +1,12 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'yamlade'
3
+ s.version = "0.0.1"
4
+ s.platform = Gem::Platform::RUBY
5
+ s.summary = "Use YAML files to cook configuration Marmelade through HTML forms"
6
+ s.description = "Do a YAML file, and Yamlade take care of the HTML forms for you"
7
+ s.files = `git ls-files`.split("\n").sort
8
+ s.require_path = '.'
9
+ s.author = "Mickael Riga"
10
+ s.email = "mig@mypeplum.com"
11
+ s.homepage = "http://github.com/mig-hub/yamlade"
12
+ end
data/yamlade.rb ADDED
@@ -0,0 +1,69 @@
1
+ require 'yaml'
2
+ class Yamlade
3
+ def initialize(f, public_root=::File.expand_path('../public/', f))
4
+ @file = f
5
+ @public_root = public_root
6
+ @public_root << '/' unless public_root[/\/$/]
7
+ end
8
+ def to_fields
9
+ o = "<h3>#{::File.basename(@file, '.yml').upcase.tr('_', ' ')}</h3>"
10
+ config = YAML.load_file(@file)
11
+ config.each do |k,v|
12
+ label, type = k.split('__')
13
+ o << "<p>#{label.capitalize.tr('_', ' ')}<br />"
14
+
15
+ o << case type
16
+ when 'string'
17
+ "<input type='text' value='#{v}' name='yaml[#{k}]' />"
18
+ when 'text'
19
+ "<textarea cols='40' rows='5' name='yaml[#{k}]'>#{v}</textarea>"
20
+ when 'file'
21
+ preview = (v.nil? || v=='') ? '' : "<img src='/#{v}' width='100' /><br />"
22
+ "#{preview}<input type='file' name='yaml[#{k}]' />"
23
+ when 'data'
24
+ "<input type='file' name='yaml[#{k}]' />"
25
+ when 'boolean'
26
+ s = [' checked', nil]
27
+ s.reverse! unless v=='true'
28
+ %{
29
+ <input type='radio' name='yaml[#{k}]' value='true'#{s[0]} /> True<br />
30
+ <input type='radio' name='yaml[#{k}]' value='false'#{s[1]} /> False
31
+ }.strip
32
+ end
33
+
34
+ o << '</p>'
35
+ end
36
+ o
37
+ end
38
+
39
+ def to_form(url)
40
+ "<form action='#{url}' method='POST' enctype='multipart/form-data'>#{self.to_fields}<input type='submit' name='save' value='SAVE' /></form>"
41
+ end
42
+
43
+ def update(h)
44
+ config = YAML.load_file(@file)
45
+ # Data
46
+ datas = h.find_all {|k,v| k[/_data$/] }
47
+ datas.each do |data|
48
+ field, data_hash = data
49
+ tempfile, content_type = data_hash.values_at(:tempfile, :type)
50
+ h[field] = "data:#{content_type};base64,#{[::File.open(tempfile.path).read].pack('m').strip}"
51
+ tempfile.close(true)
52
+ end
53
+ # File
54
+ files = h.find_all {|k,v| k[/_file$/] }
55
+ files.each do |file|
56
+ field, file_hash = file
57
+ tempfile, content_type = file_hash.values_at(:tempfile, :type)
58
+ unless config[field].to_s.empty? or tempfile.nil?
59
+ FileUtils.move(tempfile.path, "#{@public_root}#{config[field]}", :force => true)
60
+ FileUtils.chmod(0777, "#{@public_root}#{config[field]}")
61
+ tempfile.close(true)
62
+ end
63
+ h.delete(field)
64
+ end
65
+ # Update
66
+ config = config.update(h)
67
+ ::File.open(@file, 'w') {|f| YAML.dump(config, f) }
68
+ end
69
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yamlade
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Mickael Riga
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-06-02 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Do a YAML file, and Yamlade take care of the HTML forms for you
23
+ email: mig@mypeplum.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - .gitignore
32
+ - MIT_LICENCE
33
+ - README.md
34
+ - examples/options.yml
35
+ - examples/public/my_picture.jpeg
36
+ - examples/rack.ru
37
+ - examples/ramaze.ru
38
+ - examples/sinatra.ru
39
+ - yamlade.gemspec
40
+ - yamlade.rb
41
+ has_rdoc: true
42
+ homepage: http://github.com/mig-hub/yamlade
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options: []
47
+
48
+ require_paths:
49
+ - .
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ hash: 3
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.3.7
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Use YAML files to cook configuration Marmelade through HTML forms
75
+ test_files: []
76
+