poppycock 0.0.1
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/bin/poppycock +0 -0
 - data/lib/poppycock.rb +26 -0
 - data/lib/poppycock/ext.rb +15 -0
 - data/lib/poppycock/localize.rb +62 -0
 - data/lib/poppycock/merb.rb +7 -0
 - data/spec/fixtures/ja.lang.yaml +1 -0
 - data/spec/fixtures/no.lang.yaml +1 -0
 - data/spec/localize_spec.rb +48 -0
 - data/spec/poppycock_spec.rb +8 -0
 - data/spec/spec_helper.rb +2 -0
 - data/spec/string_spec.rb +18 -0
 - metadata +65 -0
 
    
        data/bin/poppycock
    ADDED
    
    | 
         
            File without changes
         
     | 
    
        data/lib/poppycock.rb
    ADDED
    
    | 
         @@ -0,0 +1,26 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            $:.unshift File.dirname(__FILE__)
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            require 'yaml'
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            require 'poppycock/ext'
         
     | 
| 
      
 6 
     | 
    
         
            +
            require 'poppycock/localize'
         
     | 
| 
      
 7 
     | 
    
         
            +
             
     | 
| 
      
 8 
     | 
    
         
            +
            String.send(:include, Poppycock::StringExt)
         
     | 
| 
      
 9 
     | 
    
         
            +
             
     | 
| 
      
 10 
     | 
    
         
            +
            module Poppycock
         
     | 
| 
      
 11 
     | 
    
         
            +
              extend Localize
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
              VERSION = '0.0.1'
         
     | 
| 
      
 14 
     | 
    
         
            +
              DEFAULT_OPTIONS = {
         
     | 
| 
      
 15 
     | 
    
         
            +
                :languages => File.join(Dir.pwd, 'languages'),
         
     | 
| 
      
 16 
     | 
    
         
            +
                :default_language => :en
         
     | 
| 
      
 17 
     | 
    
         
            +
              }
         
     | 
| 
      
 18 
     | 
    
         
            +
             
     | 
| 
      
 19 
     | 
    
         
            +
              def self.options
         
     | 
| 
      
 20 
     | 
    
         
            +
                @@options ||= DEFAULT_OPTIONS.dup
         
     | 
| 
      
 21 
     | 
    
         
            +
              end
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
              def self.setup(options = {})
         
     | 
| 
      
 24 
     | 
    
         
            +
                self.options.merge!(options)
         
     | 
| 
      
 25 
     | 
    
         
            +
              end
         
     | 
| 
      
 26 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,15 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module Poppycock
         
     | 
| 
      
 2 
     | 
    
         
            +
              module StringExt
         
     | 
| 
      
 3 
     | 
    
         
            +
                def translate(*args)
         
     | 
| 
      
 4 
     | 
    
         
            +
                  return old_brackets(*args) unless args.first.is_a? Symbol
         
     | 
| 
      
 5 
     | 
    
         
            +
                  Poppycock.translate(self, args.shift, *args)
         
     | 
| 
      
 6 
     | 
    
         
            +
                end
         
     | 
| 
      
 7 
     | 
    
         
            +
             
     | 
| 
      
 8 
     | 
    
         
            +
                def self.included(base)
         
     | 
| 
      
 9 
     | 
    
         
            +
                  base.class_eval do
         
     | 
| 
      
 10 
     | 
    
         
            +
                    alias :old_brackets :[]
         
     | 
| 
      
 11 
     | 
    
         
            +
                    alias :[] :translate
         
     | 
| 
      
 12 
     | 
    
         
            +
                  end
         
     | 
| 
      
 13 
     | 
    
         
            +
                end
         
     | 
| 
      
 14 
     | 
    
         
            +
              end
         
     | 
| 
      
 15 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,62 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module Poppycock
         
     | 
| 
      
 2 
     | 
    
         
            +
              module Localize
         
     | 
| 
      
 3 
     | 
    
         
            +
                def current_language; @@current_language ||= Poppycock.options[:default_language] end
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
                def current_language=(language)
         
     | 
| 
      
 6 
     | 
    
         
            +
                  load_languages! if Poppycock.options[:reload_languages]
         
     | 
