expressir 0.2.10-x86-linux → 0.2.11-x86-linux

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
  SHA256:
3
- metadata.gz: dcd91f62c5ffc7c84f9671104bbc910073591765b30ced62e6b40b7af9a31ccd
4
- data.tar.gz: 11c143d244ee7fcf23f0f3e8423e480cb6f8e4af09e49e326742bb18546f271e
3
+ metadata.gz: 5961c268267212e63da6edff1351ef7c9d8d3b52dc1d99d0d4843d7a92123078
4
+ data.tar.gz: 7b5365059b79b01fa2e46562f39d494f12236ba237745ccbff44bd9477f2e1bc
5
5
  SHA512:
6
- metadata.gz: b57fa0069288475c06a602c9b0eb4b0c58f76c90e7011b030cd360736a85558d4a9883c08feac4b0598002fb3b5349fac6c719ef787d66115d5fe73db50bc39d
7
- data.tar.gz: a8c8acc103aac19c9118ba81632f749d7402a0695a5ff2efe2e6c205243b383b7690d2003db67570468789bdf194e2bb89f8e70b8113d70abfe85f8c59fe8061
6
+ metadata.gz: 93e0a0f3eb42f0570c1d075d22747d414321037a90f61e9ed96515eb41b4177ff19cace1429b348a36be57ed1c91e6d1e707ec68fcbe436d228f3f894247d58e
7
+ data.tar.gz: 18e429434ac5546908c84cc3d4713dfbe8d9af6f8dca2e03a50cb135f47242451e8f954240d75c0bca159634b6491767703b103b20b9f424be385c324c2d63a2
@@ -46,7 +46,7 @@ module Expressir
46
46
  def visit(ctx)
47
47
  result = super(ctx)
48
48
  attach_source(ctx, result)
49
- attach_parent(ctx, result)
49
+ attach_parent(result)
50
50
  attach_remarks(ctx, result)
51
51
  result
52
52
  end
@@ -124,13 +124,9 @@ module Expressir
124
124
  end
125
125
  end
126
126
 
127
- def attach_parent(ctx, node)
128
- if node.class.method_defined? :children
129
- node.children.each do |child_node|
130
- if child_node.class.method_defined? :parent and !child_node.parent
131
- child_node.parent = node
132
- end
133
- end
127
+ def attach_parent(node)
128
+ if node.class.method_defined? :attach_parent_to_children
129
+ node.attach_parent_to_children
134
130
  end
135
131
  end
136
132
 
@@ -1,16 +1,21 @@
1
1
  module Expressir
2
2
  module Model
3
3
  class ModelElement
4
- CLASS_KEY = '_class'
4
+ CLASS_KEY = '_class'
5
+ SOURCE_KEY = 'source'
5
6
 
6
7
  def to_hash(options = {})
7
8
  skip_empty = options[:skip_empty]
9
+ formatter = options[:formatter]
8
10
 
9
- instance_variables.select{|x| x != :@parent}.each_with_object({ CLASS_KEY => self.class.name }) do |variable, result|
11
+ hash = {}
12
+ hash[CLASS_KEY] = self.class.name
13
+
14
+ instance_variables.select{|x| x != :@parent}.each_with_object(hash) do |variable, result|
10
15
  key = variable.to_s.sub(/^@/, '')
11
16
  value = instance_variable_get(variable)
12
17
 
13
- # skip default values (nil, empty array)
18
+ # skip empty values (nil, empty array)
14
19
  if !skip_empty or !(value.nil? or (value.is_a? Array and value.count == 0))
15
20
  result[key] = if value.is_a? Array
16
21
  value.map do |value|
@@ -27,6 +32,12 @@ module Expressir
27
32
  end
28
33
  end
29
34
  end
35
+
36
+ if formatter
37
+ hash[SOURCE_KEY] = formatter.format(self)
38
+ end
39
+
40
+ hash
30
41
  end
31
42
 
32
43
  def self.from_hash(hash)
@@ -51,13 +62,8 @@ module Expressir
51
62
 
52
63
  node = Object.const_get(node_class).new(node_options)
53
64
 
54
- # attach parent
55
- if node.class.method_defined? :children
56
- node.children.each do |child_node|
57
- if child_node.class.method_defined? :parent and !child_node.parent
58
- child_node.parent = node
59
- end
60
- end
65
+ if node.class.method_defined? :attach_parent_to_children
66
+ node.attach_parent_to_children
61
67
  end
62
68
 
63
69
  node
@@ -55,6 +55,14 @@ module Expressir
55
55
  end
56
56
  end
57
57
 
58
+ def attach_parent_to_children
59
+ children.each do |child_node|
60
+ if child_node.class.method_defined? :parent and !child_node.parent
61
+ child_node.parent = self
62
+ end
63
+ end
64
+ end
65
+
58
66
  def children
59
67
  raise 'Not implemented'
60
68
  end
@@ -1,3 +1,3 @@
1
1
  module Expressir
