settings_js 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 @@
1
+ spec/javascripts/generated/
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm ruby-1.9.3-p327@settings-js --create
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,76 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ settings_js (0.0.1)
5
+ active_support
6
+ json
7
+ sprockets
8
+
9
+ GEM
10
+ remote: http://rubygems.org/
11
+ specs:
12
+ active_support (3.0.0)
13
+ activesupport (= 3.0.0)
14
+ activesupport (3.0.0)
15
+ addressable (2.3.2)
16
+ bourne (1.1.2)
17
+ mocha (= 0.10.5)
18
+ childprocess (0.3.6)
19
+ ffi (~> 1.0, >= 1.0.6)
20
+ diff-lcs (1.1.3)
21
+ ffi (1.2.0)
22
+ hike (1.2.1)
23
+ jasmine (1.3.0)
24
+ jasmine-core (>= 1.3.0)
25
+ rack (~> 1.0)
26
+ rspec (>= 1.3.1)
27
+ selenium-webdriver (>= 0.1.3)
28
+ jasmine-core (1.3.0)
29
+ json (1.7.5)
30
+ libwebsocket (0.1.7.1)
31
+ addressable
32
+ websocket
33
+ metaclass (0.0.1)
34
+ mocha (0.10.5)
35
+ metaclass (~> 0.0.1)
36
+ multi_json (1.4.0)
37
+ rack (1.4.1)
38
+ rspec (2.12.0)
39
+ rspec-core (~> 2.12.0)
40
+ rspec-expectations (~> 2.12.0)
41
+ rspec-mocks (~> 2.12.0)
42
+ rspec-core (2.12.0)
43
+ rspec-expectations (2.12.0)
44
+ diff-lcs (~> 1.1.3)
45
+ rspec-mocks (2.12.0)
46
+ rubyzip (0.9.9)
47
+ selenium-webdriver (2.26.0)
48
+ childprocess (>= 0.2.5)
49
+ libwebsocket (~> 0.1.3)
50
+ multi_json (~> 1.0)
51
+ rubyzip
52
+ settingslogic (2.0.8)
53
+ shoulda (3.3.2)
54
+ shoulda-context (~> 1.0.1)
55
+ shoulda-matchers (~> 1.4.1)
56
+ shoulda-context (1.0.1)
57
+ shoulda-matchers (1.4.2)
58
+ activesupport (>= 3.0.0)
59
+ bourne (~> 1.1.2)
60
+ sprockets (2.8.1)
61
+ hike (~> 1.2)
62
+ multi_json (~> 1.0)
63
+ rack (~> 1.0)
64
+ tilt (~> 1.1, != 1.3.0)
65
+ tilt (1.3.3)
66
+ websocket (1.0.4)
67
+
68
+ PLATFORMS
69
+ ruby
70
+
71
+ DEPENDENCIES
72
+ jasmine
73
+ rspec
74
+ settings_js!
75
+ settingslogic
76
+ shoulda
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+
2
+ begin
3
+ require 'jasmine'
4
+ load 'jasmine/tasks/jasmine.rake'
5
+ rescue LoadError
6
+ task :jasmine do
7
+ abort "Jasmine is not available. In order to run jasmine, you must: (sudo) gem install jasmine"
8
+ end
9
+ end
@@ -0,0 +1 @@
1
+ var Settings = <%= ::SettingsJs::Merger.new.to_json %>;
@@ -0,0 +1,22 @@
1
+ module SettingsJs
2
+ module Adapters
3
+ class SettingsLogic
4
+
5
+ attr_accessor :klass
6
+
7
+ def initialize(klass)
8
+ self.klass = klass
9
+ end
10
+
11
+ def to_hash(base_key)
12
+ keys = base_key.split(/\./)
13
+ base_hash = klass.send(keys.shift)
14
+
15
+ keys.each { |key| base_hash = base_hash.send(key) }
16
+
17
+ base_hash
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,12 @@
1
+ require 'singleton'
2
+
3
+ module SettingsJs
4
+ class Config
5
+ include ::Singleton
6
+
7
+ attr_accessor :adapter,
8
+ :keys,
9
+ :klass
10
+
11
+ end
12
+ end
@@ -0,0 +1,32 @@
1
+ require 'active_support/inflector'
2
+ require 'json'
3
+
4
+ module SettingsJs
5
+ class Merger
6
+
7
+ def to_hash
8
+ unless config.keys.respond_to?(:reduce)
9
+ raise TypeError.new('the configuration key "keys" must be reduceable')
10
+ end
11
+
12
+ config.keys.reduce({}) { |hash, base_key| hash.merge(adapter.to_hash(base_key)) }
13
+ end
14
+
15
+ def to_json
16
+ to_hash.to_json
17
+ end
18
+
19
+ private
20
+
21
+ def config
22
+ SettingsJs::Config.instance
23
+ end
24
+
25
+ def adapter
26
+ adapter_proxy_path = "settings_js/adapters/#{config.adapter}"
27
+
28
+ require adapter_proxy_path
29
+ adapter_proxy_path.classify.constantize.new(config.klass)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,11 @@
1
+ require 'settings_js/config'
2
+ require 'settings_js/merger'
3
+
4
+ module SettingsJs
5
+
6
+ def self.configuration(&block)
7
+ block.call(SettingsJs::Config.instance)
8
+ end
9
+
10
+ end
11
+
@@ -0,0 +1,20 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'settings_js'
3
+ s.version = '0.0.1'
4
+ s.date = '2012-11-30'
5
+ s.summary = 'Sharing same application settings between ruby and javascript'
6
+ s.authors = ['Kevin Disneur']
7
+ s.email = 'kevin.disneur@gmail.com'
8
+ s.files = `git ls-files`.split(/\n/)
9
+ s.test_files = `git ls-files -- spec/*`.split(/\n/)
10
+ s.homepage = 'http://rubygems.org/gems/settings_js'
11
+
12
+ s.add_dependency 'active_support'
13
+ s.add_dependency 'json'
14
+ s.add_dependency 'sprockets'
15
+
16
+ s.add_development_dependency 'jasmine'
17
+ s.add_development_dependency 'rspec'
18
+ s.add_development_dependency 'settingslogic'
19
+ s.add_development_dependency 'shoulda'
20
+ end
@@ -0,0 +1,9 @@
1
+ key1:
2
+ sub_key1: sub_value1
3
+ key2:
4
+ sub_key2_1: sub_value2_1
5
+ sub_key2_2:
6
+ sub_key2_2_1: sub_value2_2_1
7
+ sub_key2_2_2:
8
+ sub_key2_2_2_1: sub_value2_2_2_1
9
+ key3: value3
File without changes
@@ -0,0 +1,13 @@
1
+ describe('Settings ', function() {
2
+ it('Settings object contains a valid json', function() {
3
+ expected_json = {
4
+ sub_key1 : 'sub_value1',
5
+ sub_key2_2_1 : 'sub_value2_2_1',
6
+ sub_key2_2_2: {
7
+ sub_key2_2_2_1: 'sub_value2_2_2_1'
8
+ }
9
+ };
10
+
11
+ expect(Settings).toEqual(expected_json);
12
+ });
13
+ });
@@ -0,0 +1,73 @@
1
+ # src_files
2
+ #
3
+ # Return an array of filepaths relative to src_dir to include before jasmine specs.
4
+ # Default: []
5
+ #
6
+ # EXAMPLE:
7
+ #
8
+ # src_files:
9
+ # - lib/source1.js
10
+ # - lib/source2.js
11
+ # - dist/**/*.js
12
+ #
13
+ src_files:
14
+ - spec/javascripts/generated/**/*.js.erb
15
+
16
+ # stylesheets
17
+ #
18
+ # Return an array of stylesheet filepaths relative to src_dir to include before jasmine specs.
19
+ # Default: []
20
+ #
21
+ # EXAMPLE:
22
+ #
23
+ # stylesheets:
24
+ # - css/style.css
25
+ # - stylesheets/*.css
26
+ #
27
+ stylesheets:
28
+
29
+ # helpers
30
+ #
31
+ # Return an array of filepaths relative to spec_dir to include before jasmine specs.
32
+ # Default: ["helpers/**/*.js"]
33
+ #
34
+ # EXAMPLE:
35
+ #
36
+ # helpers:
37
+ # - helpers/**/*.js
38
+ #
39
+ helpers:
40
+
41
+ # spec_files
42
+ #
43
+ # Return an array of filepaths relative to spec_dir to include.
44
+ # Default: ["**/*[sS]pec.js"]
45
+ #
46
+ # EXAMPLE:
47
+ #
48
+ # spec_files:
49
+ # - **/*[sS]pec.js
50
+ #
51
+ spec_files:
52
+
53
+ # src_dir
54
+ #
55
+ # Source directory path. Your src_files must be returned relative to this path. Will use root if left blank.
56
+ # Default: project root
57
+ #
58
+ # EXAMPLE:
59
+ #
60
+ # src_dir: public
61
+ #
62
+ src_dir:
63
+
64
+ # spec_dir
65
+ #
66
+ # Spec directory path. Your spec_files must be returned relative to this path.
67
+ # Default: spec/javascripts
68
+ #
69
+ # EXAMPLE:
70
+ #
71
+ # spec_dir: spec/javascripts
72
+ #
73
+ spec_dir:
@@ -0,0 +1,57 @@
1
+ Spec_root = File.expand_path(File.join('..', '..', '..'), __FILE__)
2
+
3
+ $LOAD_PATH << File.join(Spec_root, '..', 'lib')
4
+ require 'settingslogic'
5
+ require 'settings_js'
6
+ require 'sprockets'
7
+
8
+ module Jasmine
9
+
10
+ class DummySettings < Settingslogic
11
+ source File.join(Spec_root, 'fixtures', 'settings_js', 'adapters', 'settings_logic.yml')
12
+ end
13
+
14
+
15
+ class Config
16
+
17
+ def js_files(spec_filter = nil)
18
+ gem_configuration
19
+
20
+ precompile_app_assets
21
+
22
+ # this is code from the original jasmine config js_files method - you could also just alias_method_chain it
23
+ spec_files_to_include = spec_filter.nil? ? spec_files : match_files(spec_dir, [spec_filter])
24
+ src_files.collect { |f| '/' + f } + helpers.collect { |f| File.join(spec_path, f) } + spec_files_to_include.collect { |f| File.join(spec_path, f) }
25
+ end
26
+
27
+ private
28
+
29
+ def gem_configuration
30
+ SettingsJs.configuration do |config|
31
+ config.adapter = 'settings_logic'
32
+ config.keys = %w(key1 key2.sub_key2_2)
33
+ config.klass = DummySettings
34
+ end
35
+ end
36
+
37
+ def precompile_app_assets
38
+ puts 'Precompiling assets...'
39
+
40
+ generated_files_directory = File.expand_path(File.join('..', '..', 'generated'), __FILE__)
41
+
42
+ sprockets = Sprockets::Environment.new(File.join(Spec_root, '..', 'app', 'assets', 'javascripts'))
43
+ sprockets.append_path('settings-js')
44
+
45
+ assets = sprockets.find_asset('settings.js')
46
+ prefix, basename = assets.pathname.to_s.split('/')[-2..-1]
47
+
48
+ FileUtils.mkpath(File.join(generated_files_directory))
49
+
50
+ assets.write_to(File.join(generated_files_directory, basename))
51
+ assets.to_a.each do |asset|
52
+ realname = asset.pathname.basename.to_s.split('.')[0..1].join('.')
53
+ assets.write_to(File.join(generated_files_directory, basename))
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+ require 'settingslogic'
3
+ require 'settings_js/adapters/settings_logic'
4
+
5
+ describe 'SettingsJs::Adapters::SettingsLogic' do
6
+
7
+ let(:settings_klass) do
8
+ Class.new(Settingslogic) do
9
+ source File.join(Spec_root, 'fixtures', 'settings_js', 'adapters', 'settings_logic.yml')
10
+ end
11
+ end
12
+
13
+ let(:adapter_instance) { SettingsJs::Adapters::SettingsLogic.new(settings_klass) }
14
+
15
+ it { adapter_instance.should have_attr_accessor(:klass) }
16
+
17
+ describe '#to_hash' do
18
+ it 'returns subkeys when base key is in settings root' do
19
+ adapter_instance.to_hash('key1').should == { 'sub_key1' => 'sub_value1' }
20
+ end
21
+
22
+ it 'returns subkeys when base key is under the settings root' do
23
+ expected_hash = {
24
+ 'sub_key2_2_1' => 'sub_value2_2_1' ,
25
+ 'sub_key2_2_2' => {
26
+ 'sub_key2_2_2_1' => 'sub_value2_2_2_1'
27
+ }
28
+ }
29
+
30
+ adapter_instance.to_hash('key2.sub_key2_2').should == expected_hash
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'SettingsJs::Config' do
4
+ let(:config) { SettingsJs::Config.instance }
5
+
6
+ it { config.should have_attr_accessor :adapter }
7
+ it { config.should have_attr_accessor :keys }
8
+ it { config.should have_attr_accessor :klass }
9
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ describe SettingsJs::Merger do
4
+
5
+ let(:merger) { SettingsJs::Merger.new }
6
+
7
+ describe '#to_hash' do
8
+
9
+ context 'when config.keys does not respond to "#reduce"' do
10
+
11
+ it 'raises an TypeError' do
12
+ SettingsJs.configuration { |config| config.keys = mock(:not_reduceable_object) }
13
+ expect { merger.to_hash }.to raise_error(TypeError)
14
+ end
15
+ end
16
+
17
+ context 'when config.keys respond to "#reduce"' do
18
+ let(:reduceable_object) do
19
+ reduceable_object = mock(:reduceable_object)
20
+ reduceable_object.stub(:reduce)
21
+ reduceable_object
22
+ end
23
+
24
+ it 'does not raise a TypeError' do
25
+ SettingsJs.configuration { |config| config.keys = reduceable_object }
26
+ expect { merger.to_hash }.to_not raise_error(TypeError)
27
+ end
28
+ end
29
+
30
+ it 'returns a hash which merge all keys hash response' do
31
+ adapter = mock(:adapter)
32
+ adapter.stub(:to_hash).and_return({ key1: 'value1', key2: 'value2' }, { key2: { key2_1: 'value2_1', key2_2: 'value2_2' }})
33
+
34
+ config = mock(:config)
35
+ config.stub(:keys).and_return(%w(key1 key2))
36
+
37
+ merger.stub(:config).and_return(config)
38
+ merger.stub(:adapter).and_return(adapter)
39
+
40
+ expected_hash = {
41
+ key1: 'value1',
42
+ key2: {
43
+ key2_1: 'value2_1',
44
+ key2_2: 'value2_2',
45
+ }
46
+ }
47
+
48
+ merger.to_hash.should == expected_hash
49
+ end
50
+ end
51
+
52
+ describe '#to_json' do
53
+ it 'calls #to_json method on the #to_hash result' do
54
+ hash_mocked = mock(:hash)
55
+ hash_mocked.stub(:to_json)
56
+ merger.stub(:to_hash).and_return(hash_mocked)
57
+
58
+ hash_mocked.should_receive(:to_json).once
59
+
60
+ merger.to_json
61
+ end
62
+ end
63
+
64
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe SettingsJs do
4
+
5
+ describe '.configuration' do
6
+ it 'calls block with configuration object in parameter' do
7
+ expect { |config| SettingsJs.configuration(&config) }.to yield_with_args(SettingsJs::Config.instance)
8
+ end
9
+ end
10
+
11
+ end
@@ -0,0 +1,5 @@
1
+ Spec_root = File.expand_path('..', __FILE__)
2
+
3
+ require File.join(Spec_root, '..', 'lib', 'settings_js')
4
+
5
+ Dir[File.join(Spec_root, 'support', '**', '*.rb')].each { |support_helper| require support_helper }
@@ -0,0 +1,5 @@
1
+ RSpec::Matchers.define :have_attr_accessor do |attribute_name|
2
+ match do |subject|
3
+ subject.respond_to?(attribute_name) && subject.respond_to?("#{attribute_name}=")
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,189 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: settings_js
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kevin Disneur
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: active_support
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: json
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: sprockets
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: jasmine
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rspec
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: settingslogic
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: shoulda
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ description:
127
+ email: kevin.disneur@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - .gitignore
133
+ - .rvmrc
134
+ - Gemfile
135
+ - Gemfile.lock
136
+ - Rakefile
137
+ - app/assets/javascripts/settings-js/settings.js.erb
138
+ - lib/settings_js.rb
139
+ - lib/settings_js/adapters/settings_logic.rb
140
+ - lib/settings_js/config.rb
141
+ - lib/settings_js/merger.rb
142
+ - settings_js.gemspec
143
+ - spec/fixtures/settings_js/adapters/settings_logic.yml
144
+ - spec/javascripts/helpers/SpecHelper.js
145
+ - spec/javascripts/settings-js/settings_js_spec.js
146
+ - spec/javascripts/support/jasmine.yml
147
+ - spec/javascripts/support/jasmine_config.rb
148
+ - spec/settings_js/adapters/settings_js_spec.rb
149
+ - spec/settings_js/config_spec.rb
150
+ - spec/settings_js/merger_spec.rb
151
+ - spec/settings_js_spec.rb
152
+ - spec/spec_helper.rb
153
+ - spec/support/have_attr_accessor.rb
154
+ homepage: http://rubygems.org/gems/settings_js
155
+ licenses: []
156
+ post_install_message:
157
+ rdoc_options: []
158
+ require_paths:
159
+ - lib
160
+ required_ruby_version: !ruby/object:Gem::Requirement
161
+ none: false
162
+ requirements:
163
+ - - ! '>='
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ required_rubygems_version: !ruby/object:Gem::Requirement
167
+ none: false
168
+ requirements:
169
+ - - ! '>='
170
+ - !ruby/object:Gem::Version
171
+ version: '0'
172
+ requirements: []
173
+ rubyforge_project:
174
+ rubygems_version: 1.8.24
175
+ signing_key:
176
+ specification_version: 3
177
+ summary: Sharing same application settings between ruby and javascript
178
+ test_files:
179
+ - spec/fixtures/settings_js/adapters/settings_logic.yml
180
+ - spec/javascripts/helpers/SpecHelper.js
181
+ - spec/javascripts/settings-js/settings_js_spec.js
182
+ - spec/javascripts/support/jasmine.yml
183
+ - spec/javascripts/support/jasmine_config.rb
184
+ - spec/settings_js/adapters/settings_js_spec.rb
185
+ - spec/settings_js/config_spec.rb
186
+ - spec/settings_js/merger_spec.rb
187
+ - spec/settings_js_spec.rb
188
+ - spec/spec_helper.rb
189
+ - spec/support/have_attr_accessor.rb