| 
      
 7 
     | 
    
         
            +
                  @@current_language = language
         
     | 
| 
      
 8 
     | 
    
         
            +
                end
         
     | 
| 
      
 9 
     | 
    
         
            +
             
     | 
| 
      
 10 
     | 
    
         
            +
                def languages
         
     | 
| 
      
 11 
     | 
    
         
            +
                  (@@languages ||= load_languages!).keys
         
     | 
| 
      
 12 
     | 
    
         
            +
                end
         
     | 
| 
      
 13 
     | 
    
         
            +
             
     | 
| 
      
 14 
     | 
    
         
            +
                # do i need a list of reserved keys like gibberish? I DON'T KNOW!!
         
     | 
| 
      
 15 
     | 
    
         
            +
                def translate(default, key, *args)
         
     | 
| 
      
 16 
     | 
    
         
            +
                  target = translations[key] || default
         
     | 
| 
      
 17 
     | 
    
         
            +
                  interpolate(target.dup, *args.dup)
         
     | 
| 
      
 18 
     | 
    
         
            +
                end
         
     | 
| 
      
 19 
     | 
    
         
            +
             
     | 
| 
      
 20 
     | 
    
         
            +
                def with_language(language, &block)
         
     | 
| 
      
 21 
     | 
    
         
            +
                  tmp = current_language
         
     | 
| 
      
 22 
     | 
    
         
            +
                  self.current_language = language
         
     | 
| 
      
 23 
     | 
    
         
            +
                  yield
         
     | 
| 
      
 24 
     | 
    
         
            +
                  self.current_language = tmp
         
     | 
| 
      
 25 
     | 
    
         
            +
                end
         
     | 
| 
      
 26 
     | 
    
         
            +
             
     | 
| 
      
 27 
     | 
    
         
            +
                private
         
     | 
| 
      
 28 
     | 
    
         
            +
                def translations
         
     | 
| 
      
 29 
     | 
    
         
            +
                  @@languages[current_language] || {}
         
     | 
| 
      
 30 
     | 
    
         
            +
                end
         
     | 
| 
      
 31 
     | 
    
         
            +
             
     | 
| 
      
 32 
     | 
    
         
            +
                def interpolate(string, *args)
         
     | 
| 
      
 33 
     | 
    
         
            +
                  if args.last.is_a? Hash
         
     | 
| 
      
 34 
     | 
    
         
            +
                    hash_interpolate(string, args.last)
         
     | 
| 
      
 35 
     | 
    
         
            +
                  else
         
     | 
| 
      
 36 
     | 
    
         
            +
                    string_interpolate(string, args)
         
     | 
| 
      
 37 
     | 
    
         
            +
                  end
         
     | 
| 
      
 38 
     | 
    
         
            +
                end
         
     | 
| 
      
 39 
     | 
    
         
            +
             
     | 
| 
      
 40 
     | 
    
         
            +
                def hash_interpolate(string, hash)
         
     | 
| 
      
 41 
     | 
    
         
            +
                  hash.inject(string) do |target, (search, replace)|
         
     | 
| 
      
 42 
     | 
    
         
            +
                    target.sub("{#{search}}", replace)
         
     | 
| 
      
 43 
     | 
    
         
            +
                  end 
         
     | 
| 
      
 44 
     | 
    
         
            +
                end
         
     | 
| 
      
 45 
     | 
    
         
            +
             
     | 
| 
      
 46 
     | 
    
         
            +
                def string_interpolate(string, strings)
         
     | 
| 
      
 47 
     | 
    
         
            +
                  string.gsub(/\{\w+\}/) { strings.shift }
         
     | 
| 
      
 48 
     | 
    
         
            +
                end
         
     | 
| 
      
 49 
     | 
    
         
            +
             
     | 
| 
      
 50 
     | 
    
         
            +
                def language_files
         
     | 
| 
      
 51 
     | 
    
         
            +
                  Dir["#{Poppycock.options[:languages]}/**/*.lang.yaml"]
         
     | 
| 
      
 52 
     | 
    
         
            +
                end
         
     | 
| 
      
 53 
     | 
    
         
            +
             
     | 
