virtus_convert 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5c5c381b93ea5fe77c01bd3a067ff5e681ddbdc1
4
+ data.tar.gz: acc0e6f24a7f73a006f312621321887f20cd8c3a
5
+ SHA512:
6
+ metadata.gz: 25054f9067fa85c24abbd263d06f36f856dc45d18d14f05d169e92176c99a8bbaab7ce576f8f8c43fa4b424523659f754e354dcb5f24c29e4e79f11f270fff54
7
+ data.tar.gz: 1396620431464bba748bda26d8932a2680a342c345d2b69eaf284b73867156aa31f6e1463b4eec03535cdf7132c70bfa88ff693593d3190b0b98aec49ae4e8c2
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ /*--format documentation*/
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in virtus_convert.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Benjamin Guest
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # VirtusConvert
2
+
3
+ VirtusConvert allows you to convert a deeply nested tree of Virtus\* or other objects that respond
4
+ to attributes to nested tree of hashes and arrays
5
+
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'virtus_convert'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install virtus_convert
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ jack = Person.new(name:'Jack', address:'123 Fake Street' )
27
+ order = Order.new(customer: jack, total:1_000_000)
28
+
29
+ VirtusConvert.new(order).to_hash #=> {customer:{name:'Jack', address:'123 Fake Street'}, total: 1000000}
30
+ ```
31
+
32
+ Look in the specs/ folder for more example use cases
33
+
34
+ ## Development
35
+
36
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
37
+
38
+ ## Contributing
39
+
40
+ 1. Fork it ( https://github.com/[my-github-username]/virtus_convert/fork )
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 2. Write Tests!
43
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task :default => :spec
6
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "virtus_convert"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,12 @@
1
+ module VirtusConvert
2
+ class Array
3
+ def initialize(array = [])
4
+ @array = array.map{|item| VirtusConvert.new(item)}
5
+ end
6
+
7
+ def to_hash
8
+ @array.map(&:to_hash)
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,21 @@
1
+ require 'virtus_convert/object'
2
+ require 'virtus_convert/hash'
3
+ require 'virtus_convert/array'
4
+
5
+ module VirtusConvert
6
+
7
+ class Converter
8
+
9
+ def initialize(unknown)
10
+ @root = VirtusConvert::Hash.new(unknown) if unknown.is_a? ::Hash
11
+ @root ||= VirtusConvert::Array.new(unknown) if unknown.is_a? ::Array
12
+ @root ||= VirtusConvert::Object.new(unknown)
13
+ end
14
+
15
+ def to_hash
16
+ @root.respond_to?(:to_hash) ? @root.to_hash : @root
17
+ end
18
+
19
+ end
20
+
21
+ end
@@ -0,0 +1,16 @@
1
+ module VirtusConvert
2
+ class Hash
3
+ def initialize(hash = {})
4
+ @hash = hash.inject({}){|h,(k,v)| h[k] = VirtusConvert.new(v); h}
5
+ end
6
+
7
+ def to_hash
8
+ @hash.inject({}) do |hash,(k, v)|
9
+ hash[k] = (v.respond_to?(:to_hash) ? v.to_hash : v)
10
+ hash
11
+ end
12
+ end
13
+ end
14
+
15
+ end
16
+
@@ -0,0 +1,15 @@
1
+ module VirtusConvert
2
+ class Object
3
+ def initialize(object)
4
+ if object.respond_to?(:attributes)
5
+ @object = VirtusConvert.new(object.attributes)
6
+ else
7
+ @object = object
8
+ end
9
+ end
10
+
11
+ def to_hash
12
+ @object.respond_to?(:to_hash) ? @object.to_hash : @object
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module VirtusConvert
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,8 @@
1
+ require "virtus_convert/version"
2
+ require 'virtus_convert/converter'
3
+
4
+ module VirtusConvert
5
+ def self.new(unknown)
6
+ VirtusConvert::Converter.new(unknown)
7
+ end
8
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'virtus_convert/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "virtus_convert"
8
+ spec.version = VirtusConvert::VERSION
9
+ spec.authors = ["Benjamin Guest"]
10
+ spec.email = ["benguest@gmail.com"]
11
+
12
+ spec.summary = %q{Converts nested object trees to hashes / arrays}
13
+ spec.description = %q{Convert deeply nested Virtus or other objects that respond to attributes to hashes and arrays}
14
+ spec.homepage = "https://github.com/bguest/virtus_convert"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.8"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency 'pry-byebug', '~>3.0'
25
+ spec.add_development_dependency 'rspec', '~>3.2'
26
+ spec.add_development_dependency 'simplecov', '~>0.9.2'
27
+ spec.add_development_dependency 'dotenv', '~>2.0'
28
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: virtus_convert
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Benjamin Guest
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-03-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.9.2
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.9.2
83
+ - !ruby/object:Gem::Dependency
84
+ name: dotenv
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '2.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '2.0'
97
+ description: Convert deeply nested Virtus or other objects that respond to attributes
98
+ to hashes and arrays
99
+ email:
100
+ - benguest@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".travis.yml"
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - lib/virtus_convert.rb
115
+ - lib/virtus_convert/array.rb
116
+ - lib/virtus_convert/converter.rb
117
+ - lib/virtus_convert/hash.rb
118
+ - lib/virtus_convert/object.rb
119
+ - lib/virtus_convert/version.rb
120
+ - virtus_convert.gemspec
121
+ homepage: https://github.com/bguest/virtus_convert
122
+ licenses:
123
+ - MIT
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubyforge_project:
141
+ rubygems_version: 2.4.6
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: Converts nested object trees to hashes / arrays
145
+ test_files: []