parsed 0.1.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.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +98 -0
- data/Rakefile +1 -0
- data/lib/parsed/parseable.rb +66 -0
- data/lib/parsed/version.rb +3 -0
- data/lib/parsed.rb +2 -0
- data/parsed.gemspec +27 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5bdb8dccd3c53ef25db2c5a9c87fbe2651c41828
|
4
|
+
data.tar.gz: fdd42fdf713581a79e140c0bfdbf512f65ba2654
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0f7062e41e1e0f2ece126223fff9b77fd9bb19f5ff97637626d7fcc6374fd8ff4ec39e86cbeb16a1744a9f92e3142e04d8e8ceae9c72e5ed008cef0f33db4124
|
7
|
+
data.tar.gz: e7a52e6b4ec6b17b4a644848f78e19a605ea6c17792ab730dd4dc7491ab3e005575cfa49caedae529bc8fc7d7b6e8582c1c4ecbadf21bb8b57d20115dcefe03c
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Robin Roestenburg
|
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,98 @@
|
|
1
|
+
# Parsed
|
2
|
+
|
3
|
+
Parses files into basic Ruby objects. Currently only the JSON format is
|
4
|
+
supported. Future versions are planned to support XML, Haml and TOML.
|
5
|
+
|
6
|
+
## Why
|
7
|
+
|
8
|
+
TODO: Write rationale behind gem.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
gem 'parsed'
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install parsed
|
23
|
+
|
24
|
+
## How do I use it?
|
25
|
+
|
26
|
+
Say, you have a JSON file containing football divisions and their teams:
|
27
|
+
|
28
|
+
``` json
|
29
|
+
{
|
30
|
+
"name": "Premier League",
|
31
|
+
"country": "England",
|
32
|
+
|
33
|
+
"teams": [
|
34
|
+
{
|
35
|
+
"name": "Arsenal",
|
36
|
+
"city": "London"
|
37
|
+
},
|
38
|
+
{
|
39
|
+
"name": "Swansea City",
|
40
|
+
"city": "Swansea"
|
41
|
+
}
|
42
|
+
]
|
43
|
+
}
|
44
|
+
```
|
45
|
+
|
46
|
+
You would like to use this data in your Ruby program to create a game.
|
47
|
+
|
48
|
+
In order to do this you have two basic models: `League` and `Team`. You declare
|
49
|
+
what attributes and collections need to be parsed on those models:
|
50
|
+
|
51
|
+
``` ruby
|
52
|
+
# file: league.rb
|
53
|
+
class League
|
54
|
+
include Parsed::Parseable
|
55
|
+
|
56
|
+
attr_accessor :name, :country, :teams
|
57
|
+
|
58
|
+
parses :name
|
59
|
+
parses :country
|
60
|
+
parses_collection :teams
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
# file: team.rb
|
65
|
+
class Team
|
66
|
+
include Parsed::Parseable
|
67
|
+
|
68
|
+
attr_accessor :name, :city
|
69
|
+
|
70
|
+
parses :name
|
71
|
+
parses :city
|
72
|
+
|
73
|
+
end
|
74
|
+
```
|
75
|
+
|
76
|
+
Finally, you parse the JSON file as follows:
|
77
|
+
|
78
|
+
``` ruby
|
79
|
+
require 'rubygems'
|
80
|
+
require 'parsed'
|
81
|
+
|
82
|
+
league = League.parse(File.read('premier_league.json'))
|
83
|
+
|
84
|
+
p league.name # => 'Premier League'
|
85
|
+
p league.teams # =>
|
86
|
+
|
87
|
+
arsenal = league.teams.first
|
88
|
+
|
89
|
+
p arsenal.name # => 'Arsenal'
|
90
|
+
```
|
91
|
+
|
92
|
+
## Contributing
|
93
|
+
|
94
|
+
1. Fork it
|
95
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
96
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
97
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
98
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'active_support/inflector'
|
3
|
+
|
4
|
+
module Parsed
|
5
|
+
|
6
|
+
module Parseable
|
7
|
+
|
8
|
+
def self.included(base)
|
9
|
+
base.instance_variable_set("@fields", [])
|
10
|
+
base.instance_variable_set("@collections", [])
|
11
|
+
base.instance_variable_set("@json", {})
|
12
|
+
|
13
|
+
base.extend ClassMethods
|
14
|
+
end
|
15
|
+
|
16
|
+
module ClassMethods
|
17
|
+
|
18
|
+
def parse(json)
|
19
|
+
@json = JSON.parse(json, { :symbolize_names => true })
|
20
|
+
|
21
|
+
instance = new
|
22
|
+
parse_instance(instance)
|
23
|
+
instance
|
24
|
+
end
|
25
|
+
|
26
|
+
def parses(type)
|
27
|
+
@fields << type
|
28
|
+
end
|
29
|
+
|
30
|
+
def parses_collection(type)
|
31
|
+
@collections << type
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def parse_instance(instance)
|
37
|
+
parse_fields(instance)
|
38
|
+
parse_collections(instance)
|
39
|
+
end
|
40
|
+
|
41
|
+
def parse_fields(instance)
|
42
|
+
@fields.each do |field|
|
43
|
+
value = @json[field.to_sym]
|
44
|
+
instance.send("#{field}=".to_sym, value)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def parse_collections(instance)
|
49
|
+
@collections.each do |collection|
|
50
|
+
|
51
|
+
clazz = Kernel.const_get(collection.to_s.singularize.camelize)
|
52
|
+
elements = @json[collection] || []
|
53
|
+
|
54
|
+
parsed_elements = elements.map do |element|
|
55
|
+
clazz.parse(element.to_json)
|
56
|
+
end
|
57
|
+
|
58
|
+
instance.send("#{collection}=".to_sym, parsed_elements)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
data/lib/parsed.rb
ADDED
data/parsed.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'parsed/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = 'parsed'
|
8
|
+
gem.version = Parsed::VERSION
|
9
|
+
gem.authors = ['Robin Roestenburg']
|
10
|
+
gem.email = ['robin@roestenburg.io']
|
11
|
+
gem.description = %q{Parses files into basic Ruby objects. Currently only
|
12
|
+
the JSON format is supported. Future versions are
|
13
|
+
planned to support XML, Haml and TOML.}
|
14
|
+
gem.summary = %q{Write a gem summary}
|
15
|
+
gem.homepage = 'https://github.com/robinroestenburg/parsed'
|
16
|
+
gem.license = 'MIT'
|
17
|
+
|
18
|
+
gem.files = `git ls-files`.split($/)
|
19
|
+
gem.executables = gem.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
20
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
21
|
+
gem.require_paths = ['lib']
|
22
|
+
|
23
|
+
gem.add_development_dependency 'bundler', '~> 1.3'
|
24
|
+
gem.add_development_dependency 'rake'
|
25
|
+
|
26
|
+
gem.add_runtime_dependency 'active_support'
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: parsed
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Robin Roestenburg
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-07-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: active_support
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: |-
|
56
|
+
Parses files into basic Ruby objects. Currently only
|
57
|
+
the JSON format is supported. Future versions are
|
58
|
+
planned to support XML, Haml and TOML.
|
59
|
+
email:
|
60
|
+
- robin@roestenburg.io
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- .gitignore
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- lib/parsed.rb
|
71
|
+
- lib/parsed/parseable.rb
|
72
|
+
- lib/parsed/version.rb
|
73
|
+
- parsed.gemspec
|
74
|
+
homepage: https://github.com/robinroestenburg/parsed
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.0.3
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: Write a gem summary
|
98
|
+
test_files: []
|