snippr 0.1.7 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG CHANGED
@@ -1,2 +1,16 @@
1
+ == 0.2.0 (2010-05-13)
2
+ * Added support for I18n snippr files. Simply enable I18n:
3
+
4
+ Snippr.i18n = true
5
+
6
+ And Snippr will automatically retrieve the current locale from I18n and append
7
+ it to every snippr file you're trying to load:
8
+
9
+ I18n.locale = :de
10
+ Snippr.load(:topup, :success) # Will load: "topup/success_de.snip"
11
+
12
+ * From the example above, you should also notice that the method for loading a
13
+ snippr file has changed from +new+ to +load+.
14
+
1
15
  == 0.1.0 (2010-04-30)
2
16
  * Initial version.
data/README.rdoc CHANGED
@@ -1,62 +1,67 @@
1
1
  = Snippr
2
2
  ==== File based content management
3
3
 
4
- A snippr is a piece of HTML to be included in a website. They are plain text
5
- files stored on the file system.
4
+ A snippr file is a piece of HTML or raw text to be included in a website. They are plain text
5
+ files stored on the file system. Snippr files end with ".snip" and are read from the Snippr path.
6
6
 
7
7
  == Snippr path
8
8
 
9
- You need to specify the path to the snippr directory:
9
+ You need to specify the path to the directory where your Snippr files are stored:
10
10
 
11
- Snippr.path = File.join(File.dirname(__FILE__), "..", "snippr")
11
+ Snippr.path = File.join File.dirname(__FILE__), "..", "snippr"
12
12
 
13
- When running on JRuby, you can also set the path via JVM properties. The property
14
- you need to specify is defined in +SnipprPath::JVMProperty+. This allows system
15
- administrators to change the path without having to touch your application.
13
+ When running on JRuby, you can also set the path via JVM properties. The property you need to
14
+ specify is defined in Snippr::Path::JVMProperty. This allows system administrators to change the
15
+ path without having to touch your application.
16
16
 
17
- == Instantiation
17
+ == Loading a snippr
18
18
 
19
- Instantiating a new snippr is done by passing in the path to the snippr file as
19
+ To load a snippr file, you can use the +load+ method, passing in the path to the snippr file as
20
20
  a String (including path separators):
21
21
 
22
- Snippr.new "tariff/einheit"
22
+ Snippr.load "tariff/einheit"
23
23
 
24
24
  or by using multiple Strings or Symbols:
25
25
 
26
- Snippr.new :tariff, :einheit
26
+ Snippr.load :tariff, :einheit
27
27
 
28
28
  === Dynamic values
29
29
 
30
- A snippr may contain placeholders to be replaced with dynamic values. Placeholders
31
- are wrapped in curly braces.
30
+ A snippr file may contain placeholders to be replaced with dynamic values. Placeholders are
31
+ wrapped in curly braces.
32
32
 
33
33
  <p>You're topup of {topup_amount} at {date_today} was successful.</p>
34
34
 
35
- To replace both {topup_amount} and {date_today} with a dynamic value, you just pass
36
- in a Hash of placeholders and dynamic values when instantiating a new snippr.
35
+ To replace both {topup_amount} and {date_today} with a dynamic value, you just pass in a Hash of
36
+ placeholders and dynamic values when loading a snippr file.
37
37
 
38
- Snippr.new :topup, :success, :topup_amount => number_to_currency(15), :date_today => Date.today
38
+ Snippr.load :topup, :success, :topup_amount => number_to_currency(15), :date_today => Date.today
39
39
 
40
40
  The result will obviously be something like:
41
41
 
42
42
  <p>You're topup of 15,00 &euro; at 2010-04-03 was successful.</p>
43
43
 
44
- == Links
44
+ == I18n
45
45
 
46
- ...
46
+ Snippr comes with support for I18n, but up until further notice, you have to manually enable this
47
+ behavior in your application.
47
48
 
48
- == Snippr content
49
+ Snippr.i18n = true
49
50
 
