aston 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 825ca0a986669eb04c434775792fadf6f4127f57d82144d4fab9282d2af7e8bd
4
- data.tar.gz: d6acd466695b6236f494a9b00c784f0b33a7cf61dafedb8d9d2cf1c610d5e811
3
+ metadata.gz: eebe5f2b036ff05e61f6e1b09233577fa2494c928c5a50ed21a6392c49e000dd
4
+ data.tar.gz: 60d363e3a4047ead68e7af6b16b6a7d05d9419d6918622d1c5e54b15426b8928
5
5
  SHA512:
6
- metadata.gz: df8dda9a4fe333ebdc535f7cda0369a30be3478dbbc295f093a08797b9ba9345c7226339fdd10957577c1a9b631aae7caf9f62ae249f290f71dce18663c683d8
7
- data.tar.gz: b0c056e04628e20b07b9b7346282d55a1e8cef5b250a7751be204d2fb4685f1042e7feb51bf4c58fb98f2e96cf00a431087fce4dbfabed96d1058053466a1909
6
+ metadata.gz: 0f20ae2f4e61ac774706b0c8ac136e14606485f68877817cbddfb2e27a4bd4e1a961fd898989e269dccf4313c7df0ab01d330ec0ff688938a60f6530bd6846c4
7
+ data.tar.gz: 14ba62157897f974353b9f7fc198213bdc4dd285718f77470f7cc0d0f261ce6e3b748f644d522b8a339d11983c4b3081d693b0ee1464ea9a2197da3ed2137f52
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- aston (0.1.0)
4
+ aston (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # Aston
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/aston`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ The tiny library providing a tooling to deal with _ASTON_, which is like _JSON_, but isomorphic to _XML_.
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,18 +20,123 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ ```ruby
24
+ Aston.new(:root).
25
+ then { |a| a.put_attribute([:bar, :baz], 'attr', 'value') }.
26
+ then { |a| a.put_content([:bar, :baz], 'Hello, world!') }
27
+ #⇒ #<Aston:0x0000646926a11370
28
+ # @attributes=#<Aston::Attributes:0x0000646926a112f8 @data={}>,
29
+ # @content=
30
+ # #<Aston::Content:0x0000646926a112d0
31
+ # @data=
32
+ # [#<Aston:0x0000646926a110f0
33
+ # @attributes=#<Aston::Attributes:0x0000646926a11078 @data={}>,
34
+ # @content=
35
+ # #<Aston::Content:0x0000646926a11050
36
+ # @data=
37
+ # [#<Aston:0x0000646926a10f38
38
+ # @attributes=#<Aston::Attributes:0x0000646926a10ec0 @data={"attr"=>"value"}>,
39
+ # @content=#<Aston::Content:0x0000646926a10e98 @data=["Hello, world!"]>,
40
+ # @name=:baz>]>,
41
+ # @name=:bar>]>,
42
+ # @name=:root>
43
+ ```
44
+
45
+ ### `#to_s`
46
+
47
+ ```ruby
48
+ puts Aston.new(:root).
49
+ then { |a| a.put_attribute([:bar, :baz], 'attr', 'value') }.
50
+ then { |a| a.put_content([:bar, :baz], "Hello, world!") }.
51
+ to_s
52
+ ```
53
+
54
+ ```xml
55
+ <!-- Aston:1380:root -->
56
+ <root>
57
+ <!-- Aston:1400:bar -->
58
+ <bar>
59
+ <!-- Aston:1420:baz -->
60
+ <baz attr='value'>
61
+ Hello, world!
62
+ </baz>
63
+ </bar>
64
+ </root>
65
+ ```
66
+
67
+ ### `#to_json`
26
68
 
27
- ## Development
69
+ ```ruby
70
+ puts Aston.new(:root).
71
+ then { |a| a.put_attribute([:bar, :baz], 'attr', 'value') }.
72
+ then { |a| a.put_content([:bar, :baz], "Hello, world!") }.
73
+ to_json
74
+ #⇒ {"name":"root","attributes":{},"content":[
75
+ # {"name":"bar","attributes":{},"content":[
76
+ # {"name":"baz","attributes":{"attr":"value"},"content":["Hello, world!"]}]}]}
77
+ ```
28
78
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
79
+ ### `Aston#parse_hash`
30
80
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
81
+ ```ruby
82
+ json = Aston.new(:root).
83
+ then { |a| a.put_attribute([:bar, :baz], 'attr', 'value') }.
84
+ then { |a| a.put_content([:bar, :baz], "Hello, world!") }.
85
+ to_json
86
+ Aston.parse_hash(JSON.parse(json))
87
+ #⇒ #<Aston:0x0000643279caa890
88
+ # @attributes=#<Aston::Attributes:0x0000643279caa868 @data={}>,
89
+ # @content=
90
+ # #<Aston::Content:0x0000643279caa840
91
+ # @data=
92
+ # [#<Aston:0x0000643279caa930
93
+ # @attributes=#<Aston::Attributes:0x0000643279caa908 @data={}>,
94
+ # @content=
95
+ # #<Aston::Content:0x0000643279caa8e0
96
+ # @data=
97
+ # [#<Aston:0x0000643279caa9d0
98
+ # @attributes=#<Aston::Attributes:0x0000643279caa9a8 @data={"attr"=>"value"}>,
99
+ # @content=#<Aston::Content:0x0000643279caa980 @data=["Hello, world!"]>,
100
+ # @name="baz">]>,
101
+ # @name="bar">]>,
102
+ # @name="root">
103
+ ```
32
104
 
33
- ## Contributing
105
+ ### `#update_in`
34
106
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/am-kantox/aston. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/am-kantox/aston/blob/master/CODE_OF_CONDUCT.md).
107
+ ```ruby
108
+ a = Aston.new :aston, attributes: { foo: :bar }
109
+ a.put_attribute %i[bar baz], :ok, 42
110
+ a.put_content %i[bar baz], 'Hello'
111
+ a.update_in %i[bar baz] do |content|
112
+ content <<
113
+ Aston.new(:seq, attributes: { name: :seq1 }) <<
114
+ Aston.new(:seq, attributes: { name: :seq2 }) <<
115
+ Aston.new(:seq, attributes: { name: :seq3 })
116
+ end
117
+
118
+ #⇒ [#<Aston:0x0000643279c616e0
119
+ # @attributes=#<Aston::Attributes:0x0000643279c61668 @data={:ok=>42}>,
120
+ # @content=
121
+ # #<Aston::Content:0x0000643279c61640
122
+ # @data=
123
+ # ["Hello",
124
+ # #<Aston:0x0000643279c60d58
125
+ # @attributes=#<Aston::Attributes:0x0000643279c60d08 @data={:name=>:seq1}>,
126
+ # @content=#<Aston::Content:0x0000643279c60ce0 @data=[]>,
127
+ # @name=:seq>,
128
+ # #<Aston:0x0000643279c60c68
129
+ # @attributes=#<Aston::Attributes:0x0000643279c60c18 @data={:name=>:seq2}>,
130
+ # @content=#<Aston::Content:0x0000643279c60bc8 @data=[]>,
131
+ # @name=:seq>,
132
+ # #<Aston:0x0000643279c60b50
133
+ # @attributes=#<Aston::Attributes:0x0000643279c60b00 @data={:name=>:seq3}>,
134
+ # @content=#<Aston::Content:0x0000643279c60ad8 @data=[]>,
135
+ # @name=:seq>]>,
136
+ # @name=:baz>]
137
+ ```
36
138
 
