expressir 0.2.10 → 0.2.11

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: b7ba84b2e2bb6810108340208cb5d2e72047af1576990acfeb73c4f077eb8ba2
4
- data.tar.gz: e92db63b885211b20df63adb3f91e6bb299af7ce745aa4c6f9557e7deb583257
3
+ metadata.gz: c788bcd3215a930c41909c735d098a1feafa31577617a21ef8c1c125540d52ce
4
+ data.tar.gz: 6a8c08814b964eefa5b061ca557eb32f0a65e1e8b4b0d1aef4729bf32ca6f2a3
5
5
  SHA512:
6
- metadata.gz: 5ded502bc8563615f49af32175de1127b41aa019f0efe42699ba953da542bc5df04d3639196c6355f55d1b12c94b496eb8bfe03e1e41a179df69bfd97270dc5a
7
- data.tar.gz: 99015fc2a30fe12f69ccd861ffd49e54593666e6892176f0076c90f215df282bb77c83ddf7bf197915366bdab162757a1834851dd4118426de9fb38735c69855
6
+ metadata.gz: fd1e849c801051e85571859e2df466d9e834168bb075453e0c4a9d2686e23f7233f5f564640af4110a1c8232e2d7505cfcc1a5950fa5f50bf31f59885b02f658
7
+ data.tar.gz: 7d48af398524ca7c829fd05456e86c2b0d301f947bcd9dafa45dd6005325c91415db0f0e3d7dba612b6d3b6c0b6821cdf96a386889a53cfcd4dcf2971331b81c
@@ -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: ruby
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