50
- Returning the snippr content is done by calling the +to_s+ method. This will replace
51
- all specified placeholders and links.
51
+ Afterwards Snippr uses the locale specified via I18n and automatically prepends the current locale
52
+ prefixed with a "_" to your snippr files.
52
53
 
53
- Snippr.new(:tariff, :einheit).to_s
54
+ I18n.locale = :de
55
+ Snippr.load :shop # tries to load "shop_de.snip" relative to your Snippr path
56
+
57
+ I18n.locale = :en
58
+ Snippr.load :shop # tries to load "shop_en.snip" relative to your Snippr path
54
59
 
55
60
  == Rails Helper
56
61
 
57
- When using the Snippr component with Rails, it automatically adds the +SnipprHelper+
58
- module to your views. You can then use the +snippr+ helper method to load snippr files.
62
+ When using the Snippr module with Rails, it automatically adds the +Snippr::Helper+ module to
63
+ your views. You can then use the +snippr+ helper method to load snippr files.
59
64
 
60
65
  %h1 Topup successful
61
66
  .topup.info
62
- = snippr :topup, :success
67
+ = snippr :topup, :success
data/Rakefile CHANGED
@@ -1,20 +1,9 @@
1
1
  require "rake"
2
2
  require "spec/rake/spectask"
3
- require "spec/rake/verify_rcov"
4
3
 
5
4
  task :default => :spec
6
5
 
7
- Spec::Rake::SpecTask.new do |t|
8
- # t.rcov = true
9
- # t.rcov_opts = %w(--exclude-only ^\/User,spec\/)
10
- end
11
-
12
- namespace :spec do
13
- RCov::VerifyTask.new(:rcov => :spec) do |t|
14
- t.threshold = 90
15
- t.index_html = "coverage/index.html"
16
- end
17
- end
6
+ Spec::Rake::SpecTask.new
18
7
 
19
8
  begin
20
9
  require "hanna/rdoctask"
@@ -27,5 +16,5 @@ begin
27
16
  t.options << "--webcvs=http://github.com/blaulabs/snippr/tree/master/"
28
17
  end
29
18
  rescue LoadError
30
- puts "'gem install hanna' for documentation"
31
- end
19
+ puts "'gem install hanna' to generate documentation"
20
+ end
@@ -9,4 +9,4 @@ class String
9
9
  str
10
10
  end
11
11
 
12
- end
12
+ end
@@ -0,0 +1,23 @@
1
+ # = Snippr::Helper
2
+ #
3
+ # This module is automatically included into +ActionView::Base+ when using the Snippr
4
+ # component with Rails. It provides a +snippr+ helper method for loading snippr files.
5
+ #
6
+ # %h1 Topup successful
7
+ # .topup.info
8
+ # = snippr :topup, :success
9
+ module Snippr
10
+ module Helper
11
+
12
+ # Returns a snippr specified via +args+.
13
+ def snippr(*args)
14
+ snippr = Snippr.load *args
15
+ snippr.respond_to?(:html_safe) ? snippr.html_safe : snippr
16
+ end
17
+
18
+ end
19
+ end
20
+
21
+ if defined? ActionView::Base
22
+ ActionView::Base.send :include, Snippr::Helper
23
+ end
@@ -0,0 +1,27 @@
1
+ require "i18n"
2
+
3
+ # = Snippr::I18n
4
+ #
5
+ # Extends the Snippr module adding support for I18n snippr files.
6
+ module Snippr
7
+ module I18n
8
+
9
+ # Sets whether to use I18n snippr files.
10
+ def i18n=(enabled)
11
+ @enabled = enabled
12
+ end
13
+
14
+ # Returns whether to use I18n snippr files.
15
+ def i18n?
16
+ !!@enabled
17
+ end
18
+
19
+ # Returns the current locale prefixed with a "_" from I18n if I18n is enabled.
20
+ # Otherwise defaults to an empty String.
21
+ def locale
22
+ return "" unless i18n?
23
+ ["_", ::I18n.locale].join
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,29 @@
1
+ # = Snippr::Path
2
+ #
3
+ # Extends the Snippr module with methods dealing the path to the snippr files.
4
+ module Snippr
5
+ module Path
6
+
7
+ # The JVM property to set the path to the snippr files.
8
+ JVMProperty = "cms.snippet.path"
9
+
10
+ # Returns the path to the snippr files (from JVM properties if available).
11
+ def path
12
+ @path ||= JavaLang::System.get_property(JVMProperty) rescue ""
13
+ end
14
+
15
+ # Sets the path to the snippr files.
16
+ def path=(path)
17
+ @path = path.to_s
18
+ end
19
+
20
+ private
21
+
22
+ if RUBY_PLATFORM =~ /java/
23
+ module JavaLang
24
+ include_package "java.lang"
25
+ end
26
+ end
27
+
28
+ end
29
+ end
data/lib/snippr/snippr.rb CHANGED
@@ -1,114 +1,61 @@
1
+ require "snippr/core_ext"
2
+ require "snippr/path"
3
+ require "snippr/i18n"
4
+
1
5
  # = Snippr