| 
      
 54 
     | 
    
         
            +
                def load_languages!
         
     | 
| 
      
 55 
     | 
    
         
            +
                  language_files.inject({}) do |languages, file|
         
     | 
| 
      
 56 
     | 
    
         
            +
                    id = File.basename(file, '.lang.yaml').to_sym
         
     | 
| 
      
 57 
     | 
    
         
            +
                    languages[id] ||= {}
         
     | 
| 
      
 58 
     | 
    
         
            +
                    languages.update(id => YAML.load_file(file))
         
     | 
| 
      
 59 
     | 
    
         
            +
                  end
         
     | 
| 
      
 60 
     | 
    
         
            +
                end
         
     | 
| 
      
 61 
     | 
    
         
            +
              end
         
     | 
| 
      
 62 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            :good_day: こんいちは
         
     | 
| 
         @@ -0,0 +1 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            :good_day: God dag!
         
     | 
| 
         @@ -0,0 +1,48 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require File.dirname(__FILE__) + '/spec_helper'
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            describe Poppycock::Localize do
         
     | 
| 
      
 4 
     | 
    
         
            +
              before(:all) do
         
     | 
| 
      
 5 
     | 
    
         
            +
                Poppycock.setup(:languages => File.join(File.dirname(__FILE__), 'fixtures'))
         
     | 
| 
      
 6 
     | 
    
         
            +
              end
         
     | 
| 
      
 7 
     | 
    
         
            +
             
     | 
| 
      
 8 
     | 
    
         
            +
              it 'should know which languages are available' do
         
     | 
| 
      
 9 
     | 
    
         
            +
                Poppycock.languages.should include(:no, :ja)
         
     | 
| 
      
 10 
     | 
    
         
            +
              end
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
              it 'should translate simple strings' do
         
     | 
| 
      
 13 
     | 
    
         
            +
                Poppycock.with_language(:no) do
         
     | 
| 
      
 14 
     | 
    
         
            +
                  "Good day!"[:good_day].should == "God dag!"
         
     | 
| 
      
 15 
     | 
    
         
            +
                end
         
     | 
| 
      
 16 
     | 
    
         
            +
              end
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
      
 18 
     | 
    
         
            +
              it "shouldn't barf on an unknown language" do
         
     | 
| 
      
 19 
     | 
    
         
            +
                Poppycock.with_language(:es) do
         
     | 
| 
      
 20 
     | 
    
         
            +
                  "Good day!"[:good_day].should == "Good day!"
         
     | 
| 
      
 21 
     | 
    
         
            +
                end
         
     | 
| 
      
 22 
     | 
    
         
            +
              end
         
     | 
| 
      
 23 
     | 
    
         
            +
             
     | 
| 
      
 24 
     | 
    
         
            +
              it "should handle those wacky unicodes" do
         
     | 
| 
      
 25 
     | 
    
         
            +
                Poppycock.with_language(:ja) do
         
     | 
| 
      
 26 
     | 
    
         
            +
                  "Good day!"[:good_day].should == "こんいちは"
         
     | 
| 
      
 27 
     | 
    
         
            +
                end
         
     | 
| 
      
 28 
     | 
    
         
            +
              end
         
     | 
| 
      
 29 
     | 
    
         
            +
              
         
     | 
| 
      
 30 
     | 
    
         
            +
              it 'should do string interpolation' do
         
     | 
| 
      
 31 
     | 
    
         
            +
                "Good day {man}!"[:good_day, "YOU!"].should == "Good day YOU!!"
         
     | 
| 
      
 32 
     | 
    
         
            +
              end
         
     | 
| 
      
 33 
     | 
    
         
            +
             
     | 
| 
      
 34 
     | 
    
         
            +
              it 'should do hash interpolation' do
         
     | 
| 
      
 35 
     | 
    
         
            +
                "I want {food} and {drink}!"[:empty, { :drink => "melon soda", :food => 'ramen'}].should == "I want ramen and melon soda!"
         
     | 
| 
      
 36 
     | 
    
         
            +
              end
         
     | 
| 
      
 37 
     | 
    
         
            +
             
     | 
| 
      
 38 
     | 
    
         
            +
              it "shouldn't normally reload the language files" do
         
     | 
