parsed 0.2.1 → 0.2.2
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 +4 -4
- data/lib/parsed/parseable.rb +19 -26
- data/lib/parsed/parses_json.rb +22 -0
- data/lib/parsed/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8f46c0b0e188e6d67cb47daf58082dfc8092dcd3
|
4
|
+
data.tar.gz: 91d380704374bdc1b80b9af8eecf4745bda53632
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ffe99125da545612439ca787d9f70a319a809a53d46b4001cf4258948b3ea03ced0f9c20821ebbeb4334427738b404370000f64b8d634d816f4ab25442312787
|
7
|
+
data.tar.gz: 916b138ae60c6acb784d367c8cc37fdd067dd6138ea08c3924453f2c8c132a1f3d98c847af4ca583989d2b271874b25d648de5529ec5ed7904c2a062d7be1975
|
data/lib/parsed/parseable.rb
CHANGED
@@ -1,39 +1,26 @@
|
|
1
1
|
require 'json'
|
2
2
|
require 'active_support/inflector'
|
3
|
+
require_relative 'parses_json'
|
3
4
|
|
4
5
|
module Parsed
|
5
6
|
|
6
7
|
module Parseable
|
7
8
|
|
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
|
-
|
12
9
|
def self.included(base)
|
13
|
-
|
14
|
-
|
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.
|
25
|
-
|
10
|
+
base.instance_variable_set(:@parseable_fields, [])
|
11
|
+
base.instance_variable_set(:@collection_class_cache, {})
|
26
12
|
base.extend ClassMethods
|
27
13
|
end
|
28
14
|
|
29
15
|
module ClassMethods
|
30
16
|
|
31
|
-
attr_accessor :
|
17
|
+
attr_accessor :parseable_hash,
|
32
18
|
:parseable_fields,
|
33
|
-
:
|
19
|
+
:parser
|
34
20
|
|
35
|
-
def parse(data)
|
36
|
-
@
|
21
|
+
def parse(data, parser = ParsesJson)
|
22
|
+
@parser = parser
|
23
|
+
@parseable_hash = parser.parse(data)
|
37
24
|
|
38
25
|
instance = new
|
39
26
|
parse_fields(instance)
|
@@ -70,24 +57,30 @@ module Parsed
|
|
70
57
|
def parse_field(field)
|
71
58
|
clazz = determine_collection_class(field)
|
72
59
|
if clazz
|
73
|
-
elements =
|
60
|
+
elements = parser.parse_elements(parseable_hash, field)
|
74
61
|
|
75
62
|
elements.map do |element|
|
76
63
|
if element.instance_of? Hash
|
77
|
-
clazz.parse(element
|
64
|
+
clazz.parse(element)
|
78
65
|
else
|
79
66
|
element
|
80
67
|
end
|
81
68
|
end
|
69
|
+
|
82
70
|
else
|
83
|
-
|
71
|
+
parser.parse_value(parseable_hash, field)
|
84
72
|
end
|
85
73
|
end
|
86
74
|
|
87
75
|
def determine_collection_class(field)
|
88
|
-
field
|
76
|
+
if @collection_class_cache.has_key? field
|
77
|
+
@collection_class_cache[field]
|
78
|
+
else
|
79
|
+
@collection_class_cache[field] =
|
80
|
+
field.to_s.singularize.camelize.constantize
|
81
|
+
end
|
89
82
|
rescue NameError => e
|
90
|
-
nil
|
83
|
+
@collection_class_cache[field] = nil
|
91
84
|
end
|
92
85
|
end
|
93
86
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module Parsed
|
4
|
+
class ParsesJson < Struct.new(:data)
|
5
|
+
|
6
|
+
def self.parse(data)
|
7
|
+
return data if data.instance_of? Hash
|
8
|
+
|
9
|
+
JSON.parse(data, { :symbolize_names => true })
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.parse_value(data, field)
|
13
|
+
data[field.to_sym]
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.parse_elements(data, field)
|
17
|
+
data[field.to_sym] || []
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
data/lib/parsed/version.rb
CHANGED
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.2.
|
4
|
+
version: 0.2.2
|
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-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -84,6 +84,7 @@ files:
|
|
84
84
|
- Rakefile
|
85
85
|
- lib/parsed.rb
|
86
86
|
- lib/parsed/parseable.rb
|
87
|
+
- lib/parsed/parses_json.rb
|
87
88
|
- lib/parsed/version.rb
|
88
89
|
- parsed.gemspec
|
89
90
|
- spec/parsing_regular_classes_spec.rb
|