parsed 0.1.1 → 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fad66279c57cbc0a81e19e54982931973ec28bd9
4
- data.tar.gz: 0aeae4eae56089a78f81898ff8cb71f437d8035f
3
+ metadata.gz: e649af05fed7637c624567a995d7d42065f94037
4
+ data.tar.gz: 835ea170f8bcefb50a2504a3dbd0c92844584ad2
5
5
  SHA512:
6
- metadata.gz: 67959d141ad658007d9ae612773f2a4104b550f854c4b78b602c8b4b9aace7e6d122bf5bcd2962d7d3227b9b3200fa1f80a5e99ef0ab79997a61b29a553bb315
7
- data.tar.gz: 0de8c97c5456532ed10d5e5f369c06e1779aeab62438447abb946e60453f8384ede686dde6bf60dee743a58389b38353dd0011bbfdbd335d5fbb9a82211a9274
6
+ metadata.gz: 8311ecf1b5363788ea33545a48df968cc2fbc52b79c437396361b306c951730989e949ba284fdf90fab9b12b8953e5eb2039927f64e057baf0ce03bed3ce4c8e
7
+ data.tar.gz: 99bbd5c32f951b0ff7123dbe446e6c2c1168de4b64bd00343356e29b9d03e2b5f842fa3cfd265a9cfd5c731c438e13594f850e42519d7872f375a37a8501d7f5
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
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 # => 'Premier League'
85
- p league.teams # =>
77
+ p league.name
78
+ # => "Premier League"
86
79
 
87
- arsenal = league.teams.first
80
+ p league.teams
81
+ # => [#<Team:0x007fcd18848428 @name="Arsenal", @city="London">,
82
+ # #<Team:0x007fcd188436d0 @name="Swansea City", @city="Swansea">]
88
83
 
89
- p arsenal.name # => 'Arsenal'
84
+ p league.teams.first.name
85
+ # => "Arsenal"
90
86
  ```
91
87
 
92
88
  ## Contributing
@@ -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
- base.instance_variable_set("@fields", [])
10
- base.instance_variable_set("@collections", [])
11
- base.instance_variable_set("@json", {})
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
- def parse(json)
19
- @json = JSON.parse(json, { :symbolize_names => true })
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
- parse_instance(instance)
39
+ parse_fields(instance)
23
40
  instance
24
41
  end
25
42
 
26
- def parses(type)
27
- @fields << type
28
- end
29
-
30
- def parses_collection(type)
31
- @collections << type
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
- @fields.each do |field|
43
- value = @json[field.to_sym]
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 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|
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
- instance.send("#{collection}=".to_sym, parsed_elements)
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
@@ -1,3 +1,3 @@
1
1
  module Parsed
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.0'
3
3
  end
data/parsed.gemspec CHANGED
@@ -22,6 +22,7 @@ Gem::Specification.new do |gem|
22
22
 
23
23
  gem.add_development_dependency 'bundler', '~> 1.3'
24
24
  gem.add_development_dependency 'rake'
25
+ gem.add_development_dependency 'rspec'
25
26
 
26
27
  gem.add_runtime_dependency 'activesupport'
27
28
  end
@@ -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
@@ -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.1.1
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-01 00:00:00.000000000 Z
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