139
+ `#update_in` returns an array of updated elements on the path given. Unlike `#update_in`.
37
140
 
38
141
  ## License
39
142
 
data/lib/aston/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Aston
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
data/lib/aston.rb CHANGED
@@ -10,7 +10,7 @@ class Aston
10
10
  class Attributes
11
11
  attr_reader :data
12
12
 
13
- def initialize(data)
13
+ def initialize(data = {})
14
14
  @data = data
15
15
  end
16
16
 
@@ -22,10 +22,6 @@ class Aston
22
22
  @data[key] = value
23
23
  end
24
24
 
25
- def to_s
26
- @data.map { |k, v| "#{k}='#{v}'" }.join(' ')
27
- end
28
-
29
25
  def ==(other)
30
26
  other.is_a?(Attributes) && @data == other.data
31
27
  end
@@ -33,13 +29,21 @@ class Aston
33
29
  def size
34
30
  @data.size
35
31
  end
32
+
33
+ def to_s
34
+ @data.map { |k, v| "#{k}='#{v}'" }.join(' ')
35
+ end
36
+
37
+ def to_json(opts = nil)
38
+ @data.to_json(opts)
39
+ end
36
40
  end
37
41
 
38
42
  # Wrapper for content
39
43
  class Content
40
44
  attr_reader :data
41
45
 
42
- def initialize(data)
46
+ def initialize(data = [])
43
47
  @data = data
