code_string 0.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,23 @@
1
+ *~
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
19
+ *.bundle
20
+ *.so
21
+ *.o
22
+ *.a
23
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in code_string.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Brice Texier
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.rdoc ADDED
@@ -0,0 +1,34 @@
1
+ = CodeString
2
+
3
+ Class to manipulate code strings.
4
+
5
+ == Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'code_string'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install code_string
18
+
19
+ == Usage
20
+
21
+ $ "sum = 1 + 5".c
22
+ $ c = "sum = 1 + 5".c(:ruby)
23
+ $ puts c.to_formatted_s
24
+ # language: ruby
25
+ # encoding: US-ASCII
26
+ 1: sum = 1 + 5
27
+
28
+ == Contributing
29
+
30
+ 1. Fork it ( https://github.com/burisu/code_string/fork )
31
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
32
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
33
+ 4. Push to the branch (`git push origin my-new-feature`)
34
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+ Rake::TestTask.new(:test) do |test|
5
+ test.libs << 'lib' << 'test'
6
+ test.pattern = 'test/**/test_*.rb'
7
+ test.verbose = true
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'code_string'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "code_string"
8
+ spec.version = CodeString::VERSION
9
+ spec.authors = ["Brice Texier"]
10
+ spec.email = ["burisu@oneiros.fr"]
11
+ spec.summary = "String to manage codes"
12
+ spec.description = "Like AS::SafeBuffer, a way to manage code string safely for code generation"
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,82 @@
1
+ # CodeString is string with an indicator of the manipulated language
2
+ # It permits to secure concantenation
3
+ # Code strings simplifies code displaying
4
+ class CodeString < String
5
+
6
+ VERSION = "0.0.0"
7
+
8
+ class IncompatibleLanguage < StandardError
9
+ end
10
+
11
+ @@default_language = :ruby
12
+
13
+ class << self
14
+ attr_accessor :default_language
15
+ end
16
+
17
+ attr_reader :language
18
+
19
+ def initialize(text, language = nil)
20
+ @language = language || @@default_language
21
+ super(text)
22
+ end
23
+
24
+ def +(text)
25
+ if text.is_a?(CodeString)
26
+ if self.language == text.language
27
+ super text
28
+ else
29
+ raise IncompatibleLanguage, "Language #{self.language} is not compatible with language: #{text.language}"
30
+ end
31
+ else
32
+ super text
33
+ end
34
+ end
35
+
36
+ def <<(text)
37
+ if text.is_a?(CodeString)
38
+ if self.language == text.language
39
+ super text
40
+ else
41
+ raise IncompatibleLanguage, "Language #{self.language} is not compatible with language: #{text.language}"
42
+ end
43
+ else
44
+ super text.to_s
45
+ end
46
+ end
47
+
48
+ def to_formatted_s
49
+ string, index = "", 1
50
+ string << "# language: #{language}\n"
51
+ string << "# encoding: #{encoding}\n"
52
+ for line in self.split(/\n/)
53
+ string << index.to_s.rjust(4) + ": " + line + "\n"
54
+ index += 1
55
+ end
56
+ return string
57
+ end
58
+
59
+ def inspect
60
+ self.to_s
61
+ end
62
+
63
+ def dig(*args)
64
+ options = args.last.is_a?(Hash) ? args.delete_at(-1) : {}
65
+ depth = args.shift || options[:depth] || 1
66
+ spacer = args.shift || options[:spacer] || " "
67
+ return (self.strip.gsub(/^/, spacer * depth) + "\n").c(@language)
68
+ end
69
+
70
+ end
71
+
72
+
73
+ class ::String
74
+ # Convert a String to a Code fragment
75
+ def to_code(language = nil)
76
+ CodeString.new(self, language)
77
+ end
78
+
79
+ def c(language = nil)
80
+ CodeString.new(self, language)
81
+ end
82
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,9 @@
1
+ # require 'coveralls'
2
+ # Coveralls.wear!
3
+
4
+ require 'bundler/setup'
5
+ require 'test/unit'
6
+
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
9
+ require 'code_string'
@@ -0,0 +1,25 @@
1
+ require 'helper'
2
+
3
+ class TestConcatenation < Test::Unit::TestCase
4
+
5
+ def test_valid_concatenation
6
+ assert_nothing_raised do
7
+ "5 + 3\n".c + "5 + 3".c
8
+ end
9
+
10
+ assert_nothing_raised do
11
+ "5 + 3\n".c + "5 + 3".c(:ruby)
12
+ end
13
+ end
14
+
15
+ def test_invalid_concatenation
16
+ assert_raise CodeString::IncompatibleLanguage do
17
+ "5 + 3\n".c + "5 + 3".c(:javascript)
18
+ end
19
+
20
+ assert_raise CodeString::IncompatibleLanguage do
21
+ "5 + 3\n".c(:c) + "5 + 3".c
22
+ end
23
+ end
24
+
25
+ end
@@ -0,0 +1,21 @@
1
+ require 'helper'
2
+
3
+ class TestInstanciation < Test::Unit::TestCase
4
+
5
+ def test_instanciation_from_string
6
+ assert_nothing_raised do
7
+ "5 + 3".c
8
+ end
9
+ end
10
+
11
+ def test_classical_instanciation
12
+ cs = nil
13
+
14
+ assert_nothing_raised do
15
+ cs = CodeString.new("5 + 3")
16
+ end
17
+
18
+ assert_equal :ruby, cs.language
19
+ end
20
+
21
+ end
@@ -0,0 +1,23 @@
1
+ require 'helper'
2
+
3
+ class TestOutput < Test::Unit::TestCase
4
+
5
+ def test_inspect_output
6
+ assert_nothing_raised do
7
+ "5 + 3".c.inspect
8
+ end
9
+ end
10
+
11
+ def test_string_output
12
+ assert_nothing_raised do
13
+ "5 + 3".c.to_s
14
+ end
15
+ end
16
+
17
+ def test_formatted_string_output
18
+ assert_nothing_raised do
19
+ "5 + 3\nreturn 0".c.to_formatted_s
20
+ end
21
+ end
22
+
23
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: code_string
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brice Texier
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-05-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.6'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.6'
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
+ description: Like AS::SafeBuffer, a way to manage code string safely for code generation
47
+ email:
48
+ - burisu@oneiros.fr
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.rdoc
57
+ - Rakefile
58
+ - code_string.gemspec
59
+ - lib/code_string.rb
60
+ - test/helper.rb
61
+ - test/test_concatenation.rb
62
+ - test/test_instanciation.rb
63
+ - test/test_output.rb
64
+ homepage: ''
65
+ licenses:
66
+ - MIT
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 1.8.23.2
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: String to manage codes
89
+ test_files:
90
+ - test/helper.rb
91
+ - test/test_concatenation.rb
92
+ - test/test_instanciation.rb
93
+ - test/test_output.rb