expressir 0.2.10-arm64-darwin → 0.2.11-arm64-darwin

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: b6b9fd133266e935daa1a46dd8ed3405f59dd8ef07f0cdb1c111469a30e653c9
4
- data.tar.gz: 492f03f70f9ca4860d49d905094475cffe343ec8d5734f0a104c512a82be4432
3
+ metadata.gz: a7ca823c0cea8c0464cb3e31d6cd59d82ad2d82613260b082633d99d98380fc9
4
+ data.tar.gz: 0bceba8f9858852533ad4eb734b5b559a46ab97ba219594b5a44aab86e234f3d
5
5
  SHA512:
6
- metadata.gz: 25bc06b8f1901b3130a6f3d3b0bda8e38d5e5c3b881019d40f0b53dcf701d9ac4451fba2621e89aa9e547e0eaa507f4ca2888944f3652323e9d7785489dd8890
7
- data.tar.gz: bb6804d766279028844b929c1d3f8cdf6404c84bacf53d1c7b37b24eca23868991cdfd9ddec980ddf8905f51e78ead7e25e393be236e78ce45819c6af38c4cbb
6
+ metadata.gz: 9831af9252d67dee6425594d251471a64f9c769d953610fbbeb7932eed7cf3599429e466e12f78f61b793d1a294487a24dd0b8f4d04111eda1f77de611f82e52
7
+ data.tar.gz: 67dcad8003be72122c2b9aeeb504ce33cfd4e74fa509fa9de795050f9051b7be4220398d6de2b94d182fe15431ebbfc593c9aaf8685c03448f77558f04eec1e3
@@ -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: arm64-darwin
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