jievro-ast 0.5.0 → 0.8.0

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: ca0d93bc074485e2ba45a4c1bb4c08f5eb1ee7af
4
- data.tar.gz: df3dd857a330104d2d4fdff7513bfa33e25108d2
3
+ metadata.gz: 49d9fe0ebd50c3c84a6de1eaa46c779a8d11477a
4
+ data.tar.gz: c73e464fb408e2eaa2df994b72df41ee454ba35d
5
5
  SHA512:
6
- metadata.gz: 884d3b767545f81a4f20ee8aa57131fa5fc56d936e155075c81984e4fe02b67c442fd902cdfc6429657a2a86bb5f04a86c17848f530ef635e37c17467fe7885d
7
- data.tar.gz: 39717a5271a2dcf3de6fc22163933c1897a77c6fe013a04d4715b5dfa7c739d2893760b4f38073a0cdec9965bd00d99f5a849fe9caf0f593b220ac8778065a42
6
+ metadata.gz: 5ccd7c52df8befc996f614267283f649b744fa81cfc80dab1703620754152863711a40342144f47fbcb3bb423f4bf440f9b8c4890fb54c5810092de45ec51a85
7
+ data.tar.gz: 9b6fc18a26281e6d18c651a3689d34d16fec5ce72bcc4d74eb4923190fb84c58c21bc5830728c97ab62de6335f3d486a287f8a4f22d37f14350f2951b7c915ca
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- jievro-ast (0.5.0)
4
+ jievro-ast (0.7.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -1,27 +1,37 @@
1
1
  module Jievro
2
2
  module Ast
3
3
  class Node
4
- TYPE_LITERAL = '__literal'
5
- TYPE_COLLECTION = '__collection'
4
+ attr_reader :id, :parent, :type
6
5
 
7
- attr_reader :parent, :type, :value
6
+ def initialize(initializer, parent = nil)
7
+ fail Exception, 'The initializer must be Hash' \
8
+ unless initializer.is_a?(Hash)
8
9
 
9
- def initialize(initializer, parent = nil, key = nil)
10
+ fail Exception, 'Initializer is missing __type value' \
11
+ unless initializer.key?(:__type)
12
+
13
+ @id = Ast::Node.generate_next_id
10
14
  @parent = parent
11
- @key = key
15
+ @value = {}
16
+ @type = initializer[:__type]
12
17
 
13
- case initializer
14
- when NilClass, FalseClass, TrueClass, String, Fixnum, Float
15
- initialize_as_literal(initializer)
16
- when Array
17
- initialize_as_array(initializer)
18
- when Hash
19
- initialize_as_hash(initializer)
20
- else
21
- fail Exception, "The initializer must be
22
- NilClass, FalseClass, TrueClass, String,
23
- Fixnum, Float, Array or Hash.
24
- #{initializer.class} given"
18
+ initializer.keys.each do |k|
19
+ next if k == :__type
20
+
21
+ value = initializer[k]
22
+
23
+ if literal?(value)
24
+ @value[k] = value
25
+ else
26
+ if array?(value)
27
+ @value[k] = []
28
+ value.each_with_index do |v, i|
29
+ @value[k][i] = Node.new(v, self)
30
+ end
31
+ else
32
+ @value[k] = Node.new(value, self)
33
+ end
34
+ end
25
35
  end
26
36
  end
27
37
 
@@ -32,25 +42,12 @@ module Jievro
32
42
  @value[key]
33
43
  end
34
44
 
35
- def key
36
- @key.to_s
37
- end
38
-
39
45
  def keys
40
46
  @value.keys
41
47
  end
42
48
 
43
- def value
44
- fail Exception, 'Cannot get value from a non literal node' \
45
- unless @type == TYPE_LITERAL
46
-
47
- @value
48
- end
49
-
50
49
  def serialize
51
- return @value if type == TYPE_LITERAL
52
-
53
- if type == TYPE_COLLECTION
50
+ if array?(@value)
54
51
  serialized = []
55
52
  else
56
53
  serialized = {
@@ -59,7 +56,18 @@ module Jievro
59
56
  end
60
57
 
61
58
  each_with_index do |v, k|
62
- serialized[k] = v.serialize
59
+ if literal?(v)
60
+ serialized[k] = v
61
+ else
62
+ if array?(v)
63
+ serialized[k] = []
64
+ v.each_with_index do |w, i|
65
+ serialized[k][i] = w.serialize
66
+ end
67
+ else
68
+ serialized[k] = v.serialize
69
+ end
70
+ end
63
71
  end
64
72
 
65
73
  serialized
@@ -72,9 +80,6 @@ module Jievro
72
80
  end
73
81
 
74
82
  def each_with_index
75
- fail Exception, 'Cannot iterate a literal node' \
76
- if @type == TYPE_LITERAL
77
-
78
83
  @value.keys.each do |v|
79
84
  yield @value[v], v
80
85
  end
@@ -82,33 +87,23 @@ module Jievro
82
87
 
83
88
  private
84
89
 
85
- def initialize_as_literal(initializer)
86
- @value = initializer
87
- @type = TYPE_LITERAL
88
- end
89
-
90
- def initialize_as_array(initializer)
91
- @value = {}
92
- @type = TYPE_COLLECTION
93
-
94
- initializer.each_with_index do |e, i|
95
- @value[i] = Node.new(e, self, i)
90
+ def literal?(value)
91
+ case value
92
+ when NilClass, FalseClass, TrueClass, String, Fixnum, Float
93
+ return true
94
+ else
95
+ return false
96
96
  end
97
97
  end
98
98
 
99
- def initialize_as_hash(initializer)
100
- fail Exception, 'Initializer is missing __type value' \
101
- unless initializer.key?(:__type)
102
-
103
- @value = {}
104
- @type = initializer[:__type]
105
-
106
- initializer.keys.each do |k|
107
- # skip all the meta info
108
- next if k.to_s.to_s.start_with?('__')
99
+ def array?(value)
100
+ value.is_a?(Array)
101
+ end
109
102
 
110
- @value[k] = Node.new(initializer[k], self, k)
111
- end
103
+ def self.generate_next_id
104
+ @id = 0 if @id.nil?
105
+ @id += 1
106
+ @id
112
107
  end
113
108
  end
114
109
  end
@@ -1,5 +1,5 @@
1
1
  module Jievro
2
2
  module Ast
3
- VERSION = '0.5.0'
3
+ VERSION = '0.8.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,55 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jievro-ast
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefano Azzalini
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-08 00:00:00.000000000 Z
11
+ date: 2015-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.9'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.9'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
33
  version: '10.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ~>
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
47
  version: '3.2'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ~>
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.2'
55
55
  description:
@@ -58,8 +58,8 @@ executables: []
58
58
  extensions: []
59
59
  extra_rdoc_files: []
60
60
  files:
61
- - .gitignore
62
- - .travis.yml
61
+ - ".gitignore"
62
+ - ".travis.yml"
63
63
  - Gemfile
64
64
  - Gemfile.lock
65
65
  - LICENSE.txt
@@ -79,17 +79,17 @@ require_paths:
79
79
  - lib
80
80
  required_ruby_version: !ruby/object:Gem::Requirement
81
81
  requirements:
82
- - - ~>
82
+ - - "~>"
83
83
  - !ruby/object:Gem::Version
84
84
  version: '2.0'
85
85
  required_rubygems_version: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - '>='
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  requirements: []
91
91
  rubyforge_project:
92
- rubygems_version: 2.0.14
92
+ rubygems_version: 2.4.6
93
93
  signing_key:
94
94
  specification_version: 4
95
95
  summary: AST model used internally by jievro-parser