DSON 0.1.0 → 0.2.0

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/.gitignore CHANGED
@@ -1,22 +1,22 @@
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
- *.bundle
19
- *.so
20
- *.o
21
- *.a
22
- mkmf.log
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
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - 2.1.1
@@ -1,25 +1,29 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'DSON/version'
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = "DSON"
8
- spec.version = DSON::VERSION
9
- spec.authors = ["ishakir"]
10
- spec.email = ["imran.pshakir@gmail.com"]
11
- spec.summary = %q{A pure-ruby DSON Serializer}
12
- spec.homepage = ""
13
- spec.license = "MIT"
14
-
15
- spec.files = `git ls-files -z`.split("\x0")
16
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
- spec.require_paths = ["lib"]
19
-
20
- spec.add_development_dependency "bundler", "~> 1.6"
21
- spec.add_development_dependency "rake"
22
- spec.add_development_dependency "rspec"
23
- spec.add_development_dependency "rubocop"
24
- spec.add_development_dependency "magic_encoding"
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'DSON/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "DSON"
8
+ spec.version = DSON::VERSION
9
+ spec.authors = ["ishakir"]
10
+ spec.email = ["imran.pshakir@gmail.com"]
11
+ spec.summary = %q{A pure-ruby DSON Parser and Serializer}
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.required_ruby_version = '>= 1.9.2'
21
+
22
+ spec.add_dependency "radix", "~> 2.2"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.6"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ spec.add_development_dependency "rubocop"
28
+ spec.add_development_dependency "magic_encoding"
25
29
  end
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in DSON.gemspec
4
- gemspec
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in DSON.gemspec
4
+ gemspec
@@ -1,22 +1,22 @@
1
- Copyright (c) 2014 ishakir
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.
1
+ Copyright (c) 2014 ishakir
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 CHANGED
@@ -1,62 +1,61 @@
1
- # DSON
2
-
3
- Such serialization now also parsing! Totally pure-ruby also completely DSON. Wow!
4
-
5
- Currently known deficiencies:
6
- * Number handling (needs to be octal, currently treated as string)
7
-
8
- ## Installation
9
-
10
- Add this line to your application's Gemfile:
11
-
12
- gem 'DSON'
13
-
14
- And then execute:
15
-
16
- $ bundle
17
-
18
- Or install it yourself as:
19
-
20
- $ gem install DSON
21
-
22
- ## Usage
23
-
24
- *Serialization*
25
-
26
- Currently supports a ruby object, hash and array data structure and outputs it's representation in DSON ([Doge Serialized Object notation](http://dogeon.org/)). By way of an example, try:
27
-
28
- require 'DSON'
29
-
30
- puts DSON.such_serialize_wow({
31
- ruby: 'pure',
32
- supports: [
33
- 'hash',
34
- 'array'
35
- ]
36
- })
37
-
38
- This should output:
39
-
40
- such "ruby" is "pure", "supports" is so "hash" also "array" many wow
41
-
42
- Correct to the DSON spec.
43
-
44
- Try it out with custom ruby objects too!
45
-
46
- *Parsing*
47
-
48
- Surrently should parse a DSON object given as a string. Not the world's best put together parse but should function reasonably with general DSON strings. Try
49
-
50
- require 'DSON'
51
-
52
- puts DSON.so_parse(
53
- 'such "ruby" is "pure", "supports" is so "hash" also "array" many wow'
54
- )
55
-
56
- ## Contributing
57
-
58
- 1. Fork it ( https://github.com/[my-github-username]/DSON/fork )
59
- 2. Create your feature branch (`git checkout -b my-new-feature`)
60
- 3. Commit your changes (`git commit -am 'Add some feature'`)
61
- 4. Push to the branch (`git push origin my-new-feature`)
62
- 5. Create a new Pull Request
1
+ [![Build Status](https://travis-ci.org/ishakir/DSON.svg)](https://travis-ci.org/ishakir/DSON)
2
+ [![Code Climate](https://codeclimate.com/github/ishakir/DSON.png)](https://codeclimate.com/github/ishakir/DSON)
3
+ # DSON
4
+
5
+ Such serialization now also parsing! Totally pure-ruby also completely DSON. Wow!
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'DSON'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install DSON
20
+
21
+ ## Usage
22
+
23
+ **Serialization**
24
+
25
+ Currently supports a ruby object, hash and array data structure and outputs it's representation in DSON ([Doge Serialized Object notation](http://dogeon.org/)). By way of an example, try:
26
+
27
+ require 'DSON'
28
+
29
+ puts DSON.such_serialize_wow({
30
+ ruby: 'pure',
31
+ supports: [
32
+ 'hash',
33
+ 'array'
34
+ ]
35
+ })
36
+
37
+ This should output:
38
+
39
+ such "ruby" is "pure", "supports" is so "hash" also "array" many wow
40
+
41
+ Correct to the DSON spec.
42
+
43
+ Try it out with custom ruby objects too!
44
+
45
+ **Parsing**
46
+
47
+ Currently should parse a DSON object given as a string. Not the world's best put together parser but should function reasonably with general DSON strings. Try:
48
+
49
+ require 'DSON'
50
+
51
+ puts DSON.so_parse(
52
+ 'such "ruby" is "pure", "supports" is so "hash" also "array" many wow'
53
+ )
54
+
55
+ ## Contributing
56
+
57
+ 1. Fork it ( https://github.com/[my-github-username]/DSON/fork )
58
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
59
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
60
+ 4. Push to the branch (`git push origin my-new-feature`)
61
+ 5. Create a new Pull Request
data/Rakefile CHANGED
@@ -1,2 +1,6 @@
1
- require "bundler/gem_tasks"
2
-
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -1,13 +1,14 @@
1
- # -*- encoding : utf-8 -*-
2
- require 'DSON/version'
3
- require 'DSON/value'
4
-
5
- module DSON
6
- def self.such_serialize_wow(subject)
7
- DSON::Value.new(subject).such_serialize_wow
8
- end
9
-
10
- def self.so_parse(string)
11
- DSON::Value.so_parse(string)
12
- end
13
- end
1
+ # -*- encoding : utf-8 -*-
2
+ require 'radix'
3
+ require 'DSON/version'
4
+ require 'DSON/value'
5
+
6
+ module DSON
7
+ def self.such_serialize_wow(subject)
8
+ DSON::Value.new(subject).such_serialize_wow
9
+ end
10
+
11
+ def self.so_parse(string)
12
+ DSON::Value.so_parse(string)
13
+ end
14
+ end
@@ -1,98 +1,94 @@
1
- # -*- encoding : utf-8 -*-
2
- # -*- encoding : utf-8 -*-
3
- require 'DSON/value/hash_value'
4
- require 'DSON/value/array_value'
5
- require 'DSON/value/string_value'
6
- require 'DSON/value/nil_value'
7
- require 'DSON/value/true_value'
8
- require 'DSON/value/false_value'
9
- require 'DSON/value/numeric_value'
10
- require 'DSON/value/object_value'
11
-
12
- module DSON
13
- module Value
14
- SPACE = %q( )
15
-
16
- def self.new(value)
17
- return HashValue.new(value) if value.respond_to? :keys
18
- return ArrayValue.new(value) if value.respond_to? :each
19
- return NilValue.instance if value.nil?
20
- return TrueValue.instance if value.is_a? TrueClass
21
- return FalseValue.instance if value.is_a? FalseClass
22
- # return NumericValue.new(value) if value.is_a? Fixnum
23
- return StringValue.new(value) if value.is_a? String
24
- ObjectValue.new(value)
25
- end
26
-
27
- def self.so_parse(dson_string)
28
- string_hash, replaced_string = remove_all_strings(dson_string)
29
- handle_next(
30
- word_array: replaced_string.gsub(/,|\?|!|\./, ' ,').split(' '),
31
- string_hash: string_hash
32
- )
33
- end
34
-
35
- def self.handle_next(options)
36
- word_array = options[:word_array]
37
-
38
- fail 'An error has occurred, this could be either user error or a bug. Please check your DSON is valid, and if it is, please raise a GitHub issue' if word_array.empty?
39
-
40
- first_word = word_array.shift
41
-
42
- if first_word == 'such'
43
- return HashValue.so_parse(
44
- word_array: word_array,
45
- parent_hash: {},
46
- string_hash: options[:string_hash]
47
- )
48
- end
49
- if first_word == 'so'
50
- return ArrayValue.so_parse(
51
- word_array: word_array,
52
- parent_array: [],
53
- string_hash: options[:string_hash]
54
- )
55
- end
56
- return TrueValue.so_parse if first_word == 'yes'
57
- return FalseValue.so_parse if first_word == 'no'
58
- return NilValue.so_parse if first_word == 'empty'
59
-
60
- options[:first_word] = first_word
61
- StringValue.so_parse(options)
62
- end
63
-
64
- # Class methods can't be accessed from subclasses if protected...
65
- # Find better way if possible
66
- def self.remove_first_and_last_words(string)
67
- non_whitespace_elements = string.split(' ')
68
- non_whitespace_elements.pop
69
- non_whitespace_elements.shift
70
- non_whitespace_elements.join(' ')
71
- end
72
-
73
- def self.remove_first_and_last_elements(array)
74
- array.pop
75
- array.shift
76
- end
77
-
78
- protected
79
-
80
- def self.remove_all_strings(dson_string)
81
- string_hash = {}
82
- replaced_string = dson_string.gsub(/"(.*?)"/).with_index do |match, index|
83
- string_hash[index] = match[1..-2]
84
- index
85
- end
86
- [string_hash, replaced_string]
87
- end
88
-
89
- def reduce(list, potential_joiners)
90
- list.each_with_index.reduce('') do |acc, (element, index)|
91
- is_last = (index == list.size - 1)
92
-
93
- formed_string = is_last ? element : element + potential_joiners.sample
94
- acc + formed_string + SPACE
95
- end
96
- end
97
- end
98
- end
1
+ # -*- encoding : utf-8 -*-
2
+ # -*- encoding : utf-8 -*-
3
+ require 'DSON/value/hash_value'
4
+ require 'DSON/value/array_value'
5
+ require 'DSON/value/string_value'
6
+ require 'DSON/value/nil_value'
7
+ require 'DSON/value/true_value'
8
+ require 'DSON/value/false_value'
9
+ require 'DSON/value/numeric_value'
10
+ require 'DSON/value/object_value'
11
+
12
+ module DSON
13
+ module Value
14
+ SPACE = %q( )
15
+
16
+ def self.new(value)
17
+ return HashValue.new(value) if value.respond_to? :keys
18
+ return ArrayValue.new(value) if value.respond_to? :each
19
+ return NilValue.instance if value.nil?
20
+ return TrueValue.instance if value.is_a? TrueClass
21
+ return FalseValue.instance if value.is_a? FalseClass
22
+ return NumericValue.new(value) if value.is_a? Integer or value.is_a? Float
23
+ return StringValue.new(value) if value.is_a? String
24
+ ObjectValue.new(value)
25
+ end
26
+
27
+ def self.so_parse(dson_string)
28
+ handle_next(
29
+ word_array: dson_string.scan(/(?:"(?:\\.|[^"])*"|[^" ,!\?])+|\.(?!\d)+/),
30
+ )
31
+ end
32
+
33
+ def self.handle_next(options)
34
+ word_array = options[:word_array]
35
+
36
+ fail 'An error has occurred, this could be either user error or a bug. Please check your DSON is valid, and if it is, please raise a GitHub issue' if word_array.empty?
37
+
38
+ first_word = word_array.shift
39
+
40
+ if first_word == 'such'
41
+ return HashValue.so_parse(
42
+ word_array: word_array,
43
+ parent_hash: {}
44
+ )
45
+ end
46
+ if first_word == 'so'
47
+ return ArrayValue.so_parse(
48
+ word_array: word_array,
49
+ parent_array: []
50
+ )
51
+ end
52
+ return TrueValue.so_parse if first_word == 'yes'
53
+ return FalseValue.so_parse if first_word == 'no'
54
+ return NilValue.so_parse if first_word == 'empty'
55
+ return NumericValue.so_parse(first_word) unless first_word.start_with? '"'
56
+
57
+ StringValue.so_parse(first_word)
58
+ end
59
+
60
+ # Class methods can't be accessed from subclasses if protected...
61
+ # Find better way if possible
62
+ def self.remove_first_and_last_words(string)
63
+ non_whitespace_elements = string.split(' ')
64
+ non_whitespace_elements.pop
65
+ non_whitespace_elements.shift
66
+ non_whitespace_elements.join(' ')
67
+ end
68
+
69
+ def self.remove_first_and_last_elements(array)
70
+ array.pop
71
+ array.shift
72
+ end
73
+
74
+ protected
75
+
76
+ def self.remove_all_strings(dson_string)
77
+ string_hash = {}
78
+ replaced_string = dson_string.gsub(/"(.*?)"/).with_index do |match, index|
79
+ string_hash[index] = match[1..-2]
80
+ index
81
+ end
82
+ [string_hash, replaced_string]
83
+ end
84
+
85
+ def reduce(list, potential_joiners)
86
+ list.each_with_index.reduce('') do |acc, (element, index)|
87
+ is_last = (index == list.size - 1)
88
+
89
+ formed_string = is_last ? element : element + potential_joiners.sample
90
+ acc + formed_string + SPACE
91
+ end
92
+ end
93
+ end
94
+ end