parsed 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +2 -0
- data/README.md +11 -15
- data/lib/parsed/parseable.rb +53 -30
- data/lib/parsed/version.rb +1 -1
- data/parsed.gemspec +1 -0
- data/spec/parsing_regular_classes_spec.rb +104 -0
- data/spec/spec_helper.rb +16 -0
- metadata +22 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e649af05fed7637c624567a995d7d42065f94037
|
4
|
+
data.tar.gz: 835ea170f8bcefb50a2504a3dbd0c92844584ad2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8311ecf1b5363788ea33545a48df968cc2fbc52b79c437396361b306c951730989e949ba284fdf90fab9b12b8953e5eb2039927f64e057baf0ce03bed3ce4c8e
|
7
|
+
data.tar.gz: 99bbd5c32f951b0ff7123dbe446e6c2c1168de4b64bd00343356e29b9d03e2b5f842fa3cfd265a9cfd5c731c438e13594f850e42519d7872f375a37a8501d7f5
|
data/.rspec
ADDED
data/README.md
CHANGED
@@ -52,24 +52,15 @@ what attributes and collections need to be parsed on those models:
|
|
52
52
|
# file: league.rb
|
53
53
|
class League
|
54
54
|
include Parsed::Parseable
|
55
|
-
|
56
55
|
attr_accessor :name, :country, :teams
|
57
|
-
|
58
|
-
parses :name
|
59
|
-
parses :country
|
60
|
-
parses_collection :teams
|
61
|
-
|
56
|
+
parses :name, :country, :teams
|
62
57
|
end
|
63
58
|
|
64
59
|
# file: team.rb
|
65
60
|
class Team
|
66
61
|
include Parsed::Parseable
|
67
|
-
|
68
62
|
attr_accessor :name, :city
|
69
|
-
|
70
|
-
parses :name
|
71
|
-
parses :city
|
72
|
-
|
63
|
+
parses :name, :city
|
73
64
|
end
|
74
65
|
```
|
75
66
|
|
@@ -78,15 +69,20 @@ Finally, you parse the JSON file as follows:
|
|
78
69
|
``` ruby
|
79
70
|
require 'rubygems'
|
80
71
|
require 'parsed'
|
72
|
+
require_relative 'league'
|
73
|
+
require_relative 'team'
|
81
74
|
|
82
75
|
league = League.parse(File.read('premier_league.json'))
|
83
76
|
|
84
|
-
p league.name
|
85
|
-
|
77
|
+
p league.name
|
78
|
+
# => "Premier League"
|
86
79
|
|
87
|
-
|
80
|
+
p league.teams
|
81
|
+
# => [#<Team:0x007fcd18848428 @name="Arsenal", @city="London">,
|
82
|
+
# #<Team:0x007fcd188436d0 @name="Swansea City", @city="Swansea">]
|
88
83
|
|
89
|
-
p
|
84
|
+
p league.teams.first.name
|
85
|
+
# => "Arsenal"
|
90
86
|
```
|
91
87
|
|
92
88
|
## Contributing
|
data/lib/parsed/parseable.rb
CHANGED
@@ -5,62 +5,85 @@ module Parsed
|
|
5
5
|
|
6
6
|
module Parseable
|
7
7
|
|
8
|
+
# Callback invoked whenever the receiver is included in another module or
|
9
|
+
# class. This should be used in preference to Module.append_features if your
|
10
|
+
# code wants to perform some action when a module is included in another.
|
11
|
+
|
8
12
|
def self.included(base)
|
9
|
-
|
10
|
-
|
11
|
-
|
13
|
+
|
14
|
+
# Sets the instance variable names by symbol to object, thereby
|
15
|
+
# frustrating the efforts of the class's author to attempt to provide
|
16
|
+
# proper encapsulation. The variable did not have to exist prior to this
|
17
|
+
# call. If the instance variable name is passed as a string, that string
|
18
|
+
# is converted to a symbol.
|
19
|
+
|
20
|
+
base.instance_variable_set("@parseable_fields", [])
|
21
|
+
base.instance_variable_set("@parseable_collections", [])
|
22
|
+
base.instance_variable_set("@parseable_json", {})
|
23
|
+
|
24
|
+
# Adds to obj the instance methods from each module given as a parameter.
|
12
25
|
|
13
26
|
base.extend ClassMethods
|
14
27
|
end
|
15
28
|
|
16
29
|
module ClassMethods
|
17
30
|
|
18
|
-
|
19
|
-
|
31
|
+
attr_accessor :parseable_json,
|
32
|
+
:parseable_fields,
|
33
|
+
:parseable_collections
|
34
|
+
|
35
|
+
def parse(data)
|
36
|
+
@parseable_json = JSON.parse(data, { :symbolize_names => true })
|
20
37
|
|
21
38
|
instance = new
|
22
|
-
|
39
|
+
parse_fields(instance)
|
23
40
|
instance
|
24
41
|
end
|
25
42
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
43
|
+
# Public: Registers attributes that are to be parsed.
|
44
|
+
#
|
45
|
+
# attributes - The fields to be parsed
|
46
|
+
#
|
47
|
+
# Examples
|
48
|
+
#
|
49
|
+
# class Foo
|
50
|
+
# include Parsed::Parseable
|
51
|
+
# attr_accessor :foo, :bar, :baz
|
52
|
+
# parsed :foo, :bar, :baz
|
53
|
+
# end
|
54
|
+
#
|
55
|
+
def parses(*attributes)
|
56
|
+
attributes.each do |attributes|
|
57
|
+
parseable_fields << attributes
|
58
|
+
end
|
32
59
|
end
|
33
60
|
|
34
61
|
private
|
35
62
|
|
36
|
-
def parse_instance(instance)
|
37
|
-
parse_fields(instance)
|
38
|
-
parse_collections(instance)
|
39
|
-
end
|
40
|
-
|
41
63
|
def parse_fields(instance)
|
42
|
-
|
43
|
-
value =
|
64
|
+
parseable_fields.each do |field|
|
65
|
+
value = parse_field(field)
|
44
66
|
instance.send("#{field}=".to_sym, value)
|
45
67
|
end
|
46
68
|
end
|
47
69
|
|
48
|
-
def
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
elements
|
53
|
-
|
54
|
-
parsed_elements = elements.map do |element|
|
70
|
+
def parse_field(field)
|
71
|
+
clazz = determine_collection_class(field)
|
72
|
+
if clazz
|
73
|
+
elements = parseable_json[field.to_sym] || []
|
74
|
+
elements.map do |element|
|
55
75
|
clazz.parse(element.to_json)
|
56
76
|
end
|
57
|
-
|
58
|
-
|
77
|
+
else
|
78
|
+
parseable_json[field.to_sym]
|
59
79
|
end
|
60
80
|
end
|
61
81
|
|
82
|
+
def determine_collection_class(field)
|
83
|
+
field.to_s.singularize.camelize.constantize
|
84
|
+
rescue NameError => e
|
85
|
+
nil
|
86
|
+
end
|
62
87
|
end
|
63
|
-
|
64
88
|
end
|
65
|
-
|
66
89
|
end
|
data/lib/parsed/version.rb
CHANGED
data/parsed.gemspec
CHANGED
@@ -0,0 +1,104 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class League
|
4
|
+
include Parsed::Parseable
|
5
|
+
|
6
|
+
attr_accessor :name, :country, :teams
|
7
|
+
|
8
|
+
parses :name, :teams
|
9
|
+
|
10
|
+
end
|
11
|
+
|
12
|
+
class Team
|
13
|
+
include Parsed::Parseable
|
14
|
+
|
15
|
+
attr_accessor :name, :city
|
16
|
+
|
17
|
+
parses :name, :city
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
describe League do
|
22
|
+
|
23
|
+
describe '#parse' do
|
24
|
+
|
25
|
+
describe 'parsing fields' do
|
26
|
+
|
27
|
+
let(:premier_league) do
|
28
|
+
<<-EOS
|
29
|
+
{
|
30
|
+
"name": "Premier League",
|
31
|
+
"country": "England"
|
32
|
+
}
|
33
|
+
EOS
|
34
|
+
end
|
35
|
+
|
36
|
+
subject do
|
37
|
+
League.parse(premier_league)
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'parses fields' do
|
41
|
+
subject.name.should == 'Premier League'
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'skips fields that are not to be parsed' do
|
45
|
+
subject.country.should be_nil
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
describe 'parsing collections' do
|
51
|
+
|
52
|
+
describe 'with elements' do
|
53
|
+
|
54
|
+
let(:premier_league) do
|
55
|
+
<<-EOS
|
56
|
+
{
|
57
|
+
"name": "Premier League",
|
58
|
+
|
59
|
+
"teams": [
|
60
|
+
{ "name": "Arsenal", "city": "London" },
|
61
|
+
{ "name": "Swansea City", "city": "Swansea" }
|
62
|
+
]
|
63
|
+
}
|
64
|
+
EOS
|
65
|
+
end
|
66
|
+
|
67
|
+
subject do
|
68
|
+
League.parse(premier_league)
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'returns an object for each element in the collection' do
|
72
|
+
subject.teams.size == 2
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'parses parseable objects inside the collection' do
|
76
|
+
subject.teams.first.name == 'Arsenal'
|
77
|
+
subject.teams.first.city == 'Swansea City'
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
describe 'without elements' do
|
83
|
+
|
84
|
+
let(:premier_league) do
|
85
|
+
<<-EOS
|
86
|
+
{
|
87
|
+
"name": "Premier League",
|
88
|
+
"teams": []
|
89
|
+
}
|
90
|
+
EOS
|
91
|
+
end
|
92
|
+
|
93
|
+
subject do
|
94
|
+
League.parse(premier_league)
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'returns an empty collection' do
|
98
|
+
subject.teams.should be_empty
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'parsed'
|
3
|
+
|
4
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
5
|
+
RSpec.configure do |config|
|
6
|
+
|
7
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
8
|
+
config.run_all_when_everything_filtered = true
|
9
|
+
config.filter_run :focus
|
10
|
+
|
11
|
+
# Run specs in random order to surface order dependencies. If you find an
|
12
|
+
# order dependency and want to debug it, you can fix the order by providing
|
13
|
+
# the seed, which is printed after each run.
|
14
|
+
# --seed 1234
|
15
|
+
config.order = 'random'
|
16
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: parsed
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robin Roestenburg
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-07-
|
11
|
+
date: 2013-07-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: activesupport
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -63,6 +77,7 @@ extensions: []
|
|
63
77
|
extra_rdoc_files: []
|
64
78
|
files:
|
65
79
|
- .gitignore
|
80
|
+
- .rspec
|
66
81
|
- Gemfile
|
67
82
|
- LICENSE.txt
|
68
83
|
- README.md
|
@@ -71,6 +86,8 @@ files:
|
|
71
86
|
- lib/parsed/parseable.rb
|
72
87
|
- lib/parsed/version.rb
|
73
88
|
- parsed.gemspec
|
89
|
+
- spec/parsing_regular_classes_spec.rb
|
90
|
+
- spec/spec_helper.rb
|
74
91
|
homepage: https://github.com/robinroestenburg/parsed
|
75
92
|
licenses:
|
76
93
|
- MIT
|
@@ -95,4 +112,6 @@ rubygems_version: 2.0.3
|
|
95
112
|
signing_key:
|
96
113
|
specification_version: 4
|
97
114
|
summary: Write a gem summary
|
98
|
-
test_files:
|
115
|
+
test_files:
|
116
|
+
- spec/parsing_regular_classes_spec.rb
|
117
|
+
- spec/spec_helper.rb
|