parsed 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a31b648e579f02f5a67e76ff13c50c320a41684e
4
- data.tar.gz: c08072c995a863fcca5798d7582ebe2f0f7ae8b8
3
+ metadata.gz: 8f46c0b0e188e6d67cb47daf58082dfc8092dcd3
4
+ data.tar.gz: 91d380704374bdc1b80b9af8eecf4745bda53632
5
5
  SHA512:
6
- metadata.gz: 438a84a1c8c5b334c21d40863ca1d530cb1e86ba687a24964f94891c7ca952222c0272dcbeadf210b5cf10e55787596674cbcd166884b4fd01c459ef80468bb6
7
- data.tar.gz: 6c52e884a17a830ab855cd468a1884635442f0b373adcd798925e86d99d31acb9f2da6a8829f21ee7bf67487327c2ce7fdbfd6a1ef7e66f805a90586eaf61a1b
6
+ metadata.gz: ffe99125da545612439ca787d9f70a319a809a53d46b4001cf4258948b3ea03ced0f9c20821ebbeb4334427738b404370000f64b8d634d816f4ab25442312787
7
+ data.tar.gz: 916b138ae60c6acb784d367c8cc37fdd067dd6138ea08c3924453f2c8c132a1f3d98c847af4ca583989d2b271874b25d648de5529ec5ed7904c2a062d7be1975
@@ -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
- # 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.
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 :parseable_json,
17
+ attr_accessor :parseable_hash,
32
18
  :parseable_fields,
33
- :parseable_collections
19
+ :parser
34
20
 
35
- def parse(data)
36
- @parseable_json = JSON.parse(data, { :symbolize_names => true })
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 = parseable_json[field.to_sym] || []
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.to_json)
64
+ clazz.parse(element)
78
65
  else
79
66
  element
80
67
  end
81
68
  end
69
+
82
70
  else
83
- parseable_json[field.to_sym]
71
+ parser.parse_value(parseable_hash, field)
84
72
  end
85
73
  end
86
74
 
87
75
  def determine_collection_class(field)
88
- field.to_s.singularize.camelize.constantize
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
@@ -1,3 +1,3 @@
1
1
  module Parsed
2
- VERSION = '0.2.1'
2
+ VERSION = '0.2.2'
3
3
  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.2.1
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-02 00:00:00.000000000 Z
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