alephant-renderer 0.0.10 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c7001528da4f7c06a258cce649ffe62ca7c4b4f4
4
- data.tar.gz: 25abdd3bc60c640c21f31f1de2c4585955eeae81
3
+ metadata.gz: b124075259704175277b3f266e3693741ef07d0e
4
+ data.tar.gz: 4bc052c06b6c55118e188432c431897592763ef3
5
5
  SHA512:
6
- metadata.gz: 9f8f5068a7969f9bf0faabd88d76740ffb33d7d8e758d9ab5d4ba8a507b3051bed41de5a3d910ac65510d8d2b4f1b6a5890435c16fa5d9c70e19274a19ed4e4d
7
- data.tar.gz: f6612815917388f3e3370d20ed2d8b9fd3323f2c3eb4fd3d5b7f8a77ead7612894d9fedffad04b050a14d6e6cf880449609a28905bc887c4c0c5e5199f8e1fa7
6
+ metadata.gz: 113f572f461dfc7bc1d4a1e1b133eb57e5ee9d6ec38504ca591792e87fb5e85558597c2be7c4270e8edaacfbaa9add81f2237bf2d82c64ce07359b45a69a528f
7
+ data.tar.gz: 4e76b23a5ebffb1a8b9a7542f602acbe80c7aca6951a7d2f5cf9042bd34fad320859b280396cc70e396e2b0602f701006cdcd6246f43365a488c1c78631867b7
data/Gemfile CHANGED
@@ -2,4 +2,3 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in alephant-renderer.gemspec
4
4
  gemspec
5
- gem 'hashie'
data/README.md CHANGED
@@ -76,24 +76,32 @@ class TestModel < Alephant::Views::Base
76
76
  end