2
- VERSION = "0.2.10".freeze
2
+ VERSION = "0.2.11".freeze
3
3
  end
@@ -1,9 +1,10 @@
1
1
  require "spec_helper"
2
2
  require "expressir/express_exp/parser"
3
+ require "expressir/express_exp/formatter"
3
4
 
4
5
  RSpec.describe Expressir::Model::ModelElement do
5
6
  describe ".from_hash" do
6
- it "parses an object from hash" do
7
+ it "parses an object from a hash" do
7
8
  input = {
8
9
  "_class" => "Expressir::Model::Repository",
9
10
  "schemas" => [{
@@ -21,17 +22,17 @@ RSpec.describe Expressir::Model::ModelElement do
21
22
  expect(repo).to be_instance_of(Expressir::Model::Repository)
22
23
  expect(repo.schemas).to be_instance_of(Array)
23
24
  expect(repo.schemas.count).to eq(1)
24
- expect(repo.schemas[0]).to be_instance_of(Expressir::Model::Schema)
25
- expect(repo.schemas[0].id).to eq("simple_schema")
26
- expect(repo.schemas[0].entities).to be_instance_of(Array)
27
- expect(repo.schemas[0].entities.count).to eq(1)
28
- expect(repo.schemas[0].entities[0]).to be_instance_of(Expressir::Model::Entity)
29
- expect(repo.schemas[0].entities[0].id).to eq("empty_entity")
25
+ expect(repo.schemas.first).to be_instance_of(Expressir::Model::Schema)
26
+ expect(repo.schemas.first.id).to eq("simple_schema")
27
+ expect(repo.schemas.first.entities).to be_instance_of(Array)
28
+ expect(repo.schemas.first.entities.count).to eq(1)
29
+ expect(repo.schemas.first.entities.first).to be_instance_of(Expressir::Model::Entity)
30
+ expect(repo.schemas.first.entities.first.id).to eq("empty_entity")
30
31
  end
31
32
  end
32
33
 
33
34
  describe ".to_hash" do
34
- it "exports an object to hash" do
35
+ it "exports an object to a hash" do
35
36
  repo = Expressir::ExpressExp::Parser.from_exp(sample_file)
36
37
 
37
38
  result = repo.to_hash
@@ -40,14 +41,31 @@ RSpec.describe Expressir::Model::ModelElement do
40
41
  expect(result["_class"]).to eq("Expressir::Model::Repository")
41
42
  expect(result["schemas"]).to be_instance_of(Array)
42
43
  expect(result["schemas"].count).to eq(1)
43
- expect(result["schemas"][0]).to be_instance_of(Hash)
44
- expect(result["schemas"][0]["_class"]).to eq("Expressir::Model::Schema")
45
- expect(result["schemas"][0]["id"]).to eq("simple_schema")
46
- expect(result["schemas"][0]["entities"]).to be_instance_of(Array)
47
- expect(result["schemas"][0]["entities"].count).to eq(1)
48
- expect(result["schemas"][0]["entities"][0]).to be_instance_of(Hash)
49
- expect(result["schemas"][0]["entities"][0]["_class"]).to eq("Expressir::Model::Entity")
50
- expect(result["schemas"][0]["entities"][0]["id"]).to eq("empty_entity")
44
+ expect(result["schemas"].first).to be_instance_of(Hash)
45
+ expect(result["schemas"].first["_class"]).to eq("Expressir::Model::Schema")
46
+ expect(result["schemas"].first["id"]).to eq("simple_schema")
47
+ expect(result["schemas"].first["entities"]).to be_instance_of(Array)
48
+ expect(result["schemas"].first["entities"].count).to eq(1)
49
+ expect(result["schemas"].first["entities"].first).to be_instance_of(Hash)
50
+ expect(result["schemas"].first["entities"].first["_class"]).to eq("Expressir::Model::Entity")
51
+ expect(result["schemas"].first["entities"].first["id"]).to eq("empty_entity")
52
+ end
53
+
54
+ it "exports an object to a hash with a custom formatter" do
55
+ repo = Expressir::ExpressExp::Parser.from_exp(sample_file)
56
+
57
+ class CustomFormatter < Expressir::ExpressExp::Formatter
58
+ def format_schema(node)
59
+ "Oook."
60
+ end
61
+ end
62
+ result = repo.to_hash({ formatter: CustomFormatter })
63
+
64
+ expect(result).to be_instance_of(Hash)
65
+ expect(result["_class"]).to eq("Expressir::Model::Repository")
66
+ expect(result["schemas"]).to be_instance_of(Array)
67
+ expect(result["schemas"].count).to eq(1)
68
+ expect(result["schemas"].first["source"]).to eq("Oook.")
51
69
  end
52
70
  end
53
71
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: expressir
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.10
4
+ version: 0.2.11
5
5
  platform: x86-linux
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-02-08 00:00:00.000000000 Z
11
+ date: 2021-02-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri