jsb3 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ rvm:
2
+ - 1.9.2
3
+ - 1.9.3
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## v0.0.1
2
+
3
+ * Initial release.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in jsb3.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Ryan Lovelett
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Jsb3 [![Build Status](https://secure.travis-ci.org/RLovelett/jsb3.png)](http://travis-ci.org/RLovelett/jsb3)
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'jsb3'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install jsb3
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "rspec/core/rake_task"
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task default: :spec
data/jsb3.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/jsb3/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Ryan Lovelett"]
6
+ gem.email = ["ryan@wahvee.com"]
7
+ gem.description = %q{Parses a Sencha JsBuilder configuration file.}
8
+ gem.summary = %q{http://extjs.com/products/jsbuilder/}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "jsb3"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Jsb3::VERSION
17
+
18
+ gem.add_dependency "yajl-ruby"
19
+ gem.add_development_dependency "rake"
20
+ gem.add_development_dependency "rspec"
21
+ end
data/lib/jsb3.rb ADDED
@@ -0,0 +1,147 @@
1
+ require 'yajl'
2
+ require "jsb3/version"
3
+
4
+ module Jsb3
5
+ class Packages
6
+ include Enumerable
7
+ attr_accessor :elements
8
+
9
+ def initialize(raw_package_array, resources)
10
+ @elements = []
11
+ raw_package_array.each do |raw_package|
12
+ @elements.push(Package.new(raw_package, resources))
13
+ end
14
+ end
15
+
16
+ def each(&block)
17
+ @elements.each do |element|
18
+ block.call(element)
19
+ end
20
+ end
21
+
22
+ def ids
23
+ @elements.map do |pkg|
24
+ pkg.id
25
+ end
26
+ end
27
+
28
+ def names
29
+ @elements.map do |pkg|
30
+ pkg.name
31
+ end
32
+ end
33
+ end
34
+
35
+ class Package
36
+ include Enumerable
37
+ attr_reader :id, :name, :target, :files
38
+
39
+ def initialize(raw_package_hash, resources)
40
+ @id = raw_package_hash['id']
41
+ @name = raw_package_hash['name']
42
+ @target = raw_package_hash['target']
43
+ @files = raw_package_hash['files'].map do |raw_file|
44
+ File.new raw_file, resources
45
+ end
46
+ end
47
+
48
+ def to_s
49
+ "#{@name} has #{@files.count} files."
50
+ end
51
+ end
52
+
53
+ class File
54
+ def initialize(raw_file_hash, resources)
55
+ path = raw_file_hash['path']
56
+ path = resources.apply(path)
57
+ @path = path
58
+ @name = raw_file_hash['name']
59
+ end
60
+
61
+ def dirname
62
+ ::File.dirname(to_path)
63
+ end
64
+
65
+ alias :path :dirname
66
+
67
+ def basename
68
+ ::File.basename(to_path)
69
+ end
70
+
71
+ alias :name :basename
72
+
73
+ def to_s
74
+ "#{dirname}/#{basename}"
75
+ end
76
+
77
+ def exists?
78
+ ::File.exists?(to_s)
79
+ end
80
+
81
+ def absolute_path
82
+ ::File.absolute_path(to_s)
83
+ end
84
+
85
+ private
86
+ def to_path
87
+ "#{@path}/#{@name}"
88
+ end
89
+ end
90
+
91
+ class Resources
92
+ include Enumerable
93
+
94
+ def initialize(raw_resources_array = Array.new)
95
+ @resources = raw_resources_array.map do |raw_resource|
96
+ Resource.new raw_resource
97
+ end
98
+ end
99
+
100
+ def each(&block)
101
+ @resources.each do |resource|
102
+ block.call(resource)
103
+ end
104
+ end
105
+
106
+ def apply(str)
107
+ resource = @resources.find do |resource|
108
+ str.to_s.include? resource.src
109
+ end
110
+ return str.sub(resource.src, resource.dest) unless resource.nil?
111
+ return str
112
+ end
113
+ end
114
+
115
+ class Resource
116
+ attr_reader :src, :dest
117
+ def initialize(raw_resource_hash)
118
+ @src = raw_resource_hash['src']
119
+ @dest = raw_resource_hash['dest']
120
+ end
121
+ end
122
+
123
+ class Jsb3
124
+ attr_reader :packages, :name, :license, :resources
125
+
126
+ def initialize(path)
127
+ json = ::File.new(path, 'r')
128
+ parser = Yajl::Parser.new
129
+ hash = parser.parse(json)
130
+ @name = hash.fetch("projectName")
131
+ @license = hash.fetch("licenseText")
132
+ @resources = Resources.new(hash.fetch("resources"))
133
+ @packages = Packages.new(hash.fetch("packages"), @resources)
134
+ end
135
+
136
+ def method_missing(sym, *args, &block)
137
+ package = @packages.find do |package|
138
+ package.id === sym.to_s
139
+ end
140
+ return package unless package.nil?
141
+ return super
142
+ end
143
+
144
+ def respond_to?(method_sym, include_private = false)
145
+ end
146
+ end
147
+ end
@@ -0,0 +1,3 @@
1
+ module Jsb3
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe Jsb3::File do
4
+ let(:jsb3_resource_file_1) { Jsb3::Jsb3.new("spec/sample_files/sdk.jsb3").one.files[0] }
5
+ let(:jsb3_resource_file_2) { Jsb3::Jsb3.new("spec/sample_files/sdk.jsb3").one.files[1] }
6
+ let(:jsb3_file) { Jsb3::Jsb3.new("spec/sample_files/sdk.jsb3").two.files.first }
7
+ context "resource used to change path" do
8
+ it { jsb3_resource_file_1.should be_a(Jsb3::File) }
9
+ it { jsb3_resource_file_1.name.should == "sample.js" }
10
+ it { jsb3_resource_file_1.basename.should == "sample.js" }
11
+ it { jsb3_resource_file_1.path.should == "spec/sample_files" }
12
+ it { jsb3_resource_file_1.dirname.should == "spec/sample_files" }
13
+ it { jsb3_resource_file_1.to_s.should_not == "../override/this/path/sample.js" }
14
+ it { jsb3_resource_file_1.to_s.should == "spec/sample_files/sample.js" }
15
+ it { jsb3_resource_file_1.absolute_path.should == File.absolute_path("spec/sample_files/sample.js") }
16
+ it { jsb3_resource_file_1.absolute_path.should == File.absolute_path("spec/sample_files/sample.js") }
17
+ it { jsb3_resource_file_1.exists?.should be_true }
18
+
19
+ it { jsb3_resource_file_2.should be_a(Jsb3::File) }
20
+ it { jsb3_resource_file_2.name.should == "another.js" }
21
+ it { jsb3_resource_file_2.basename.should == "another.js" }
22
+ it { jsb3_resource_file_2.path.should == "spec/sample_files/lang" }
23
+ it { jsb3_resource_file_2.dirname.should == "spec/sample_files/lang" }
24
+ it { jsb3_resource_file_2.to_s.should_not == "../override/this/path/lang/another.js" }
25
+ it { jsb3_resource_file_2.to_s.should == "spec/sample_files/lang/another.js" }
26
+ it { jsb3_resource_file_2.absolute_path.should_not == File.absolute_path("../override/this/path/lang/another.js") }
27
+ it { jsb3_resource_file_2.absolute_path.should == File.absolute_path("spec/sample_files/lang/another.js") }
28
+ it { jsb3_resource_file_2.exists?.should be_true }
29
+ end
30
+ context "no resource matching to replace path" do
31
+ it { jsb3_file.should be_a(Jsb3::File) }
32
+ it { jsb3_file.name.should == "sample.js" }
33
+ it { jsb3_file.basename.should == "sample.js" }
34
+ it { jsb3_file.path.should == "spec/sample_files" }
35
+ it { jsb3_file.dirname.should == "spec/sample_files" }
36
+ it { jsb3_file.to_s.should == "spec/sample_files/sample.js" }
37
+ it { jsb3_file.absolute_path.should == File.absolute_path("spec/sample_files/sample.js") }
38
+ it { jsb3_file.exists?.should be_true }
39
+ end
40
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe Jsb3 do
4
+ context 'missing file' do
5
+ it "should raise" do
6
+ expect { Jsb3::Jsb3.new("spec/sample_files/sdk.jsb2") }.to raise_error(Exception)
7
+ end
8
+ end
9
+ context 'valid file' do
10
+ let(:jsb3) { Jsb3::Jsb3.new("spec/sample_files/sdk.jsb3") }
11
+ it { jsb3.name.should == "Ryan's Sample" }
12
+ it { jsb3.license.should == "MIT" }
13
+ it { jsb3.one.should be_a(Jsb3::Package) }
14
+ it { jsb3.two.should be_a(Jsb3::Package) }
15
+ end
16
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe Jsb3::Package do
4
+ let(:jsb3) { Jsb3::Jsb3.new("spec/sample_files/sdk.jsb3") }
5
+ it { jsb3.one.should be_a(Jsb3::Package) }
6
+ it { jsb3.one.id.should == "one" }
7
+ it { jsb3.one.name.should == "One" }
8
+ it { jsb3.one.target.should == "this/cool-guy.js" }
9
+ it { jsb3.one.files.should have(2).items }
10
+ it { jsb3.one.files.first.should be_a(Jsb3::File) }
11
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe Jsb3::Packages do
4
+ let(:jsb3) { Jsb3::Jsb3.new("spec/sample_files/sdk.jsb3") }
5
+ it { jsb3.packages.should have(2).items }
6
+ it { jsb3.packages.ids.should have(2).items }
7
+ it { jsb3.packages.names.should have(2).items }
8
+ it { jsb3.packages.ids.should == ['one', 'two'] }
9
+ it { jsb3.packages.names.should == ['One', 'Two'] }
10
+ it { jsb3.packages.should be_a(Jsb3::Packages) }
11
+ it { jsb3.packages.first.should be_a(Jsb3::Package) }
12
+ end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+
3
+ describe Jsb3::Resource do
4
+ let(:jsb3) { Jsb3::Jsb3.new("spec/sample_files/sdk.jsb3") }
5
+ it { jsb3.resources.first.should be_a(Jsb3::Resource) }
6
+ it { jsb3.resources.first.src.should == "../override/this/path/" }
7
+ it { jsb3.resources.first.dest.should == "spec/sample_files/" }
8
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe Jsb3::Resources do
4
+ let(:jsb3) { Jsb3::Jsb3.new("spec/sample_files/sdk.jsb3") }
5
+ it { jsb3.resources.should be_a(Jsb3::Resources) }
6
+ it { jsb3.resources.should have(1).items }
7
+ it { jsb3.resources.first.should be_a(Jsb3::Resource) }
8
+
9
+ context "path correction" do
10
+ it { jsb3.resources.apply("test").should == "test" }
11
+ it { jsb3.resources.apply("../override/this/path/").should == "spec/sample_files/" }
12
+ it { jsb3.resources.apply("test/../override/this/path/").should == "test/spec/sample_files/" }
13
+ it { jsb3.resources.apply("test../override/this/path/").should == "testspec/sample_files/" }
14
+
15
+ it { jsb3.resources.apply("test/../override/this/path/test").should == "test/spec/sample_files/test" }
16
+ it { jsb3.resources.apply("test../override/this/path/test").should == "testspec/sample_files/test" }
17
+ it { jsb3.resources.apply("test/../override/this/path//test").should == "test/spec/sample_files//test" }
18
+ it { jsb3.resources.apply("test../override/this/path//test").should == "testspec/sample_files//test" }
19
+ end
20
+ end
File without changes
File without changes
File without changes
@@ -0,0 +1,42 @@
1
+ {
2
+ "projectName": "Ryan's Sample",
3
+ "licenseText": "MIT",
4
+ "packages": [
5
+ {
6
+ "name": "One",
7
+ "target": "this/cool-guy.js",
8
+ "id": "one",
9
+ "files": [
10
+ {
11
+ "path": "../override/this/path/",
12
+ "name": "sample.js"
13
+ },
14
+ {
15
+ "path": "../override/this/path/lang/",
16
+ "name": "another.js"
17
+ }
18
+ ]
19
+ },
20
+ {
21
+ "name": "Two",
22
+ "target": "this/is-not-a-cool-guy.js",
23
+ "id": "two",
24
+ "files": [
25
+ {
26
+ "path": "spec/sample_files/",
27
+ "name": "sample.js"
28
+ },
29
+ {
30
+ "path": "spec/sample_files/",
31
+ "name": "another.js"
32
+ }
33
+ ]
34
+ }
35
+ ],
36
+ "resources": [
37
+ {
38
+ "src": "../override/this/path/",
39
+ "dest": "spec/sample_files/"
40
+ }
41
+ ]
42
+ }
@@ -0,0 +1 @@
1
+ require 'jsb3'
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsb3
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ryan Lovelett
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: yajl-ruby
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: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
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: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
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
+ description: Parses a Sencha JsBuilder configuration file.
63
+ email:
64
+ - ryan@wahvee.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - .rspec
71
+ - .travis.yml
72
+ - CHANGELOG.md
73
+ - Gemfile
74
+ - LICENSE
75
+ - README.md
76
+ - Rakefile
77
+ - jsb3.gemspec
78
+ - lib/jsb3.rb
79
+ - lib/jsb3/version.rb
80
+ - spec/jsb3/file_spec.rb
81
+ - spec/jsb3/jsb3_spec.rb
82
+ - spec/jsb3/package_spec.rb
83
+ - spec/jsb3/packages_spec.rb
84
+ - spec/jsb3/resource_spec.rb
85
+ - spec/jsb3/resources_spec.rb
86
+ - spec/sample_files/another.js
87
+ - spec/sample_files/lang/another.js
88
+ - spec/sample_files/sample.js
89
+ - spec/sample_files/sdk.jsb3
90
+ - spec/spec_helper.rb
91
+ homepage: ''
92
+ licenses: []
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 1.8.24
112
+ signing_key:
113
+ specification_version: 3
114
+ summary: http://extjs.com/products/jsbuilder/
115
+ test_files:
116
+ - spec/jsb3/file_spec.rb
117
+ - spec/jsb3/jsb3_spec.rb
118
+ - spec/jsb3/package_spec.rb
119
+ - spec/jsb3/packages_spec.rb
120
+ - spec/jsb3/resource_spec.rb
121
+ - spec/jsb3/resources_spec.rb
122
+ - spec/sample_files/another.js
123
+ - spec/sample_files/lang/another.js
124
+ - spec/sample_files/sample.js
125
+ - spec/sample_files/sdk.jsb3
126
+ - spec/spec_helper.rb