2
6
  # ==== File based content management
3
7
  #
4
- # A snippr is a piece of HTML to be included in a website. They are plain text
5
- # files stored on the file system.
6
- #
7
- # == Snippr path
8
- #
9
- # You need to specify the path to the snippr directory:
10
- #
11
- # Snippr.path = File.join(File.dirname(__FILE__), "..", "snipprs")
12
- #
13
- # When running on JRuby, you can also set the path via JVM properties. The property
14
- # you need to specify is defined in +SnipprPath::JVMProperty+. This allows system
15
- # administrators to change the path without having to touch your application.
16
- #
17
- # == Instantiation
18
- #
19
- # Instantiating a new snippr is done by passing in the path to the snippr file as
20
- # a String (including path separators):
21
- #
22
- # Snippr.new "tariff/einheit"
23
- #
24
- # or by using multiple Strings or Symbols:
25
- #
26
- # Snippr.new :tariff, :einheit
27
- #
28
- # === Dynamic values
29
- #
30
- # A snippr may contain placeholders to be replaced with dynamic values. Placeholders
31
- # are wrapped in curly braces.
32
- #
33
- # <p>You're topup of {topup_amount} at {date_today} was successful.</p>
34
- #
35
- # To replace both {topup_amount} and {date_today} with a dynamic value, you just pass
36
- # in a Hash of placeholders and dynamic values when instantiating a new snippr.
37
- #
38
- # Snippr.new :topup, :success, :topup_amount => number_to_currency(15), :date_today => Date.today
39
- #
40
- # The result will obviously be something like:
41
- #
42
- # <p>You're topup of 15,00 &euro; at 2010-04-03 was successful.</p>
43
- #
44
- # == Links
45
- #
46
- # ...
47
- #
48
- # == Snippr content
49
- #
50
- # Returning the snippr content is done by calling the +to_s+ method. This will replace
51
- # all specified placeholders and links.
52
- #
53
- # Snippr.new(:tariff, :einheit).to_s
54
- #
55
- # == Rails Helper
56
- #
57
- # When using the Snippr component with Rails, it automatically adds the +SnipprHelper+
58
- # module to your views. You can then use the +snippr+ helper method to load snippr files.
59
- #
60
- # %h1 Topup successful
61
- # .topup.info
62
- # = snippr :topup, :success
63
- class Snippr
64
- extend SnipprPath
65
-
66
- # The snippr file extension.
67
- SnipprFileExtension = ".snip"
68
-
69
- # The comments wrapping a snippr.
70
- SnipprWrapper = "<!-- starting with snippr: %s -->\n%s\n<!-- ending with snippr: %s -->"
71
-
72
- # The fallback for a missing snippr.
73
- MissingSnippr = '<samp class="missing snippr" />'
74
-
75
- # Expects the paths to a snippr. Also accepts a Hash of placeholders
76
- # to be replaced with dynamic values.
77
- def initialize(*args)
78
- @dynamic_values = args.last.kind_of?(Hash) ? args.pop : {}
79
- @snippr_name = args.map { |arg| arg.kind_of?(Symbol) ? arg.to_s.lower_camelcase : arg }.join("/")
80
- end
8
+ # A snippr file is a piece of HTML or raw text to be included in a website. They are plain text
9
+ # files stored on the file system. Snippr files end with ".snip" and are read from the Snippr path.
10
+ module Snippr
11
+ extend Snippr::Path
12
+ extend Snippr::I18n
13
+
14
+ class << self
15
+
16
+ # The snippr file extension.
17
+ FileExtension = ".snip"
18
+
19
+ # The comments wrapping a snippr.
20
+ SnipprComments = "<!-- starting snippr: %s -->\n%s\n<!-- closing snippr: %s -->"
21
+
22
+ # The fallback tag for a missing snippr.
23
+ MissingSnipprTag = '<samp class="missing snippr" />'
24
+
25
+ # Expects the name of a snippr file. Also accepts a Hash of placeholders
26
+ # to be replaced with dynamic values.
27
+ def load(*args)
28
+ @dynamics = args.last.kind_of?(Hash) ? args.pop : {}
29
+ @name = name_from args
30
+ SnipprComments % [@name, content, @name]
31
+ end
32
+
33
+ private
34
+
35
+ # Returns the name of a snippr from a given Array of +args+.
36
+ def name_from(args)
37
+ args.map { |arg| arg.kind_of?(Symbol) ? arg.to_s.lower_camelcase : arg }.join("/") + locale
38
+ end
39
+
40
+ # Returns the raw snippr content or a +MissingSnipprTag+ in case the snippr
41
+ # file does not seem to exist.
42
+ def content
43
+ return MissingSnipprTag unless File.exist? file
44
+
45
+ content = File.read(file).strip
46
+ insert_dynamics content
47
+ content
48
+ end
49
+
50
+ # Returns the complete path to a snippr file.
51
+ def file
52
+ File.join path, [@name, FileExtension].join
53
+ end
54
+
55
+ # Replaces placeholders found in a given snippr +content+ with dynamic values.
56
+ def insert_dynamics(content)
57
+ @dynamics.each { |placeholder, value| content.gsub! "{#{placeholder}}", value.to_s }
58
+ end
81
59
 
