piano 0.8.3 → 0.8.4

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -3,4 +3,5 @@
3
3
  Gemfile.lock
4
4
  pkg/*
5
5
  views/*
6
- test/*
6
+ test/*
7
+ config/*
@@ -32,15 +32,15 @@ Any route added to the <tt>Pianofile</tt> will be parsed before the default rout
32
32
 
33
33
  This file, for example, will bring back the email masking functionality that was deprecated in version 0.7.6
34
34
 
35
- class Piano
36
- helpers do
37
- def unicode_entities(string)
38
- encodings = ""
39
- string.codepoints do |c|
40
- encodings += "&##{c};"
41
- end
42
- encodings
35
+ Note: as version 0.8.4, direct DSL usage, Sinatra-style, is available. This documentation will be updated as soon as is stable.
36
+
37
+ helpers do
38
+ def unicode_entities(string)
39
+ encodings = ""
40
+ string.codepoints do |c|
41
+ encodings += "&##{c};"
43
42
  end
43
+ encodings
44
44
  end
45
45
  end
46
46
 
data/bin/piano CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ require "metafun/delegator"
2
3
 
3
4
  unless ARGV.empty?
4
5
 
@@ -59,6 +60,16 @@ def make_sample
59
60
  FileUtils.cp_r "#{source_dir}/sample", this_dir
60
61
  end
61
62
 
63
+ self.extend Metafun::Delegator
64
+ self.delegate Piano, :get, :patch, :put, :post, :delete,
65
+ :head, :options, :template, :layout,
66
+ :before, :after, :error, :not_found,
67
+ :configure, :set, :mime_type, :enable,
68
+ :disable, :use, :development?, :test?,
69
+ :production?, :helpers, :settings
70
+
71
+ self.helpers Sinatra::Piano
72
+
62
73
  if File.exists?(File.expand_path(Dir.pwd) + "/Pianofile")
63
74
  piano_file = File.expand_path(Dir.pwd) + "/Pianofile"
64
75
  puts "Pianofile found, loading..."
@@ -0,0 +1,28 @@
1
+ Feature: i18n
2
+ In order to have internationalization in Piano
3
+ As a Piano user
4
+ I want to get it loaded and ready
5
+
6
+ Scenario: Load the i18n
7
+ Given I have installed the i18n gem
8
+ When I require the i18n
9
+ Then I have the I18n module in scope
10
+
11
+ Scenario: Get an english translation
12
+ Given I have the file "config/locale/en.yml"
13
+ And I have the file "config/locale/es.yml"
14
+ And the file "config/locale/en.yml" includes:
15
+ """
16
+ en:
17
+ hello:
18
+ world: 'Hello World!'
19
+ """
20
+ When I load the i18ns within "config/locale"
21
+ And I set the locale as :en
22
+ And I run the translation with "hello.world"
23
+ Then I should get the translated "Hello World!"
24
+
25
+ Scenario: Provide a helper
26
+ Given everything is ok
27
+ When execute instance.t "some.text"
28
+ Then translation should match "translation missing"
@@ -0,0 +1,52 @@
1
+ Given /I have installed the (.*?) gem$/ do |gem|
2
+ Gem.available? gem
3
+ end
4
+
5
+ When /I require the (.*?)$/ do |file|
6
+ Piano.should be_a(Class)
7
+ end
8
+
9
+ Then /I have the (.*?) module in scope/ do |class_name|
10
+ klass = Module.const_get class_name
11
+ klass.should be_a(Module)
12
+ end
13
+
14
+ Given /I have the file "([^"]*)"$/ do |file_path|
15
+ @base_path = File.expand_path("../../../", __FILE__)
16
+ file_name = "#{@base_path}/#{file_path}"
17
+ File.should exist(file_name)
18
+ end
19
+
20
+ Given /the file "([^"]*)" includes:$/ do |file_path, content|
21
+ file_content = File.read "#{@base_path}/#{file_path}"
22
+ file_content.should == content
23
+ end
24
+
25
+ When /I load the i18ns within "([^"]*)"/ do |folder|
26
+ Piano.i18n_path = folder
27
+ Piano.i18n!
28
+ end
29
+
30
+ When /I set the locale as :(.*)/ do |locale_string|
31
+ I18n.locale = locale_string.to_sym
32
+ end
33
+
34
+ When /I run the translation with "([^"]*)"/ do |key|
35
+ @translation = I18n.translate key
36
+ end
37
+
38
+ Then /I should get the translated "([^"]*)"/ do |translated|
39
+ @translation.should == translated
40
+ end
41
+
42
+ Given /everything is ok/ do; end
43
+
44
+ When /execute instance.t "([^"]*)"/ do |text|
45
+ obj = Object.new
46
+ obj.extend Sinatra::Piano::Helpers
47
+ @translation = obj.t text
48
+ end
49
+
50
+ Then /translation should match "([^"]*)"/ do |text|
51
+ @translation.should match(Regexp.new(text))
52
+ end
@@ -0,0 +1,3 @@
1
+ $LOAD_PATH << File.expand_path("../../../lib", __FILE__)
2
+
3
+ require "piano"
@@ -2,6 +2,7 @@ require "sinatra/base"
2
2
  require "haml"
3
3
  require "sass"
4
4
  require "yaml"
5
+ require "i18n"
5
6
 
6
7
  begin
7
8
  require "coffee-script"
@@ -13,6 +14,107 @@ end
13
14
 
14
15
  module Sinatra
15
16
  module Piano
17
+ module Helpers
18
+ def try_haml(template)
19
+ file_name = "#{pwd}/#{template}.haml"
20
+ bad_luck file_name unless File.exists? file_name
21
+
22
+ if etags?
23
+ hash = hash_for template, :haml
24
+ hash += hash_for "data/#{template}", :yaml if File.exists? "#{pwd}/data/#{template}.yaml"
25
+ etag hash
26
+ end
27
+ haml template.to_sym
28
+ end
29
+
30
+ def sass(template)
31
+ file_name = "#{pwd}/#{template}.sass"
32
+ bad_luck file_name unless File.exists? file_name
33
+
34
+ etag hash_for(template, :sass) if etags?
35
+ Sass.compile File.read(file_name), :syntax => :sass
36
+ end
37
+
38
+ def coffee(template)
39
+ file_name = "#{pwd}/#{template}.coffee"
40
+ bad_luck file_name unless File.exists? file_name
41
+
42
+ etag hash_for(template, :coffee) if etags?
43
+ CoffeeScript.compile(File.read(file_name))
44
+ end
45
+
46
+ def data_for(template)
47
+ file_name = "#{pwd}/data/#{template}.yaml"
48
+ YAML.load_file(file_name) if File.exists?(file_name)
49
+ end
50
+
51
+ def style(path)
52
+ "<link rel='stylesheet' type='text/css' href='#{path}' />"
53
+ end
54
+
55
+ def script(path)
56
+ "<script type='text/javascript' src='#{path}'></script>"
57
+ end
58
+
59
+ def pwd
60
+ settings.views
61
+ end
62
+
63
+ def bad_luck(path)
64
+ content_type :html
65
+ if settings.environment == :production
66
+ if File.exists? "#{pwd}/404.haml"
67
+ @data = data_for "404"
68
+ halt 404, haml(:"404")
69
+ else
70
+ halt 404, "<h1>404 - Not Found</h1><p>Piano has found nothing in this address</p>"
71
+ end
72
+ else
73
+ halt 404, "<h1>You have still to put something here.</h1><p>This is <em>#{path}</em></p><blockquote>Good luck!</blockquote>"
74
+ end
75
+ end
76
+
77
+ def hash_for(name, type)
78
+ "#{name}.#{type} - " + File.mtime("#{pwd}/#{name}.#{type}").to_s
79
+ end
80
+
81
+ def extract(text, length = 80)
82
+ words = text.gsub(/<.+?>/, "").split
83
+ return text if words.length <= length
84
+ words[0..(length-1)].join(" ") + "..."
85
+ end
86
+
87
+ def link(text, length = 5)
88
+ words = text
89
+ .gsub(/<.+?>/, "")
90
+ .gsub(" ", "-")
91
+ .downcase
92
+ .gsub(/[^a-z0-9\-]/, "")
93
+ .split("-")
94
+ words[0..(length-1)].join("-")
95
+ end
96
+
97
+ def etags?
98
+ if settings.respond_to? :etags
99
+ settings.etags == :on
100
+ else
101
+ true
102
+ end
103
+ end
104
+
105
+ def t(key)
106
+ I18n.translate key
107
+ end
108
+ end
109
+ end
110
+
111
+ helpers Piano::Helpers
112
+ end
113
+
114
+
115
+
116
+ class Piano < Sinatra::Base
117
+ helpers do
16
118
  def try_haml(template)
17
119
  file_name = "#{pwd}/#{template}.haml"
18
120
  bad_luck file_name unless File.exists? file_name
@@ -99,21 +201,31 @@ module Sinatra
99
201
  true
100
202
  end
101
203
  end
204
+
205
+ def t(key)
206
+ I18n.translate key
207
+ end
102
208
  end
103
-
104
- helpers Piano
105
- end
106
-
107
209
 
108
-
109
- class Piano < Sinatra::Base
110
- helpers Sinatra::Piano
111
210
 
112
211
  set :root, File.expand_path(Dir.pwd)
113
212
  set :views, File.expand_path(Dir.pwd)
114
213
  set :etags, :on
214
+ set :i18n_path, File.expand_path(Dir.pwd) + "/config/locale"
215
+
216
+ def self.i18n!
217
+ return unless Dir.exists? self.i18n_path
218
+ dir = Dir.new self.i18n_path
219
+ i18n_files = []
220
+ dir.each do |file|
221
+ if file.end_with?(".yml") or file.end_with?(".yaml")
222
+ i18n_files << "#{dir.path}/#{file}"
223
+ end
224
+ end
225
+ I18n.load_path = i18n_files
226
+ end
115
227
 
116
228
  def self.play!
117
- self.run!
229
+ self.run!
118
230
  end
119
231
  end
@@ -1,3 +1,3 @@
1
- module Piano
2
- VERSION = "0.8.3"
3
- end
1
+ module Piano
2
+ VERSION = "0.8.4"
3
+ end
@@ -17,6 +17,8 @@ Gem::Specification.new do |s|
17
17
  s.add_dependency "haml", ">= 3.1.1"
18
18
  s.add_dependency "sass", ">= 3.1.1"
19
19
  s.add_dependency "coffee-script", ">= 2.2.0"
20
+ s.add_dependency "i18n", ">= 0.6.0"
21
+ s.add_dependency "metafun"
20
22
 
21
23
  s.files = `git ls-files`.split("\n")
22
24
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: piano
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.3
4
+ version: 0.8.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-06-03 00:00:00.000000000 -03:00
12
+ date: 2011-09-05 00:00:00.000000000 -03:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: sinatra
17
- requirement: &21731508 !ruby/object:Gem::Requirement
17
+ requirement: &22118244 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: 1.2.6
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *21731508
25
+ version_requirements: *22118244
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: haml
28
- requirement: &21731208 !ruby/object:Gem::Requirement
28
+ requirement: &22117824 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: 3.1.1
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *21731208
36
+ version_requirements: *22117824
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: sass
39
- requirement: &21730932 !ruby/object:Gem::Requirement
39
+ requirement: &22117368 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: 3.1.1
45
45
  type: :runtime
46
46
  prerelease: false
47
- version_requirements: *21730932
47
+ version_requirements: *22117368
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: coffee-script
50
- requirement: &21730656 !ruby/object:Gem::Requirement
50
+ requirement: &22116924 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ! '>='
@@ -55,7 +55,29 @@ dependencies:
55
55
  version: 2.2.0
56
56
  type: :runtime
57
57
  prerelease: false
58
- version_requirements: *21730656
58
+ version_requirements: *22116924
59
+ - !ruby/object:Gem::Dependency
60
+ name: i18n
61
+ requirement: &22116528 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: 0.6.0
67
+ type: :runtime
68
+ prerelease: false
69
+ version_requirements: *22116528
70
+ - !ruby/object:Gem::Dependency
71
+ name: metafun
72
+ requirement: &22116264 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ type: :runtime
79
+ prerelease: false
80
+ version_requirements: *22116264
59
81
  description: Out-of-the-box sinatra server for web site sketching using haml + sass
60
82
  + coffee-script
61
83
  email:
@@ -70,6 +92,9 @@ files:
70
92
  - README.rdoc
71
93
  - Rakefile
72
94
  - bin/piano
95
+ - features/piano_i18n.feature
96
+ - features/step_definitions/i18n_steps.rb
97
+ - features/support/env.rb
73
98
  - lib/piano.rb
74
99
  - lib/piano/routes.rb
75
100
  - lib/piano/version.rb