| 
      
 39 
     | 
    
         
            +
                Poppycock.should_not_receive(:load_languages!)
         
     | 
| 
      
 40 
     | 
    
         
            +
                Poppycock.with_language(:ja) { nil }
         
     | 
| 
      
 41 
     | 
    
         
            +
              end
         
     | 
| 
      
 42 
     | 
    
         
            +
             
     | 
| 
      
 43 
     | 
    
         
            +
              it "should reload the language files when reloading is enabled" do
         
     | 
| 
      
 44 
     | 
    
         
            +
                Poppycock.setup(:reload_languages => true)
         
     | 
| 
      
 45 
     | 
    
         
            +
                Poppycock.should_receive(:load_languages!).twice # Once when going japanese, once after teh block
         
     | 
| 
      
 46 
     | 
    
         
            +
                Poppycock.with_language(:ja) { nil }
         
     | 
| 
      
 47 
     | 
    
         
            +
              end
         
     | 
| 
      
 48 
     | 
    
         
            +
            end
         
     | 
    
        data/spec/spec_helper.rb
    ADDED
    
    
    
        data/spec/string_spec.rb
    ADDED
    
    | 
         @@ -0,0 +1,18 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require File.dirname(__FILE__) + '/spec_helper'
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            describe 'Poppycock::StringExt' do
         
     | 
| 
      
 4 
     | 
    
         
            +
              before(:all) do
         
     | 
| 
      
 5 
     | 
    
         
            +
                Poppycock.stub!(:translate)
         
     | 
| 
      
 6 
     | 
    
         
            +
              end
         
     | 
| 
      
 7 
     | 
    
         
            +
             
     | 
| 
      
 8 
     | 
    
         
            +
              it 'should call Poppycock.translate when called with a symbol' do
         
     | 
| 
      
 9 
     | 
    
         
            +
                Poppycock.should_receive(:translate).with('A very important string', :hello, 5)
         
     | 
| 
      
 10 
     | 
    
         
            +
                "A very important string"[:hello, 5]
         
     | 
| 
      
 11 
     | 
    
         
            +
              end
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
              it 'should call the old [] method when not called with a symbol' do
         
     | 
| 
      
 14 
     | 
    
         
            +
                string = "abcde"
         
     | 
| 
      
 15 
     | 
    
         
            +
                Poppycock.should_not_receive(:translate)
         
     | 
| 
      
 16 
     | 
    
         
            +
                string[2].should == "c"[0]
         
     | 
| 
      
 17 
     | 
    
         
            +
              end
         
     | 
| 
      
 18 
     | 
    
         
            +
            end
         
     | 
    
        metadata
    ADDED
    
    | 
         @@ -0,0 +1,65 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            --- !ruby/object:Gem::Specification 
         
     | 
| 
      
 2 
     | 
    
         
            +
            name: poppycock
         
     | 
| 
      
 3 
     | 
    
         
            +
            version: !ruby/object:Gem::Version 
         
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.0.1
         
     | 
| 
      
 5 
     | 
    
         
            +
            platform: ruby
         
     | 
| 
      
 6 
     | 
    
         
            +
            authors: 
         
     | 
| 
      
 7 
     | 
    
         
            +
            - raspberry lemon
         
     | 
| 
      
 8 
     | 
    
         
            +
            autorequire: 
         
     | 
| 
      
 9 
     | 
    
         
            +
            bindir: bin
         
     | 
| 
      
 10 
     | 
    
         
            +
            cert_chain: []
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
            date: 2008-05-07 00:00:00 +02:00
         
     | 
| 
      
 13 
     | 
    
         
            +
            default_executable: 
         
     | 
| 
      
 14 
     | 
    
         
            +
            dependencies: []
         
     | 
| 
      
 15 
     | 
    
         
            +
             
     | 
| 
      
 16 
     | 
    
         
            +
            description: 
         
     | 
| 
      
 17 
     | 
    
         
            +
            email: lemon@raspberry-style.net
         
     | 
| 
      
 18 
     | 
    
         
            +
            executables: []
         
     | 
| 
      
 19 
     | 
    
         
            +
             
     | 
| 
      
 20 
     | 
    
         
            +
            extensions: []
         
     | 