82
- # Returns the snippr content.
83
- def to_s
84
- wrap_in_comments { snippr }
85
60
  end
86
-
87
- private
88
-
89
- # Returns the raw snippr content or a +MissingSnippr+ tag in case
90
- # the snippr file does not exist.
91
- def snippr
92
- return MissingSnippr unless File.exist? snippr_file
93
-
94
- snippr = File.read(snippr_file).strip
95
- insert_dynamic_values snippr
96
- snippr
97
- end
98
-
99
- # Replaces placeholders with dynamic values.
100
- def insert_dynamic_values(snippr)
101
- @dynamic_values.each { |placeholder, value| snippr.gsub! "{#{placeholder}}", value.to_s }
102
- end
103
-
104
- # Returns the complete path to a snippr file.
105
- def snippr_file
106
- File.join self.class.path, "#{@snippr_name}#{SnipprFileExtension}"
107
- end
108
-
109
- # Wraps the content from a given +block+ in descriptive comments.
110
- def wrap_in_comments
111
- SnipprWrapper % [@snippr_name, yield, @snippr_name]
112
- end
113
-
114
- end
61
+ end
data/lib/snippr.rb CHANGED
@@ -1,4 +1,2 @@
1
- require "snippr/core_ext"
2
- require "snippr/snippr_path"
3
- require "snippr/snippr_helper"
4
- require "snippr/snippr"
1
+ require "snippr/snippr"
2
+ require "snippr/helper"
data/rails/init.rb CHANGED
@@ -1 +1 @@
1
- require "snippr"
1
+ require "snippr"
@@ -0,0 +1 @@
1
+ <p>Willkommen in unserem Shop.</p>
@@ -0,0 +1 @@
1
+ <p>Welcome to our shop.</p>
@@ -5,49 +5,68 @@ describe Snippr do
5
5
  snippr_path = File.join File.dirname(__FILE__), "..", "fixtures"