77
77
  ```
78
78
 
79
- #### Templates
79
+ #### Translations
80
80
 
81
81
  The best approach with the templates when translations are needed, is to have a base template then a seperate one for each lanuage that's being translated.
82
82
 
83
- The following tag can be used in the templates to perform a translation:
83
+ In each view model, the following method is available for translations:
84
84
 
85
- ```mustache
86
- {{#t}}text.to.translate{{/t}}
85
+ ```ruby
86
+ t 'my_key'
87
87
  ```
88
88
 
89
89
  #### Example
90
90
 
91
- If we had a template called 'test_template.mustache' we would have the following:
91
+ If we had a template called 'test_template.mustache' we would have the following in the model 'test_template.rb':
92
+
93
+ >test_template.rb
94
+
95
+ ```ruby
96
+ def my_translation
97
+ t 'key'
98
+ end
99
+ ```
92
100
 
93
101
  >test_template.mustache
94
102
 
95
103
  ```mustache
96
- {{#t}}translation.key{{/t}}
104
+ {{ my_translation }}
97
105
  ```
98
106
 
99
107
  >locale/en.yml
@@ -101,10 +109,25 @@ If we had a template called 'test_template.mustache' we would have the following
101
109
  ```yaml
102
110
  en:
103
111
  test_template:
104
- translation:
105
- key: 'A translation!'
112
+ key: 'A translation!'
113
+ ```
114
+
115
+ ##### Default
116
+
117
+ By default, if the translation key doesn't exist then the translation key is used as the translation.
118
+
119
+ You can override this behaviour and provide a default:
120
+
121
+ ```ruby
122
+ def my_translation
123
+ t 'missing_key', :default => 'Some default'
124
+ end
125
+
106
126
  ```
107
127
 
128
+ So if the key doesn't exists, then 'Some default' is the translation.
129
+
130
+
108
131
  ## Contributing
109
132
 
110
133
  1. Fork it ( http://github.com/bbc-news/alephant-renderer/fork )
data/Rakefile CHANGED
@@ -3,7 +3,6 @@ $:.unshift File.join(File.dirname(__FILE__), 'lib')
3
3
  require 'rspec/core/rake_task'
4
4
  require 'bundler/gem_tasks'
5
5
  require 'alephant/renderer'
6
-
7
- RSpec::Core::RakeTask.new(:spec)
6
+ require 'rake/rspec'
8
7
 
9
8
  task :default => :spec
@@ -27,6 +27,7 @@ Gem::Specification.new do |spec|
27
27
 
28
28
  spec.add_development_dependency "bundler", "~> 1.5"
29
29
  spec.add_development_dependency "rake"
30
+ spec.add_development_dependency "rake-rspec"
30
31
 
31
32
  spec.add_runtime_dependency 'i18n'
32
33
  spec.add_runtime_dependency 'mustache', '>= 0.99.5'
@@ -1,11 +1,32 @@
1
1
  require 'alephant/renderer/version'
2
- require 'alephant/renderer/engines/mustache'
2
+ require 'alephant/renderer/view_mapper'
3
3
 
4
4
  module Alephant
5
5
  module Renderer
6
- def self.create(*args)
7
- Mustache.new(*args)
6
+ def self.create(config, data)
7
+ Renderer.new(config, data)
8
+ end
9
+
10
+ class Renderer
11
+ attr_reader :config, :data
12
+
13
+ def initialize(config, data)
14
+ @config = config
15
+ @data = data
16
+ end
17
+
18
+ def views
19
+ mapper.generate data
20
+ end
21
+
22
+ private
23
+
24
+ def mapper
25
+ @mapper ||= ViewMapper.new(
26
+ config[:renderer_id],
27
+ config[:view_path]
28
+ )
29
+ end
8
30
  end
9
31
  end
10
32
  end
11
-
@@ -1,5 +1,5 @@
1
1
  module Alephant
2
2
  module Renderer
3
- VERSION = "0.0.10"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
@@ -0,0 +1,57 @@
1
+ require 'alephant/logger'
2
+
3
+ module Alephant
4
+ module Renderer
5
+ class ViewMapper
6
+ include Logger
7
+
8
+ DEFAULT_LOCATION = 'components'
9
+
10
+ def initialize(component_id, view_base_path=nil)
11
+ self.base_path = "#{view_base_path}/#{component_id}" unless view_base_path.nil?
12
+ @component_id = component_id
13
+ end
14
+
15
+ def base_path
16
+ @base_path || DEFAULT_LOCATION
17
+ end
18
+
19
+ def base_path=(path)
20
+ @base_path = File.directory?(path) ? path : (raise "Invalid path: '#{path}'")
21
+ end
22
+
23
+ def generate(data)
24
+ model_locations.reduce({}) do |obj, file|
25
+ obj.tap do |o|
26
+ model_id = model_id_for file
27
+ o[model_id] = model(model_id, data)
28
+ end
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def model(view_id, data)
35
+ require model_location_for view_id
36
+ Views.get_registered_class(view_id).new(data)
37
+ end
38
+
39
+ def model_location_for(view_id)
40
+ File.join(base_path, 'models', "#{view_id}.rb")
41
+ end
42
+
43
+ def model_locations
44
+ Dir[model_base_path]
45
+ end
46
+
47
+ def model_base_path
48
+ "#{base_path}/models/*"
49
+ end
50
+
51
+ def model_id_for(location)
52
+ location.split('/').last.sub(/\.rb/, '')
53
+ end
54
+
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,23 @@
1
+ module Alephant
2
+ module Renderer
3
+ module Views
4
+ @@views = {}
5
+
6
+ def self.register(klass)
7
+ @@views[underscorify(klass.name.split('::').last)] = klass
8
+ end
9
+
10
+ def self.get_registered_class(id)
11
+ @@views[id]
12
+ end
13
+
14
+ def self.underscorify(str)
15
+ str.gsub(/::/, '/').
16
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
17
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
18
+ tr("-", "_").
19
+ downcase
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,46 @@
1
+ require 'alephant/renderer/views'
2
+ require 'hashie'
3
+
4
+ module Alephant
5
+ module Renderer
6
+ module Views
7
+ module Base
8
+ def self.included base
9
+ base.send :include, InstanceMethods
10
+ base.extend ClassMethods
11
+ end
12
+
13
+ module InstanceMethods
14
+ attr_reader :data, :content_type, :base_path
15
+
16
+ def initialize(data = {})
17
+ @data = Hashie::Mash.new data
18
+ @base_path = self.class.base_path
19
+
20
+ setup
21
+ end
22
+
23
+ def to_h
24
+ whitelist.reduce({}) { |m,s| m.tap { |m| m[s] = self.send(s) } }
25
+ end
26
+
27
+ def setup; end
28
+ def whitelist; [] end
29
+ end
30
+
31
+ module ClassMethods
32
+ attr_accessor :base_path
33
+
34
+ def inherited(subclass)
35
+ current_dir = File.dirname(caller.first[/\/[^:]+/])
36
+ dir_path = Pathname.new(File.join(current_dir,'..')).realdirpath
37
+
38
+ subclass.base_path = dir_path.to_s
39
+
40
+ Alephant::Renderer::Views.register(subclass)
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,63 @@
1
+ require 'alephant/renderer/views/base'
2
+ require 'mustache'
3
+ require 'i18n'
4
+
5
+ module Alephant
6
+ module Renderer
7
+ module Views
8
+ class Html < Mustache
9
+ include ::Alephant::Renderer::Views::Base
10
+
11
+ def setup
12
+ @content_type = "text/html"
13
+ load_translations_from base_path
14
+ end
15
+
16
+ def locale
17
+ :en
18
+ end
19
+
20
+ private
21
+
22
+ def load_translations_from(base_path)
23
+ if I18n.load_path.empty?
24
+ I18n.config.enforce_available_locales = false
25
+ I18n.load_path = i18n_load_path_from(base_path)
26
+ I18n.backend.load_translations
27
+ end
28
+ end
29
+
30
+ def i18n_load_path_from(base_path)
31
+ Dir[
32
+ File.join(
33
+ Pathname.new(base_path).parent,
34
+ 'locale',
35
+ '*.yml')
36
+ ]
37
+ .flatten
38
+ .uniq
39
+ end
40
+
41
+ def t(key, params = {})
42
+ I18n.locale = locale
43
+ prefix = /\/([^\/]+)\.mustache/.match(template_file)[1]
44
+ params.merge! :default => key unless params[:default]
45
+ translation = I18n.translate("#{prefix}.#{key}", params)
46
+ end
47
+
48
+ def template
49
+ @template_string ||= File.open(template_file).read
50
+ end
51
+
52
+ def template_name
53
+ Mustache.underscore(self.class.to_s).split('/').last
54
+ end
55
+
56
+ def template_file
57
+ File.join(base_path,'templates',"#{template_name}.#{template_extension}")
58
+ end
59
+
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,21 @@
1
+ require 'alephant/renderer/views/base'
2
+ require 'json'
3
+
4
+ module Alephant
5
+ module Renderer
6
+ module Views
7
+ class Json
8
+ include ::Alephant::Renderer::Views::Base
9
+
10
+ def setup
11
+ @content_type = "application/json"
12
+ end
13
+
14
+ def render
15
+ JSON.generate(to_h)
16
+ end
17
+
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,7 @@
1
+ module MyApp
2
+ class Bar < Alephant::Renderer::Views::Html
3
+ def content
4
+ @data[:content]
5
+ end
6
+ end
7
+ end
@@ -1,7 +1,7 @@
1
1
  module MyApp
2
- class FooFoo < ::Alephant::Views::Base
2
+ class Foo < Alephant::Renderer::Views::Html
3
3
  def content
4
- "content"
4
+ @data[:content]
5
5
  end
6
6
  end
7
7
  end
@@ -0,0 +1,21 @@
1
+ module MyApp
2
+ class Json < Alephant::Renderer::Views::Json
3
+
4
+ def whitelist
5
+ ['item1', 'item2']
6
+ end
7
+
8
+ def item1
9
+ @data[:foo]
10
+ end
11
+
12
+ def item2
13
+ @data[:bar]
14
+ end
15
+
16
+ def baz
17
+ @data[:baz]
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1 @@
1
+ require_relative '../spec_helper'
@@ -1,57 +1,58 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Alephant::Renderer do
4
- let(:template_file) { 'foo' }
5
- let(:base_path) { 'bar/baz' }
6
- let(:model) { :model }
7
-
8
- before(:each) do
9
- @base_path = File.join(
10
- File.dirname(__FILE__),
11
- 'fixtures',
12
- 'components',
13
- 'foo'
14
- )
4
+ let(:config) {
5
+ {
6
+ renderer_id: 'foo',
7
+ view_path: File.join(File.dirname(__FILE__), 'fixtures/components'),
8
+ }
9
+ }
10
+ let(:data) { {content: 'test'} }
11
+
12
+ describe '.create' do
13
+ context 'using valid params' do
14
+ let(:expected) { Alephant::Renderer::Renderer }
15
+
16
+ specify {
17
+ expect(subject.create(config, data)).to be_instance_of expected
18
+ }
19
+ end
15
20
  end
16
21
 
17
- describe ".create(template_file, base_path, model)" do
18
- it "returns a Alephant::Renderer::Mustache" do
19
- expect(subject.create(template_file, base_path, model)).to be_a Alephant::Renderer::Mustache
22
+ describe Alephant::Renderer::Renderer do
23
+ subject { Alephant::Renderer::Renderer.new(config, data) }
24
+
25
+ describe '#config' do
26
+ specify { expect(subject.config).to eql config }
20
27
  end
21
- end
22
28
 
23
- describe Alephant::Renderer::Mustache do
24
- subject { Alephant::Renderer::Mustache }
25
- describe "initialize(template_file, base_path, model)" do
26
- context "template_file = :template_file" do
27
- it "sets the attribute template_file" do
28
- expect(subject.new(template_file, base_path, model).template_file).to eq(template_file)
29
- end
30
- end
29
+ describe '#data' do
30
+ specify { expect(subject.data).to eql data }
31
31
  end
32
32
 
33
- describe "template()" do
34
- it "returns the template" do
35
- instance = subject.new(template_file, @base_path, model)
36
- template = instance.template
37
- expect(template).to eq("{{content}}\n")
33
+ describe '#views' do
34
+ it 'returns a Hash' do
35
+ expect(subject.views).to be_a Hash
38
36
  end
39
- end
40
37
 
41
- describe "render()" do
42
- it 'renders a template returned from template(template_file)' do
43
- ::Mustache
44
- .any_instance
45
- .stub(:render)
46
- .with(:template, :model)
47
- .and_return(:content)
38
+ context 'using three Models' do
39
+ it 'returns three Views in Hash' do
40
+ expect(subject.views.length).to eql 3
41
+ end
42
+ end
43
+
44
+ context 'using `bar`, `foo`, `json` models' do
45
+ it 'contains a View for `bar` model' do
46
+ expect(subject.views.key? 'bar').to be
47
+ end
48
48
 
49
- Alephant::Renderer::Mustache
50
- .any_instance
51
- .stub(:template)
52
- .and_return(:template)
49
+ it 'contains a View for `foo` model' do
50
+ expect(subject.views.key? 'foo').to be
51
+ end
53
52
 
54
- expect(subject.new(template_file, @base_path, model).render).to eq(:content)
53
+ it 'contains a View for `json` model' do
54
+ expect(subject.views.key? 'json').to be
55
+ end
55
56
  end
56
57
  end
57
58
  end
@@ -1,6 +1,10 @@
1
1
  $: << File.join(File.dirname(__FILE__),"..", "lib")
2
2
 
3
3
  require 'pry'
4
+
4
5
  require 'alephant/renderer'
5
- require 'alephant/views/base'
6
+ require 'alephant/renderer/views'
7
+ require 'alephant/renderer/views/json'
8
+ require 'alephant/renderer/views/html'
9
+ require 'alephant/renderer/view_mapper'
6
10
 
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe Alephant::Renderer::ViewMapper do
4
+ let(:component_id) { :foo }
5
+ let(:data) {{ :foo => :bar }}
6
+ let(:path) { File.join(File.dirname(__FILE__), 'fixtures/components') }
7
+
8
+ subject { Alephant::Renderer::ViewMapper }
9
+
10
+ describe "initialize(view_base_path)" do
11
+ context "view_base_path = invalid_path" do
12
+ it "should raise an error" do
13
+ expect {
14
+ subject.new(component_id, './invalid_path')
15
+ }.to raise_error
16
+ end
17
+ end
18
+
19
+ context "view_base_path = '.'" do
20
+ it "sets base_path" do
21
+ expect(
22
+ subject.new(component_id, path).base_path
23
+ ).to eq("#{path}/#{component_id}")
24
+ end
25
+ end
26
+
27
+ context "view_base_path = nil" do
28
+ it "sets base_path" do
29
+ expect(
30
+ subject.new(component_id).base_path
31
+ ).to eq(Alephant::Renderer::ViewMapper::DEFAULT_LOCATION)
32
+ end
33
+ end
34
+ end
35
+
36
+ describe "generate(data)" do
37
+ it "calls create_renderer for each template found" do
38
+ expect(
39
+ subject.new(component_id, path).generate(data).size
40
+ ).to eq(3)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+ require 'fixtures/components/foo/models/json'
3
+
4
+ describe Alephant::Renderer::Views::Json do
5
+ let(:data) { {'foo' => 'test', 'bar' => 'white', 'baz' => 'list'} }
6
+
7
+ describe "#content_type" do
8
+
9
+ it "sets the correct content type" do
10
+ expect(MyApp::Json.new(data).content_type).to eq('application/json')
11
+ end
12
+
13
+ end
14
+
15
+ describe "#to_h" do
16
+
17
+ it "generates a hash based on the white list" do
18
+ expected_result = {'item1' => 'test', 'item2' => 'white'}
19
+ expect(MyApp::Json.new(data).to_h).to eq(expected_result)
20
+ end
21
+
22
+ end
23
+
24
+ describe "#render" do
25
+
26
+ it "returns generated json" do
27
+ expected_result = "{\"item1\":\"test\",\"item2\":\"white\"}"
28
+ expect(MyApp::Json.new(data).render).to eq(expected_result)
29
+ end
30
+
31
+ end
32
+
33
+ end
metadata CHANGED
@@ -1,198 +1,212 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alephant-renderer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Kenny
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-13 00:00:00.000000000 Z
11
+ date: 2014-10-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
15
- version_requirements: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - '>='
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
15
  requirement: !ruby/object:Gem::Requirement
21
16
  requirements:
22
17
  - - '>='
23
18
  - !ruby/object:Gem::Version
24
19
  version: '0'
25
- prerelease: false
26
20
  type: :development
27
- - !ruby/object:Gem::Dependency
28
- name: rspec-nc
21
+ prerelease: false
29
22
  version_requirements: !ruby/object:Gem::Requirement
30
23
  requirements:
31
24
  - - '>='
32
25
  - !ruby/object:Gem::Version
33
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec-nc
34
29
  requirement: !ruby/object:Gem::Requirement
35
30
  requirements:
36
31
  - - '>='
37
32
  - !ruby/object:Gem::Version
38
33
  version: '0'
39
- prerelease: false
40
34
  type: :development
41
- - !ruby/object:Gem::Dependency
42
- name: guard
35
+ prerelease: false
43
36
  version_requirements: !ruby/object:Gem::Requirement
44
37
  requirements:
45
38
  - - '>='
46
39
  - !ruby/object:Gem::Version
47
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: guard
48
43
  requirement: !ruby/object:Gem::Requirement
49
44
  requirements:
50
45
  - - '>='
51
46
  - !ruby/object:Gem::Version
52
47
  version: '0'
53
- prerelease: false
54
48
  type: :development
55
- - !ruby/object:Gem::Dependency
56
- name: guard-rspec
49
+ prerelease: false
57
50
  version_requirements: !ruby/object:Gem::Requirement
58
51
  requirements:
59
52
  - - '>='
60
53
  - !ruby/object:Gem::Version
61
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard-rspec
62
57
  requirement: !ruby/object:Gem::Requirement
63
58
  requirements:
64
59
  - - '>='
65
60
  - !ruby/object:Gem::Version
66
61
  version: '0'
67
- prerelease: false
68
62
  type: :development
69
- - !ruby/object:Gem::Dependency
70
- name: pry
63
+ prerelease: false
71
64
  version_requirements: !ruby/object:Gem::Requirement
72
65
  requirements:
73
66
  - - '>='
74
67
  - !ruby/object:Gem::Version
75
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
76
71
  requirement: !ruby/object:Gem::Requirement
77
72
  requirements:
78
73
  - - '>='
79
74
  - !ruby/object:Gem::Version
80
75
  version: '0'
81
- prerelease: false
82
76
  type: :development
83
- - !ruby/object:Gem::Dependency
84
- name: pry-remote
77
+ prerelease: false
85
78
  version_requirements: !ruby/object:Gem::Requirement
86
79
  requirements:
87
80
  - - '>='
88
81
  - !ruby/object:Gem::Version
89
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-remote
90
85
  requirement: !ruby/object:Gem::Requirement
91
86
  requirements:
92
87
  - - '>='
93
88
  - !ruby/object:Gem::Version
94
89
  version: '0'
95
- prerelease: false
96
90
  type: :development
97
- - !ruby/object:Gem::Dependency
98
- name: pry-nav
91
+ prerelease: false
99
92
  version_requirements: !ruby/object:Gem::Requirement
100
93
  requirements:
101
94
  - - '>='
102
95
  - !ruby/object:Gem::Version
103
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry-nav
104
99
  requirement: !ruby/object:Gem::Requirement
105
100
  requirements:
106
101
  - - '>='
107
102
  - !ruby/object:Gem::Version
108
103
  version: '0'
109
- prerelease: false
110
104
  type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: bundler
113
- version_requirements: !ruby/object:Gem::Requirement
113
+ requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
115
  - - ~>
116
116
  - !ruby/object:Gem::Version
117
117
  version: '1.5'
118
- requirement: !ruby/object:Gem::Requirement
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
119
121
  requirements:
120
122
  - - ~>
121
123
  - !ruby/object:Gem::Version
122
124
  version: '1.5'
123
- prerelease: false
124
- type: :development
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: rake
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
127
134
  version_requirements: !ruby/object:Gem::Requirement
128
135
  requirements:
129
136
  - - '>='
130
137
  - !ruby/object:Gem::Version
131
138
  version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rake-rspec
132
141
  requirement: !ruby/object:Gem::Requirement
133
142
  requirements:
134
143
  - - '>='
135
144
  - !ruby/object:Gem::Version
136
145
  version: '0'
137
- prerelease: false
138
146
  type: :development
139
- - !ruby/object:Gem::Dependency
140
- name: i18n
147
+ prerelease: false
141
148
  version_requirements: !ruby/object:Gem::Requirement
142
149
  requirements:
143
150
  - - '>='
144
151
  - !ruby/object:Gem::Version
145
152
  version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: i18n
146
155
  requirement: !ruby/object:Gem::Requirement
147
156
  requirements:
148
157
  - - '>='
149
158
  - !ruby/object:Gem::Version
150
159
  version: '0'
151
- prerelease: false
152
160
  type: :runtime
153
- - !ruby/object:Gem::Dependency
154
- name: mustache
161
+ prerelease: false
155
162
  version_requirements: !ruby/object:Gem::Requirement
156
163
  requirements:
157
164
  - - '>='
158
165
  - !ruby/object:Gem::Version
159
- version: 0.99.5
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: mustache
160
169
  requirement: !ruby/object:Gem::Requirement
161
170
  requirements:
162
171
  - - '>='
163
172
  - !ruby/object:Gem::Version
164
173
  version: 0.99.5
165
- prerelease: false
166
174
  type: :runtime
167
- - !ruby/object:Gem::Dependency
168
- name: alephant-logger
175
+ prerelease: false
169
176
  version_requirements: !ruby/object:Gem::Requirement
170
177
  requirements:
171
178
  - - '>='
172
179
  - !ruby/object:Gem::Version
173
- version: '0'
180
+ version: 0.99.5
181
+ - !ruby/object:Gem::Dependency
182
+ name: alephant-logger
174
183
  requirement: !ruby/object:Gem::Requirement
175
184
  requirements:
176
185
  - - '>='
177
186
  - !ruby/object:Gem::Version
178
187
  version: '0'
179
- prerelease: false
180
188
  type: :runtime
181
- - !ruby/object:Gem::Dependency
182
- name: hashie
189
+ prerelease: false
183
190
  version_requirements: !ruby/object:Gem::Requirement
184
191
  requirements:
185
192
  - - '>='
186
193
  - !ruby/object:Gem::Version
187
194
  version: '0'
195
+ - !ruby/object:Gem::Dependency
196
+ name: hashie
188
197
  requirement: !ruby/object:Gem::Requirement
189
198
  requirements:
190
199
  - - '>='
191
200
  - !ruby/object:Gem::Version
192
201
  version: '0'
193
- prerelease: false
194
202
  type: :runtime
195
- description:
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - '>='
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
209
+ description:
196
210
  email:
197
211
  - kenoir@gmail.com
198
212
  executables: []
@@ -209,25 +223,26 @@ files:
209
223
  - Rakefile
210
224
  - alephant-renderer.gemspec
211
225
  - lib/alephant/renderer.rb
212
- - lib/alephant/renderer/engines/mustache.rb
213
226
  - lib/alephant/renderer/version.rb
214
- - lib/alephant/views.rb
215
- - lib/alephant/views/base.rb
227
+ - lib/alephant/renderer/view_mapper.rb
228
+ - lib/alephant/renderer/views.rb
229
+ - lib/alephant/renderer/views/base.rb
230
+ - lib/alephant/renderer/views/html.rb
231
+ - lib/alephant/renderer/views/json.rb
232
+ - spec/fixtures/components/foo/models/bar.rb
216
233
  - spec/fixtures/components/foo/models/foo.rb
217
- - spec/fixtures/components/foo/models/welsh.rb
218
- - spec/fixtures/components/foo/templates/bar.mustache
219
- - spec/fixtures/components/foo/templates/foo.mustache
220
- - spec/fixtures/components/locale/cy.yml
221
- - spec/fixtures/components/locale/en.yml
234
+ - spec/fixtures/components/foo/models/json.rb
235
+ - spec/integration/spec_helper.rb
222
236
  - spec/renderer_spec.rb
223
237
  - spec/spec_helper.rb
224
- - spec/views_base_spec.rb
238
+ - spec/view_mapper_spec.rb
239
+ - spec/views_json_spec.rb
225
240
  - trans.yml
226
241
  homepage: ''
227
242
  licenses:
228
243
  - MIT
229
244
  metadata: {}
230
- post_install_message:
245
+ post_install_message:
231
246
  rdoc_options: []
232
247
  require_paths:
233
248
  - lib
@@ -242,18 +257,17 @@ required_rubygems_version: !ruby/object:Gem::Requirement
242
257
  - !ruby/object:Gem::Version
243
258
  version: '0'
244
259
  requirements: []
245
- rubyforge_project:
246
- rubygems_version: 2.1.9
247
- signing_key:
260
+ rubyforge_project:
261
+ rubygems_version: 2.2.2
262
+ signing_key:
248
263
  specification_version: 4
249
264
  summary: Render HTML snippets
250
265
  test_files:
266
+ - spec/fixtures/components/foo/models/bar.rb
251
267
  - spec/fixtures/components/foo/models/foo.rb
252
- - spec/fixtures/components/foo/models/welsh.rb
253
- - spec/fixtures/components/foo/templates/bar.mustache
254
- - spec/fixtures/components/foo/templates/foo.mustache
255
- - spec/fixtures/components/locale/cy.yml
256
- - spec/fixtures/components/locale/en.yml
268
+ - spec/fixtures/components/foo/models/json.rb
269
+ - spec/integration/spec_helper.rb
257
270
  - spec/renderer_spec.rb
258
271
  - spec/spec_helper.rb
259
- - spec/views_base_spec.rb
272
+ - spec/view_mapper_spec.rb
273
+ - spec/views_json_spec.rb
@@ -1,63 +0,0 @@
1
- require 'alephant/logger'
2
- require 'mustache'
3
- require 'i18n'
4
-
5
- module Alephant
6
- module Renderer
7
- class Mustache
8
- include ::Alephant::Logger
9
-
10
- attr_reader :template_file
11
-
12
- def initialize(template_file, base_path, model)
13
- @template_file = template_file
14
- @base_path = base_path
15
- @model = model
16
-
17
- load_translations_from base_path
18
-
19
- logger.info("Renderer.initialize: end with @base_path set to #{@base_path}")
20
- end
21
-
22
- def render
23
- logger.info("Renderer.render: rendered template #{template_file}")
24
- ::Mustache.render(template, @model)
25
- end
26
-
27
- def template
28
- template_location = File.join(@base_path, 'templates', "#{template_file}.mustache")
29
-
30
- begin
31
- logger.info("Renderer.template: #{template_location}")
32
- File.open(template_location).read
33
- rescue Exception => e
34
- logger.error("Renderer.template: view template #{template_file} not found")
35
- end
36
- end
37
-
38
- private
39
-
40
- def load_translations_from(base_path)
41
- if I18n.load_path.empty?
42
- I18n.config.enforce_available_locales = false
43
- I18n.load_path = i18n_load_path_from(base_path)
44
- I18n.backend.load_translations
45
- end
46
- end
47
-
48
- def i18n_load_path_from(base_path)
49
- Dir[
50
- File.join(
51
- Pathname.new(base_path).parent,
52
- 'locale',
53
- '*.yml')
54
- ]
55
- .flatten
56
- .uniq
57
- end
58
-
59
- end
60
-
61
- end
62
- end
63
-
@@ -1,27 +0,0 @@
1
- require 'alephant/views/base'
2
-
3
- module Alephant
4
- module Views
5
- @@views = {}
6
-
7
- def self.register(klass)
8
- id = ::Alephant::Views.underscorify(
9
- klass.name.split('::').last
10
- )
11
-
12
- @@views[id] = klass
13
- end
14
-
15
- def self.get_registered_class(id)
16
- @@views[id]
17
- end
18
-
19
- def self.underscorify(str)
20
- str.gsub(/::/, '/').
21
- gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
22
- gsub(/([a-z\d])([A-Z])/,'\1_\2').
23
- tr("-", "_").
24
- downcase
25
- end
26
- end
27
- end
@@ -1,31 +0,0 @@
1
- require 'mustache'
2
- require 'alephant/views'
3
- require 'hashie'
4
- require 'json'
5
- require 'i18n'
6
-
7
- module Alephant::Views
8
- class Base < Mustache
9
- attr_accessor :data
10
-
11
- def initialize(data = {})
12
- @data = Hashie::Mash.new data
13
- end
14
-
15
- def locale
16
- :en
17
- end
18
-
19
- def t(key, params = {})
20
- I18n.locale = locale
21
- prefix = /\/([^\/]+)\.mustache/.match(template_file)[1]
22
- params.merge! :default => key unless params[:default]
23
- translation = I18n.translate("#{prefix}.#{key}", params)
24
- end
25
-
26
- def self.inherited(subclass)
27
- ::Alephant::Views.register(subclass)
28
- end
29
- end
30
- end
31
-
@@ -1,7 +0,0 @@
1
- module Fixtures
2
- class Welsh < ::Alephant::Views::Base
3
- def locale
4
- :cy
5
- end
6
- end
7
- end
@@ -1,3 +0,0 @@
1
- cy:
2
- welsh:
3
- bar: 'Welsh Translation'
@@ -1,3 +0,0 @@
1
- en:
2
- base:
3
- bar: 'Translation'
@@ -1,41 +0,0 @@
1
- require 'spec_helper'
2
- require 'fixtures/components/foo/models/welsh'
3
-
4
- describe Alephant::Views::Base do
5
- let(:base_path) do
6
- File.join(
7
- File.dirname(__FILE__),
8
- 'fixtures',
9
- 'components',
10
- 'foo'
11
- )
12
- end
13
-
14
- describe "t(key, param{})" do
15
- it "Translates a key for the default locale" do
16
- Alephant::Renderer.create('foo', base_path, :model)
17
-
18
- expect(subject.t('bar')).to eq('Translation')
19
- end
20
-
21
- it "Translates a key for an overridden locale" do
22
- Alephant::Renderer.create('welsh', base_path, :model)
23
-
24
- expect(Fixtures::Welsh.new.t('bar')).to eq('Welsh Translation')
25
- end
26
-
27
- it "Uses the translation keys value if no translation and no default set" do
28
- Alephant::Renderer.create('welsh', base_path, :model)
29
-
30
- expect(Fixtures::Welsh.new.t('non_existent')).to eq('non_existent')
31
- end
32
- end
33
-
34
- describe "t(key, param{:default => 'A default'})" do
35
- it "Uses the passed in default if translation key does not exist" do
36
- Alephant::Renderer.create('welsh', base_path, :model)
37
-
38
- expect(Fixtures::Welsh.new.t('non_existent', :default => 'A default')).to eq('A default')
39
- end
40
- end
41
- end