44
48
  end
45
49
 
@@ -64,6 +68,10 @@ class Aston
64
68
  def to_s
65
69
  @data.map(&:to_s).join("\n")
66
70
  end
71
+
72
+ def to_json(opts = nil)
73
+ @data.to_json(opts)
74
+ end
67
75
  end
68
76
 
69
77
  attr_reader :name, :attributes, :content
@@ -92,46 +100,55 @@ class Aston
92
100
 
93
101
  def to_s
94
102
  comment = "<!-- Aston:#{__id__}:#{name} -->"
95
- opening = @attributes.data.empty? ? "<#{name}>" : ['<', name, *@attributes].join(' ') << '>'
103
+ opening = @attributes.data.empty? ? "<#{name}>" : ["<#{name}", *@attributes].join(' ') << '>'
96
104
  [comment, opening, *@content, "</#{name}>"].join("\n")
97
105
  end
98
106
 
107
+ def to_json(opts = nil)
108
+ { name: @name, attributes: @attributes, content: @content }.to_json(opts)
109
+ end
110
+
111
+ def self.parse_hash(hash)
112
+ name = hash.fetch('name', hash.fetch(:name, ''))
113
+ attributes = hash.fetch('attributes', hash.fetch(:attributes, {}))
114
+ content = hash.fetch('content', hash.fetch(:content, [])).map do |elem|
115
+ case elem
116
+ when String then elem
117
+ when Hash then Aston.parse_hash(elem)
118
+ end
119
+ end
120
+ Aston.new(name, attributes: attributes, content: content)
121
+ end
122
+
99
123
  def paths
100
124
  do_paths([], [])
101
125
  end
102
126
 
103
- def find(name)
104
- @content.data.find { |a| a.is_a?(Aston) && a.name == name }
127
+ def filter(name)
128
+ @content.data.select { |a| a.is_a?(Aston) && a.name == name }
105
129
  end
106
130
 
107
- def update_in(path, create_intermediate: true, &_block)
108
- yield clamber(path, create_intermediate)
131
+ def update_in(path, create_intermediate: true, &block)
132
+ clamber(path, create_intermediate).each(&block)
109
133
  rescue StandardError => e
110
134
  raise Error, e
111
135
  end
112
136
 
113
137
  def put_attribute(path, name, value, create_intermediate: true)
114
- tap { clamber(path, create_intermediate)[name] = value }
138
+ tap { clamber(path, create_intermediate).each { |aston| aston[name] = value } }
115
139
  rescue StandardError => e
116
140
  raise Error, e
117
141
  end
118
142
 
119
- def put_content(path, aston, create_intermediate: true)
120
- tap { clamber(path, create_intermediate) << aston }
143
+ def put_content(path, content, create_intermediate: true)
144
+ tap { clamber(path, create_intermediate).each { |aston| aston << content } }
121
145
  rescue StandardError => e
122
146
  raise Error, e
123
147
  end
124
148
 
125
- def get(path, default: nil)
126
- path = fix_path(path)
127
- path.reduce(self) do |memo, e|
128
- current = memo.find(e)
129
-
130
- case current
131
- when NilClass then break default
132
- when String then current
133
- when Aston then current
134
- end
149
+ def get(path)
150
+ fix_path(path).reduce([self]) do |memo, e|
151
+ memo.flat_map { |aston| aston.filter(e) }
135
152
  end
136
153
  end
137
154
 
@@ -149,18 +166,20 @@ class Aston
149
166
  private
150
167
 
151
168
  def clamber(path, create_intermediate)
152
- fix_path(path).reduce(self) do |memo, e|
153
- current = memo.find(e)
154
-
155
- case current
156
- when NilClass then create_intermediate ? Aston.new(e).tap { |aston| memo << aston } : break
157
- when Aston then current
169
+ fix_path(path).reduce([self]) do |memo, e|
170
+ memo.flat_map do |aston|
171
+ aston.filter(e).tap do |currents|
172
+ next unless currents == []
173
+
174
+ value = [Aston.new(e).tap { |n| aston << n }] if create_intermediate
175
+ break value
176
+ end
158
177
  end
159
178
  end
160
179
  end
161
180
 
162
181
  def fix_path(path)
163
- path = path.split('.') if path.is_a?(String)
182
+ # path = path.split('.') if path.is_a?(String)
164
183
  raise Error, 'Path must be an array' unless path.is_a?(Array)
165
184
 
166
185
  path
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aston
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aleksei Matiushkin