6
6
 
7
7
  if RUBY_PLATFORM =~ /java/
8
- SnipprPath::JavaLang::System.set_property SnipprPath::JVMProperty, snippr_path
8
+ Snippr::Path::JavaLang::System.set_property Snippr::Path::JVMProperty, snippr_path
9
9
  else
10
10
  Snippr.path = snippr_path
11
11
  end
12
+ Snippr.i18n = false
12
13
  end
13
14
 
14
15
  it "should return the content of a snippr" do
15
- Snippr.new(:home).to_s.should include("<p>Home</p>")
16
+ Snippr.load(:home).should include("<p>Home</p>")
16
17
  end
17
18
 
18
19
  it "should return the content a snippr from a subfolder" do
19
- Snippr.new("tariff/einheit").to_s.should include("<p>tariff: einheit</p>")
20
+ Snippr.load("tariff/einheit").should include("<p>tariff: einheit</p>")
20
21
  end
21
22
 
22
23
  it "should return the content a snippr from a subfolder specified via multiple arguments" do
23
- Snippr.new(:tariff, :einheit).to_s.should include("<p>tariff: einheit</p>")
24
+ Snippr.load(:tariff, :einheit).should include("<p>tariff: einheit</p>")
24
25
  end
25
26
 
26
27
  it "should convert snake_case Symbols to lowerCamelCase Strings" do
27
- Snippr.new(:topup, :some_error).to_s.should include("<p>Some error occurred.</p>")
28
+ Snippr.load(:topup, :some_error).should include("<p>Some error occurred.</p>")
28
29
  end
29
30
 
30
31
  it "should wrap the snippr in descriptive comments" do
31
- Snippr.new(:home).to_s.should ==
32
- "<!-- starting with snippr: home -->\n" <<
32
+ Snippr.load(:home).should ==
33
+ "<!-- starting snippr: home -->\n" <<
33
34
  "<p>Home</p>\n" <<
34
- "<!-- ending with snippr: home -->"
35
+ "<!-- closing snippr: home -->"
35
36
  end
36
37
 
37
38
  it "should replace placeholders with dynamic values" do
38
- snippr = Snippr.new :topup, :success, :topup_amount => "15,00 &euro;", :date_today => Date.today
39
- snippr.to_s.should include("<p>You're topup of 15,00 &euro; at #{Date.today} was successful.</p>")
39
+ snippr = Snippr.load :topup, :success, :topup_amount => "15,00 &euro;", :date_today => Date.today
40
+ snippr.should include("<p>You're topup of 15,00 &euro; at #{Date.today} was successful.</p>")
40
41
  end
41
42
 
42
43
  it "should return a fallback wrapped in descriptive comments for missing snipprs" do
43
- Snippr.new(:doesnotexist).to_s.should ==
44
- "<!-- starting with snippr: doesnotexist -->\n" <<
44
+ Snippr.load(:doesnotexist).should ==
45
+ "<!-- starting snippr: doesnotexist -->\n" <<
45
46
  "<samp class=\"missing snippr\" />\n" <<
46
- "<!-- ending with snippr: doesnotexist -->"
47
+ "<!-- closing snippr: doesnotexist -->"
47
48
  end
48
49
 
49
- it "should raise an ArgumentError if the +path+ does not exist" do
50
- lambda { Snippr.path = "does_not_exist" }.should raise_exception(ArgumentError)
50
+ describe "I18n" do
51
+ before { Snippr.i18n = true }
52
+
53
+ it "should prepend the current locale prefixed with a '_' to a snippr file" do
54
+ I18n.locale = :de
55
+
56
+ Snippr.load(:i18n, :shop).should ==
57
+ "<!-- starting snippr: i18n/shop_de -->\n" <<
58
+ "<p>Willkommen in unserem Shop.</p>\n" <<
59
+ "<!-- closing snippr: i18n/shop_de -->"
60
+ end
61
+
62
+ it "should also work for other locales" do
63
+ I18n.locale = :en
64
+
65
+ Snippr.load(:i18n, :shop).should ==
66
+ "<!-- starting snippr: i18n/shop_en -->\n" <<
67
+ "<p>Welcome to our shop.</p>\n" <<
68
+ "<!-- closing snippr: i18n/shop_en -->"
69
+ end
51
70
  end
