struct_trans 0.5.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f56ffa69760f4db52aa124497a8fd23a3560ba15
4
+ data.tar.gz: 4da168561259ac1cba3ccf85647f413fdbf10e8e
5
+ SHA512:
6
+ metadata.gz: ec6ac98f57ef4b56cef4582600e2007212867d2c456c365f347070f02a9b8c2965fe0550c67ff4b0c8683490d6c97f45ceead96eb6361f9b20cecf5db91d6166
7
+ data.tar.gz: 0a0d239f14b21d3eab0b209ec823380c73778f5a925ed0816bfbae2d489b0099d9c65a2a2a7060877c316be175e0069e9eb2a46e32bf1c38004715f3d07828ce
@@ -0,0 +1,2 @@
1
+ /pkg/
2
+ /coverage/
@@ -0,0 +1,3 @@
1
+ [submodule "task"]
2
+ path = task
3
+ url = git://github.com/godfat/gemgem
@@ -0,0 +1,18 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.5
5
+ - 2.3.1
6
+ - rbx
7
+ - jruby-9
8
+
9
+ before_install:
10
+ - rvm get head
11
+ - rvm reload
12
+ - rvm use --install $TRAVIS_RUBY_VERSION --binary --latest
13
+ install: 'gem install bundler; bundle install --retry=3'
14
+ script: 'ruby -vr bundler/setup -S rake test'
15
+
16
+ matrix:
17
+ allow_failures:
18
+ - rvm: rbx
@@ -0,0 +1,5 @@
1
+ # CHANGES
2
+
3
+ ## StructTrans 0.5.0 -- 2016-11-15
4
+
5
+ * Birthday!
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+
2
+ source 'https://rubygems.org'
3
+
4
+ gemspec
5
+
6
+ gem 'rake'
7
+ gem 'pork'
8
+
9
+ gem 'simplecov', :require => false if ENV['COV']
10
+ gem 'coveralls', :require => false if ENV['CI']
11
+
12
+ platform :rbx do
13
+ gem 'rubysl-singleton' # used in rake
14
+ end
@@ -0,0 +1,76 @@
1
+ # StructTrans [![Build Status](https://secure.travis-ci.org/godfat/struct_trans.png?branch=master)](http://travis-ci.org/godfat/struct_trans) [![Coverage Status](https://coveralls.io/repos/godfat/struct_trans/badge.png?branch=master)](https://coveralls.io/r/godfat/struct_trans?branch=master) [![Join the chat at https://gitter.im/godfat/struct_trans](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/godfat/struct_trans)
2
+
3
+ by Lin Jen-Shin ([godfat](http://godfat.org))
4
+
5
+ ## LINKS:
6
+
7
+ * [github](https://github.com/godfat/struct_trans)
8
+ * [rubygems](https://rubygems.org/gems/struct_trans)
9
+ * [rdoc](http://rdoc.info/github/godfat/struct_trans)
10
+ * [issues](https://github.com/godfat/struct_trans/issues) (feel free to ask for support)
11
+
12
+ ## DESCRIPTION:
13
+
14
+ Transform a struct with a schema to a hash, other struct, or more.
15
+
16
+ ## WHY?
17
+
18
+ We need an easy way to define how we want to expose data to the external.
19
+
20
+ ## REQUIREMENTS:
21
+
22
+ * Tested with MRI (official CRuby), Rubinius and JRuby.
23
+
24
+ ## INSTALLATION:
25
+
26
+ gem install struct_trans
27
+
28
+ ## SYNOPSIS:
29
+
30
+ Here's an example to transform a struct-like object, i.e. `'nnf'` into a hash
31
+ with a specific schema defined by a hash containing an array containing
32
+ another hash and a symbol.
33
+
34
+ ``` ruby
35
+ p StructTrans.trans_hash(
36
+ 'nnf',
37
+ [:inspect, {:reverse => [{:capitalize => :swapcase}, :upcase]}])
38
+ # ['"nnf"', {:reverse => [{:capitalize => 'fNN'}, 'FNN']}]
39
+ ```
40
+
41
+ We could also transform the same struct (i.e. `'nnf'`) into an open struct
42
+ rather than a hash if you prefer.
43
+
44
+ ``` ruby
45
+ require 'struct_trans/ostruct'
46
+
47
+ o = StructTrans.trans_ostruct(
48
+ 'nnf',
49
+ [:inspect, {:reverse => [{:capitalize => :swapcase}, :upcase]}])
50
+
51
+ p o[0] # '"nnf"'
52
+ p o[1].reverse[0].capitalize # 'fNN'
53
+ p o[1].reverse[1] # 'FNN'
54
+ ```
55
+
56
+ ## CONTRIBUTORS:
57
+
58
+ * Lin Jen-Shin (@godfat)
59
+
60
+ ## LICENSE:
61
+
62
+ Apache License 2.0
63
+
64
+ Copyright (c) 2016, Lin Jen-Shin (godfat)
65
+
66
+ Licensed under the Apache License, Version 2.0 (the "License");
67
+ you may not use this file except in compliance with the License.
68
+ You may obtain a copy of the License at
69
+
70
+ <http://www.apache.org/licenses/LICENSE-2.0>
71
+
72
+ Unless required by applicable law or agreed to in writing, software
73
+ distributed under the License is distributed on an "AS IS" BASIS,
74
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
75
+ See the License for the specific language governing permissions and
76
+ limitations under the License.
@@ -0,0 +1,13 @@
1
+
2
+ begin
3
+ require "#{dir = File.dirname(__FILE__)}/task/gemgem"
4
+ rescue LoadError
5
+ sh 'git submodule update --init'
6
+ exec Gem.ruby, '-S', $PROGRAM_NAME, *ARGV
7
+ end
8
+
9
+ Gemgem.init(dir) do |s|
10
+ s.name = 'struct_trans'
11
+ s.version = '0.5.0'
12
+ %w[].each{ |g| s.add_runtime_dependency(g) }
13
+ end
@@ -0,0 +1,25 @@
1
+
2
+ require 'struct_trans/hash'
3
+
4
+ module StructTrans
5
+ module_function
6
+
7
+ def transform kind, struct, schema
8
+ case schema
9
+ when Symbol
10
+ struct.public_send(schema)
11
+ when Array
12
+ schema.map do |key|
13
+ transform(kind, struct, key)
14
+ end
15
+ when Hash
16
+ schema.inject(public_send("construct_#{kind}")) do |box, (key, value)|
17
+ public_send("write_#{kind}",
18
+ box, key, transform(kind, struct.public_send(key), value))
19
+ box
20
+ end
21
+ else
22
+ raise TypeError.new("Unknown type: #{schema.class}: #{schema}")
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,18 @@
1
+
2
+
3
+
4
+ module StructTrans
5
+ module_function
6
+
7
+ def trans_hash struct, schema
8
+ transform(:hash, struct, schema)
9
+ end
10
+
11
+ def construct_hash
12
+ {}
13
+ end
14
+
15
+ def write_hash hash, key, value
16
+ hash[key] = value
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+
2
+ require 'ostruct'
3
+
4
+ module StructTrans
5
+ module_function
6
+
7
+ def trans_ostruct struct, schema
8
+ transform(:ostruct, struct, schema)
9
+ end
10
+
11
+ def construct_ostruct
12
+ OpenStruct.new
13
+ end
14
+
15
+ def write_ostruct ostruct, key, value
16
+ ostruct.public_send("#{key}=", value)
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+
2
+ require 'pork/auto'
3
+ require 'struct_trans'
@@ -0,0 +1,40 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # stub: struct_trans 0.5.0 ruby lib
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "struct_trans".freeze
6
+ s.version = "0.5.0"
7
+
8
+ s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
9
+ s.require_paths = ["lib".freeze]
10
+ s.authors = ["Lin Jen-Shin (godfat)".freeze]
11
+ s.date = "2016-11-15"
12
+ s.description = "Transform a struct with a schema to a hash, other struct, or more.".freeze
13
+ s.email = ["godfat (XD) godfat.org".freeze]
14
+ s.files = [
15
+ ".gitignore".freeze,
16
+ ".gitmodules".freeze,
17
+ ".travis.yml".freeze,
18
+ "CHANGES.md".freeze,
19
+ "Gemfile".freeze,
20
+ "README.md".freeze,
21
+ "Rakefile".freeze,
22
+ "lib/struct_trans.rb".freeze,
23
+ "lib/struct_trans/hash.rb".freeze,
24
+ "lib/struct_trans/ostruct.rb".freeze,
25
+ "lib/struct_trans/test.rb".freeze,
26
+ "struct_trans.gemspec".freeze,
27
+ "task/README.md".freeze,
28
+ "task/gemgem.rb".freeze,
29
+ "test/test_from_readme.rb".freeze,
30
+ "test/test_trans_hash.rb".freeze,
31
+ "test/test_trans_ostruct.rb".freeze]
32
+ s.homepage = "https://github.com/godfat/struct_trans".freeze
33
+ s.licenses = ["Apache License 2.0".freeze]
34
+ s.rubygems_version = "2.6.8".freeze
35
+ s.summary = "Transform a struct with a schema to a hash, other struct, or more.".freeze
36
+ s.test_files = [
37
+ "test/test_from_readme.rb".freeze,
38
+ "test/test_trans_hash.rb".freeze,
39
+ "test/test_trans_ostruct.rb".freeze]
40
+ end
@@ -0,0 +1,54 @@
1
+ # Gemgem
2
+
3
+ ## DESCRIPTION:
4
+
5
+ Provided tasks:
6
+
7
+ rake clean # Remove ignored files
8
+ rake gem:build # Build gem
9
+ rake gem:install # Install gem
10
+ rake gem:release # Release gem
11
+ rake gem:spec # Generate gemspec
12
+ rake test # Run tests in memory
13
+
14
+ ## REQUIREMENTS:
15
+
16
+ * Tested with MRI (official CRuby) 1.9.3, 2.0.0, Rubinius and JRuby.
17
+
18
+ ## INSTALLATION:
19
+
20
+ git submodule add git://github.com/godfat/gemgem.git task
21
+
22
+ And in Rakefile:
23
+
24
+ ``` ruby
25
+ begin
26
+ require "#{dir = File.dirname(__FILE__)}/task/gemgem"
27
+ rescue LoadError
28
+ sh 'git submodule update --init'
29
+ exec Gem.ruby, '-S', $PROGRAM_NAME, *ARGV
30
+ end
31
+
32
+ Gemgem.init(dir) do |s|
33
+ s.name = 'your-gem'
34
+ s.version = '0.1.0'
35
+ end
36
+ ```
37
+
38
+ ## LICENSE:
39
+
40
+ Apache License 2.0
41
+
42
+ Copyright (c) 2011-2013, Lin Jen-Shin (godfat)
43
+
44
+ Licensed under the Apache License, Version 2.0 (the "License");
45
+ you may not use this file except in compliance with the License.
46
+ You may obtain a copy of the License at
47
+
48
+ <http://www.apache.org/licenses/LICENSE-2.0>
49
+
50
+ Unless required by applicable law or agreed to in writing, software
51
+ distributed under the License is distributed on an "AS IS" BASIS,
52
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
53
+ See the License for the specific language governing permissions and
54
+ limitations under the License.
@@ -0,0 +1,316 @@
1
+
2
+ module Gemgem
3
+ class << self
4
+ attr_accessor :dir, :spec, :spec_create
5
+ end
6
+
7
+ module_function
8
+ def gem_tag ; "#{spec.name}-#{spec.version}" ; end
9
+ def gem_path ; "#{pkg_dir}/#{gem_tag}.gem" ; end
10
+ def spec_path ; "#{dir}/#{spec.name}.gemspec" ; end
11
+ def pkg_dir ; "#{dir}/pkg" ; end
12
+ def escaped_dir; @escaped_dir ||= Regexp.escape(dir); end
13
+
14
+ def init dir, &block
15
+ self.dir = dir
16
+ $LOAD_PATH.unshift("#{dir}/lib")
17
+ ENV['RUBYLIB'] = "#{dir}/lib:#{ENV['RUBYLIB']}"
18
+ ENV['PATH'] = "#{dir}/bin:#{ENV['PATH']}"
19
+ self.spec_create = block
20
+ end
21
+
22
+ def create
23
+ spec = Gem::Specification.new do |s|
24
+ s.authors = ['Lin Jen-Shin (godfat)']
25
+ s.email = ['godfat (XD) godfat.org']
26
+
27
+ s.description = description.join
28
+ s.summary = description.first
29
+ s.license = readme['LICENSE'].sub(/.+\n\n/, '').lines.first.strip
30
+
31
+ s.date = Time.now.strftime('%Y-%m-%d')
32
+ s.files = gem_files
33
+ s.test_files = test_files
34
+ s.executables = bin_files
35
+ end
36
+ spec_create.call(spec)
37
+ spec.homepage ||= "https://github.com/godfat/#{spec.name}"
38
+ self.spec = spec
39
+ end
40
+
41
+ def gem_install
42
+ require 'rubygems/commands/install_command'
43
+ # read ~/.gemrc
44
+ Gem.use_paths(Gem.configuration[:gemhome], Gem.configuration[:gempath])
45
+ Gem::Command.extra_args = Gem.configuration[:gem]
46
+
47
+ # setup install options
48
+ cmd = Gem::Commands::InstallCommand.new
49
+ cmd.handle_options([])
50
+
51
+ # install
52
+ install = Gem::Installer.new(gem_path, cmd.options)
53
+ install.install
54
+ puts "\e[35mGem installed: \e[33m#{strip_path(install.gem_dir)}\e[0m"
55
+ end
56
+
57
+ def gem_spec
58
+ create
59
+ write
60
+ end
61
+
62
+ def gem_build
63
+ require 'fileutils'
64
+ require 'rubygems/package'
65
+ gem = nil
66
+ Dir.chdir(dir) do
67
+ gem = Gem::Package.build(Gem::Specification.load(spec_path))
68
+ FileUtils.mkdir_p(pkg_dir)
69
+ FileUtils.mv(gem, pkg_dir) # gem is relative path, but might be ok
70
+ end
71
+ puts "\e[35mGem built: \e[33m#{strip_path("#{pkg_dir}/#{gem}")}\e[0m"
72
+ end
73
+
74
+ def gem_release
75
+ sh_git('tag', gem_tag)
76
+ sh_git('push')
77
+ sh_git('push', '--tags')
78
+ sh_gem('push', gem_path)
79
+ end
80
+
81
+ def gem_check
82
+ unless git('status', '--porcelain').empty?
83
+ puts("\e[35mWorking copy is not clean.\e[0m")
84
+ exit(3)
85
+ end
86
+
87
+ ver = spec.version.to_s
88
+
89
+ if ENV['VERSION'].nil?
90
+ puts("\e[35mExpected " \
91
+ "\e[33mVERSION\e[35m=\e[33m#{ver}\e[0m")
92
+ exit(1)
93
+
94
+ elsif ENV['VERSION'] != ver
95
+ puts("\e[35mExpected \e[33mVERSION\e[35m=\e[33m#{ver} " \
96
+ "\e[35mbut got\n " \
97
+ "\e[33mVERSION\e[35m=\e[33m#{ENV['VERSION']}\e[0m")
98
+ exit(2)
99
+ end
100
+ end
101
+
102
+ def test
103
+ return if test_files.empty?
104
+
105
+ if ENV['COV'] || ENV['CI']
106
+ require 'simplecov'
107
+ if ENV['CI']
108
+ begin
109
+ require 'coveralls'
110
+ SimpleCov.formatter = Coveralls::SimpleCov::Formatter
111
+ rescue LoadError => e
112
+ puts "Cannot load coveralls, skip: #{e}"
113
+ end
114
+ end
115
+ SimpleCov.start do
116
+ add_filter('test/')
117
+ add_filter('test.rb')
118
+ end
119
+ end
120
+
121
+ test_files.each{ |file| require "#{dir}/#{file[0..-4]}" }
122
+ end
123
+
124
+ def clean
125
+ return if ignored_files.empty?
126
+
127
+ require 'fileutils'
128
+ trash = File.expand_path("~/.Trash/#{spec.name}")
129
+ puts "Move the following files into: \e[35m#{strip_path(trash)}\e[33m"
130
+
131
+ ignored_files.each do |file|
132
+ from = "#{dir}/#{file}"
133
+ to = "#{trash}/#{File.dirname(file)}"
134
+ puts strip_path(from)
135
+
136
+ FileUtils.mkdir_p(to)
137
+ FileUtils.mv(from, to)
138
+ end
139
+
140
+ print "\e[0m"
141
+ end
142
+
143
+ def write
144
+ File.open(spec_path, 'w'){ |f| f << split_lines(spec.to_ruby) }
145
+ end
146
+
147
+ def split_lines ruby
148
+ ruby.gsub(/(.+?)\s*=\s*\[(.+?)\]/){ |s|
149
+ if $2.index(',')
150
+ "#{$1} = [\n #{$2.split(',').map(&:strip).join(",\n ")}]"
151
+ else
152
+ s
153
+ end
154
+ }
155
+ end
156
+
157
+ def strip_path path
158
+ strip_home_path(strip_cwd_path(path))
159
+ end
160
+
161
+ def strip_home_path path
162
+ path.sub(ENV['HOME'], '~')
163
+ end
164
+
165
+ def strip_cwd_path path
166
+ path.sub(Dir.pwd, '.')
167
+ end
168
+
169
+ def git *args
170
+ `git --git-dir=#{dir}/.git #{args.join(' ')}`
171
+ end
172
+
173
+ def sh_git *args
174
+ Rake.sh('git', "--git-dir=#{dir}/.git", *args)
175
+ end
176
+
177
+ def sh_gem *args
178
+ Rake.sh(Gem.ruby, '-S', 'gem', *args)
179
+ end
180
+
181
+ def glob path=dir
182
+ Dir.glob("#{path}/**/*", File::FNM_DOTMATCH)
183
+ end
184
+
185
+ def readme
186
+ @readme ||=
187
+ if (path = "#{Gemgem.dir}/README.md") && File.exist?(path)
188
+ ps = "##{File.read(path)}".
189
+ scan(/((#+)[^\n]+\n\n.+?(?=(\n\n\2[^#\n]+\n)|\Z))/m).map(&:first)
190
+ ps.inject('HEADER' => ps.first){ |r, s, i|
191
+ r[s[/\w+/]] = s
192
+ r
193
+ }
194
+ else
195
+ {}
196
+ end
197
+ end
198
+
199
+ def description
200
+ # JRuby String#lines is returning an enumerator
201
+ @description ||= (readme['DESCRIPTION']||'').sub(/.+\n\n/, '').lines.to_a
202
+ end
203
+
204
+ def all_files
205
+ @all_files ||= fold_files(glob).sort
206
+ end
207
+
208
+ def fold_files files
209
+ files.inject([]){ |r, path|
210
+ if File.file?(path) && path !~ %r{/\.git(/|$)} &&
211
+ (rpath = path[%r{^#{escaped_dir}/(.*$)}, 1])
212
+ r << rpath
213
+ elsif File.symlink?(path) # walk into symlinks...
214
+ r.concat(fold_files(glob(File.expand_path(path,
215
+ File.readlink(path)))))
216
+ else
217
+ r
218
+ end
219
+ }
220
+ end
221
+
222
+ def gem_files
223
+ @gem_files ||= all_files.reject{ |f|
224
+ f =~ ignored_pattern && !git_files.include?(f)
225
+ }
226
+ end
227
+
228
+ def test_files
229
+ @test_files ||= gem_files.grep(%r{^test/(.+?/)*test_.+?\.rb$})
230
+ end
231
+
232
+ def bin_files
233
+ @bin_files ||= gem_files.grep(%r{^bin/}).map{ |f| File.basename(f) }
234
+ end
235
+
236
+ def git_files
237
+ @git_files ||= if File.exist?("#{dir}/.git")
238
+ git('ls-files').split("\n")
239
+ else
240
+ []
241
+ end
242
+ end
243
+
244
+ def ignored_files
245
+ @ignored_files ||= all_files.grep(ignored_pattern)
246
+ end
247
+
248
+ def ignored_pattern
249
+ @ignored_pattern ||= if gitignore.empty?
250
+ /^$/
251
+ else
252
+ Regexp.new(expand_patterns(gitignore).join('|'))
253
+ end
254
+ end
255
+
256
+ def expand_patterns pathes
257
+ # http://git-scm.com/docs/gitignore
258
+ pathes.flat_map{ |path|
259
+ # we didn't implement negative pattern for now
260
+ Regexp.escape(path).sub(%r{^/}, '^').gsub(/\\\*/, '[^/]*')
261
+ }
262
+ end
263
+
264
+ def gitignore
265
+ @gitignore ||= if File.exist?(path = "#{dir}/.gitignore")
266
+ File.read(path).lines.
267
+ reject{ |l| l == /^\s*(#|\s+$)/ }.map(&:strip)
268
+ else
269
+ []
270
+ end
271
+ end
272
+ end
273
+
274
+ namespace :gem do
275
+
276
+ desc 'Install gem'
277
+ task :install => [:build] do
278
+ Gemgem.gem_install
279
+ end
280
+
281
+ desc 'Build gem'
282
+ task :build => [:spec] do
283
+ Gemgem.gem_build
284
+ end
285
+
286
+ desc 'Generate gemspec'
287
+ task :spec do
288
+ Gemgem.gem_spec
289
+ end
290
+
291
+ desc 'Release gem'
292
+ task :release => [:spec, :check, :build] do
293
+ Gemgem.gem_release
294
+ end
295
+
296
+ task :check do
297
+ Gemgem.gem_check
298
+ end
299
+
300
+ end # of gem namespace
301
+
302
+ desc 'Run tests'
303
+ task :test do
304
+ Gemgem.test
305
+ end
306
+
307
+ desc 'Trash ignored files'
308
+ task :clean => ['gem:spec'] do
309
+ Gemgem.clean
310
+ end
311
+
312
+ task :default do
313
+ # Is there a reliable way to do this in the current process?
314
+ # It failed miserably before between Rake versions...
315
+ exec "#{Gem.ruby} -S #{$PROGRAM_NAME} -f #{Rake.application.rakefile} -T"
316
+ end
@@ -0,0 +1,26 @@
1
+
2
+ require 'struct_trans/test'
3
+
4
+ describe 'from README.md' do
5
+ readme = File.read("#{__dir__}/../README.md")
6
+ codes = readme.scan(/``` ruby(.+?)```/m).map(&:first)
7
+
8
+ Context = Class.new{
9
+ def results; @results ||= []; end
10
+ def p res ; results << res ; end
11
+
12
+ def verify expects
13
+ results.zip(expects).each do |(res, exp)|
14
+ res.should.eq instance_eval(exp)
15
+ end
16
+ end
17
+ }
18
+
19
+ codes.each.with_index do |code, index|
20
+ would 'pass from README.md #%02d' % index do
21
+ context = Context.new
22
+ context.instance_eval(code, 'README.md', 0)
23
+ context.verify(code.scan(/# (.+)/).map(&:first))
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,50 @@
1
+
2
+ require 'struct_trans/test'
3
+
4
+ describe 'StructTrans.trans_hash' do
5
+ def verify struct, schema, result
6
+ expect(StructTrans.trans_hash(struct, schema)).eq result
7
+ end
8
+
9
+ would 'raise TypeError for bad type' do
10
+ expect.raise(TypeError) do
11
+ StructTrans.trans_hash('nnf', 1)
12
+ end
13
+ end
14
+
15
+ would 'transform with symbol' do
16
+ verify('nnf', :reverse, 'fnn')
17
+ end
18
+
19
+ would 'transform with array' do
20
+ verify('nnf', [:reverse, :capitalize], %w[fnn Nnf])
21
+ end
22
+
23
+ would 'transform with hash' do
24
+ verify('nnf', {:reverse => :capitalize}, {:reverse => 'Fnn'})
25
+ end
26
+
27
+ would 'transform with array containing hashes' do
28
+ verify('nnf',
29
+ [:reverse, {:reverse => :capitalize}],
30
+ ['fnn', :reverse => 'Fnn'])
31
+ end
32
+
33
+ would 'transform with array containing hashes containing arrays' do
34
+ verify('nnf',
35
+ [:reverse, {:reverse => [:capitalize, :upcase]}],
36
+ ['fnn', :reverse => ['Fnn', 'FNN']])
37
+ end
38
+
39
+ would 'transform with array containing hashes containing hashes' do
40
+ verify('nnf',
41
+ [:reverse, {:reverse => {:capitalize => :swapcase}}],
42
+ ['fnn', :reverse => {:capitalize => 'fNN'}])
43
+ end
44
+
45
+ would 'transform with array containing hashes containing mixed arrays' do
46
+ verify('nnf',
47
+ [:inspect, {:reverse => [{:capitalize => :swapcase}, :upcase]}],
48
+ ['"nnf"', {:reverse => [{:capitalize => 'fNN'}, 'FNN']}])
49
+ end
50
+ end
@@ -0,0 +1,65 @@
1
+
2
+ require 'struct_trans/test'
3
+ require 'struct_trans/ostruct'
4
+
5
+ describe 'StructTrans.trans_ostruct' do
6
+ def trans struct, schema
7
+ StructTrans.trans_ostruct(struct, schema)
8
+ end
9
+
10
+ would 'raise TypeError for bad type' do
11
+ expect.raise(TypeError) do
12
+ StructTrans.trans_ostruct('nnf', 1)
13
+ end
14
+ end
15
+
16
+ would 'transform with symbol' do
17
+ o = trans('nnf', :reverse)
18
+
19
+ expect(o).eq 'fnn'
20
+ end
21
+
22
+ would 'transform with array' do
23
+ o = trans('nnf', [:reverse, :capitalize])
24
+
25
+ expect(o[0]).eq 'fnn'
26
+ expect(o[1]).eq 'Nnf'
27
+ end
28
+
29
+ would 'transform with hash' do
30
+ o = trans('nnf', {:reverse => :capitalize})
31
+
32
+ expect(o.reverse).eq 'Fnn'
33
+ end
34
+
35
+ would 'transform with array containing hashes' do
36
+ o = trans('nnf', [:reverse, {:reverse => :capitalize}])
37
+
38
+ expect(o[0]).eq 'fnn'
39
+ expect(o[1].reverse).eq 'Fnn'
40
+ end
41
+
42
+ would 'transform with array containing hashes containing arrays' do
43
+ o = trans('nnf', [:reverse, {:reverse => [:capitalize, :upcase]}])
44
+
45
+ expect(o[0]).eq 'fnn'
46
+ expect(o[1].reverse[0]).eq 'Fnn'
47
+ expect(o[1].reverse[1]).eq 'FNN'
48
+ end
49
+
50
+ would 'transform with array containing hashes containing hashes' do
51
+ o = trans('nnf', [:reverse, {:reverse => {:capitalize => :swapcase}}])
52
+
53
+ expect(o[0]).eq 'fnn'
54
+ expect(o[1].reverse.capitalize).eq 'fNN'
55
+ end
56
+
57
+ would 'transform with array containing hashes containing mixed arrays' do
58
+ o = trans('nnf',
59
+ [:inspect, {:reverse => [{:capitalize => :swapcase}, :upcase]}])
60
+
61
+ expect(o[0]).eq '"nnf"'
62
+ expect(o[1].reverse[0].capitalize).eq 'fNN'
63
+ expect(o[1].reverse[1]).eq 'FNN'
64
+ end
65
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: struct_trans
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ platform: ruby
6
+ authors:
7
+ - Lin Jen-Shin (godfat)
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-11-15 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Transform a struct with a schema to a hash, other struct, or more.
14
+ email:
15
+ - godfat (XD) godfat.org
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".gitmodules"
22
+ - ".travis.yml"
23
+ - CHANGES.md
24
+ - Gemfile
25
+ - README.md
26
+ - Rakefile
27
+ - lib/struct_trans.rb
28
+ - lib/struct_trans/hash.rb
29
+ - lib/struct_trans/ostruct.rb
30
+ - lib/struct_trans/test.rb
31
+ - struct_trans.gemspec
32
+ - task/README.md
33
+ - task/gemgem.rb
34
+ - test/test_from_readme.rb
35
+ - test/test_trans_hash.rb
36
+ - test/test_trans_ostruct.rb
37
+ homepage: https://github.com/godfat/struct_trans
38
+ licenses:
39
+ - Apache License 2.0
40
+ metadata: {}
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 2.6.8
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Transform a struct with a schema to a hash, other struct, or more.
61
+ test_files:
62
+ - test/test_from_readme.rb
63
+ - test/test_trans_hash.rb
64
+ - test/test_trans_ostruct.rb