| 
      
 21 
     | 
    
         
            +
             
     | 
| 
      
 22 
     | 
    
         
            +
            extra_rdoc_files: []
         
     | 
| 
      
 23 
     | 
    
         
            +
             
     | 
| 
      
 24 
     | 
    
         
            +
            files: 
         
     | 
| 
      
 25 
     | 
    
         
            +
            - bin/poppycock
         
     | 
| 
      
 26 
     | 
    
         
            +
            - lib/poppycock
         
     | 
| 
      
 27 
     | 
    
         
            +
            - lib/poppycock/ext.rb
         
     | 
| 
      
 28 
     | 
    
         
            +
            - lib/poppycock/localize.rb
         
     | 
| 
      
 29 
     | 
    
         
            +
            - lib/poppycock/merb.rb
         
     | 
| 
      
 30 
     | 
    
         
            +
            - lib/poppycock.rb
         
     | 
| 
      
 31 
     | 
    
         
            +
            - spec/fixtures
         
     | 
| 
      
 32 
     | 
    
         
            +
            - spec/fixtures/ja.lang.yaml
         
     | 
| 
      
 33 
     | 
    
         
            +
            - spec/fixtures/no.lang.yaml
         
     | 
| 
      
 34 
     | 
    
         
            +
            - spec/localize_spec.rb
         
     | 
| 
      
 35 
     | 
    
         
            +
            - spec/poppycock_spec.rb
         
     | 
| 
      
 36 
     | 
    
         
            +
            - spec/spec_helper.rb
         
     | 
| 
      
 37 
     | 
    
         
            +
            - spec/string_spec.rb
         
     | 
| 
      
 38 
     | 
    
         
            +
            has_rdoc: false
         
     | 
| 
      
 39 
     | 
    
         
            +
            homepage: ""
         
     | 
| 
      
 40 
     | 
    
         
            +
            post_install_message: 
         
     | 
| 
      
 41 
     | 
    
         
            +
            rdoc_options: []
         
     | 
| 
      
 42 
     | 
    
         
            +
             
     | 
| 
      
 43 
     | 
    
         
            +
            require_paths: 
         
     | 
| 
      
 44 
     | 
    
         
            +
            - lib
         
     | 
| 
      
 45 
     | 
    
         
            +
            required_ruby_version: !ruby/object:Gem::Requirement 
         
     | 
| 
      
 46 
     | 
    
         
            +
              requirements: 
         
     | 
| 
      
 47 
     | 
    
         
            +
              - - ">="
         
     | 
| 
      
 48 
     | 
    
         
            +
                - !ruby/object:Gem::Version 
         
     | 
| 
      
 49 
     | 
    
         
            +
                  version: "0"
         
     | 
| 
      
 50 
     | 
    
         
            +
              version: 
         
     | 
| 
      
 51 
     | 
    
         
            +
            required_rubygems_version: !ruby/object:Gem::Requirement 
         
     | 
| 
      
 52 
     | 
    
         
            +
              requirements: 
         
     | 
| 
      
 53 
     | 
    
         
            +
              - - ">="
         
     | 
| 
      
 54 
     | 
    
         
            +
                - !ruby/object:Gem::Version 
         
     | 
| 
      
 55 
     | 
    
         
            +
                  version: "0"
         
     | 
| 
      
 56 
     | 
    
         
            +
              version: 
         
     | 
| 
      
 57 
     | 
    
         
            +
            requirements: []
         
     | 
| 
      
 58 
     | 
    
         
            +
             
     | 
| 
      
 59 
     | 
    
         
            +
            rubyforge_project: 
         
     | 
| 
      
 60 
     | 
    
         
            +
            rubygems_version: 1.1.1
         
     | 
| 
      
 61 
     | 
    
         
            +
            signing_key: 
         
     | 
| 
      
 62 
     | 
    
         
            +
            specification_version: 2
         
     | 
| 
      
 63 
     | 
    
         
            +
            summary: gibberish for merb...and other stuff
         
     | 
| 
      
 64 
     | 
    
         
            +
            test_files: []
         
     | 
| 
      
 65 
     | 
    
         
            +
             
     |