52
71
 
53
- end
72
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  require "spec"
2
2
  require "date"
3
3
 
4
- require "snippr"
4
+ require "snippr"
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 7
9
- version: 0.1.7
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Daniel Harrington
@@ -14,13 +14,25 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-07 00:00:00 +02:00
17
+ date: 2010-05-13 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- name: rspec
21
+ name: i18n
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ version_requirements: *id001
32
+ - !ruby/object:Gem::Dependency
33
+ name: rspec
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
24
36
  requirements:
25
37
  - - ">="
26
38
  - !ruby/object:Gem::Version
@@ -30,7 +42,7 @@ dependencies:
30
42
  - 8
31
43
  version: 1.2.8
32
44
  type: :development
33
- version_requirements: *id001
45
+ version_requirements: *id002
34
46
  description:
35
47
  email: me@rubiii.com
36
48
  executables: []
@@ -44,15 +56,18 @@ files:
44
56
  - Rakefile
45
57
  - README.rdoc
46
58
  - lib/snippr/core_ext.rb
59
+ - lib/snippr/helper.rb
60
+ - lib/snippr/i18n.rb
61
+ - lib/snippr/path.rb
47
62
  - lib/snippr/snippr.rb
48
- - lib/snippr/snippr_helper.rb
49
- - lib/snippr/snippr_path.rb
50
63
  - lib/snippr.rb
51
64
  - rails/init.rb
52
65
  - spec/snippr/core_ext_spec.rb
53
66
  - spec/snippr/snippr_spec.rb
54
67
  - spec/spec_helper.rb
55
68
  - spec/fixtures/home.snip
69
+ - spec/fixtures/i18n/shop_de.snip
70
+ - spec/fixtures/i18n/shop_en.snip
56
71
  - spec/fixtures/tariff/einheit.snip
57
72
  - spec/fixtures/topup/someError.snip
58
73
  - spec/fixtures/topup/success.snip
@@ -1,20 +0,0 @@
1
- # = SnipprHelper
2
- #
3
- # This module is automatically included into +ActionView::Base+ when using the Snippr
4
- # component with Rails. It provides a +snippr+ helper method for loading snipprs.
5
- #
6
- # %h1 Topup successful
7
- # .topup.info
8
- # = snippr :topup, :success
9
- module SnipprHelper
10
-
11
- # Returns a snippr specified via +args+.
12
- def snippr(*args)
13
- Snippr.new(*args).to_s
14
- end
15
-
16
- end
17
-
18
- if defined? ActionView::Base
19
- ActionView::Base.send :include, SnipprHelper
20
- end
@@ -1,36 +0,0 @@
1
- # = SnipprPath
2
- #
3
- # This module is included into the Snippr component for retrieving and setting
4
- # the path to the snippr files.
5
- module SnipprPath
6
-
7
- # The snippr path JVM property.
8
- JVMProperty = "cms.snippet.path"
9
-
10
- # Returns the snippr path (from JVM properties if available).
11
- def path
12
- @path ||= JavaLang::System.get_property(JVMProperty) || "" if jruby?
13
- @path ||= ""
14
- end
15
-
16
- # Sets the snippr path as a JVM property.
17
- def path=(path)
18
- @path = path.to_s
19
- end
20
-
21
- private
22
-
23
- # Returns whether the current Ruby platform is JRuby.
24
- def jruby?
25
- RUBY_PLATFORM =~ /java/
26
- end
27
-
28
- module_function :jruby?
29
-
30
- if jruby?
31
- module JavaLang
32
- include_package "java.lang"
33
- end
34
- end
35
-
36
- end