open_struct_factory 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7cd3524565084916d8e9c402b6cbf5bb8786908b
4
+ data.tar.gz: 3c104557a0dddb00aa8086450c6a00446222c2c4
5
+ SHA512:
6
+ metadata.gz: 0b5cb9566a446365fc4f60a33271dbcbeacb16078839e7fed947812484fb33b73f863ef4410e364804e28a9aabb6bfc43785fd50f5c77f4d2bb46ef996d4517b
7
+ data.tar.gz: ba54ae59b50d67c7fe09aa7b556e0f5054b5af79a01bee242a8f9e42b40164351e383e2dd50c19f5a4422a09235cfa7f57146082290fa159b8711578d10db172
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in open_struct_factory.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Xavier Defrang
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # OpenStructFactory
2
+
3
+ A factory to create `OpenStruct` objects from nested hashes and arrays.
4
+
5
+ The main difference with other gems such as [recursive-open-struct](https://github.com/aetherknight/recursive-open-struct) is that this library is entirely decoupled: it doesn't inherit from `OpenStruct` and doesn't rely on any core extensions.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'open_struct_factory'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install open_struct_factory
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+
25
+ require 'ostruct'
26
+ require 'open_struct_factory'
27
+
28
+ book_data = {
29
+ title: "The World as Will and Representation",
30
+ author: {
31
+ name: "Arthur Schopenhauer",
32
+ birth_date: "1788-02-22",
33
+ birth_place: "Danzig",
34
+ },
35
+ publications: [
36
+ {edition: "1st edition", year: 1818},
37
+ {edition: "2nd expanded edition", year: 1844},
38
+ ]
39
+ }
40
+
41
+ book = OpenStructFactory.create(book_data)
42
+
43
+ puts book.title
44
+ # => The World as Will and Representation
45
+
46
+ puts book.author.name
47
+ # => Arthur Schopenhauer
48
+
49
+ puts book.publications[1].year
50
+ # => 1844
51
+
52
+ ```
53
+
54
+ A block can optionally be passed, it will be called for each key and give you the opportunity to sanitize the property name to be added to the `OpenStruct`:
55
+
56
+ ```ruby
57
+
58
+ data = {
59
+ "SomePeople" => "LoveTheirCamelCase",
60
+ }
61
+
62
+ obj = OpenStructFactory.create(data) { |key| key.downcase }
63
+
64
+ puts obj.somepeople
65
+ # => LoveTheirCamelCase
66
+
67
+ ```
68
+
69
+
70
+ ## Contributing
71
+
72
+ 1. Fork it ( https://github.com/[my-github-username]/open_struct_factory/fork )
73
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
74
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
75
+ 4. Push to the branch (`git push origin my-new-feature`)
76
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,3 @@
1
+ module OpenStructFactory
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,31 @@
1
+ require "open_struct_factory/version"
2
+
3
+ module OpenStructFactory
4
+
5
+ # Creates an OpenStruct from the given hash
6
+ # * It recursively transform nested hashes into OpenStruct
7
+ # * It recursively goes through arrays, transforming hashes into OpenStruct
8
+ # * An optional block can be passed to process the hash keys
9
+ def self.create(hash, &block) # :yields: key
10
+ properties = {}
11
+ hash.each do |key, value|
12
+ property_name = block_given? ? (yield key) : key
13
+ properties[property_name] = process_value(value, &block)
14
+ end
15
+ OpenStruct.new(properties)
16
+ end
17
+
18
+ private
19
+
20
+ def self.process_value(value, &block)
21
+ case value
22
+ when Hash
23
+ create(value, &block)
24
+ when Array
25
+ value.map { |v| process_value(v, &block) }
26
+ else
27
+ value
28
+ end
29
+ end
30
+
31
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'open_struct_factory/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "open_struct_factory"
8
+ spec.version = OpenStructFactory::VERSION
9
+ spec.authors = ["Xavier Defrang"]
10
+ spec.email = ["xavier.defrang@gmail.com"]
11
+ spec.summary = %q{A factory to create OpenStruct objects from nested hashes and arrays}
12
+ spec.description = %q{A factory to create OpenStruct objects from nested hashes and arrays}
13
+ spec.homepage = "http://github.com/xavier/open_struct_factory"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,118 @@
1
+ require "spec_helper"
2
+
3
+ describe OpenStructFactory do
4
+
5
+ describe ".create" do
6
+
7
+ context "when given a simple hash" do
8
+
9
+ let(:hash) do
10
+ {:foo => "bar", "baz" => 123}
11
+ end
12
+
13
+ it "creates an OpenStruct" do
14
+ os = OpenStructFactory.create(hash)
15
+ expect(os.foo).to eq("bar")
16
+ expect(os.baz).to eq(123)
17
+ end
18
+
19
+ end
20
+
21
+ context "when given a hash with a single level of nesting" do
22
+
23
+ let(:hash) do
24
+ {
25
+ :foo => "bar",
26
+ :baz => 123,
27
+ "nested" => {
28
+ :qux => true,
29
+ }
30
+ }
31
+ end
32
+
33
+ it "creates a nested OpenStruct" do
34
+ os = OpenStructFactory.create(hash)
35
+ expect(os.foo).to eq("bar")
36
+ expect(os.baz).to eq(123)
37
+ expect(os.nested.qux).to eq(true)
38
+ end
39
+
40
+ end
41
+
42
+ context "when given a hash with several levels of nesting" do
43
+
44
+ let(:hash) do
45
+ {
46
+ :foo => {
47
+ :bar => {
48
+ "baz" => {
49
+ :qux => true
50
+ }
51
+ }
52
+ }
53
+ }
54
+ end
55
+
56
+ it "creates a nested OpenStruct" do
57
+ os = OpenStructFactory.create(hash)
58
+ expect(os.foo.bar.baz.qux).to eq(true)
59
+ end
60
+
61
+ end
62
+
63
+ context "when given a hash containing an array of non-aggregates" do
64
+
65
+ let(:hash) do
66
+ {:foo => "bar", :baz => [1, 2, "three"]}
67
+ end
68
+
69
+ it "creates an OpenStruct" do
70
+ os = OpenStructFactory.create(hash)
71
+ expect(os.foo).to eq("bar")
72
+ expect(os.baz).to eq([1, 2, "three"])
73
+ end
74
+
75
+ end
76
+
77
+ context "when given a hash containing an array of hashes" do
78
+
79
+ let(:hash) do
80
+ {
81
+ :foo => [
82
+ {:bar => 1},
83
+ {:baz => {:qux => true}},
84
+ ]
85
+ }
86
+ end
87
+
88
+ it "creates a nested OpenStruct" do
89
+ os = OpenStructFactory.create(hash)
90
+ expect(os.foo[0].bar).to eq(1)
91
+ expect(os.foo[1].baz.qux).to eq(true)
92
+ end
93
+
94
+ end
95
+
96
+ context "when given a block" do
97
+
98
+ let(:hash) do
99
+ {
100
+ "FOO" => 123,
101
+ "Bar" => {
102
+ "BAZ" => {
103
+ "qux" => true
104
+ }
105
+ }
106
+ }
107
+ end
108
+
109
+ it "creates a nested OpenStruct with property names processed by the given block" do
110
+ os = OpenStructFactory.create(hash) { |property_name| property_name.downcase }
111
+ expect(os.foo).to eq(123)
112
+ expect(os.bar.baz.qux).to eq(true)
113
+ end
114
+ end
115
+
116
+ end # .create
117
+
118
+ end
@@ -0,0 +1,2 @@
1
+ require "ostruct"
2
+ require_relative "../lib/open_struct_factory"
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: open_struct_factory
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Xavier Defrang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-10 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A factory to create OpenStruct objects from nested hashes and arrays
56
+ email:
57
+ - xavier.defrang@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/open_struct_factory.rb
69
+ - lib/open_struct_factory/version.rb
70
+ - open_struct_factory.gemspec
71
+ - spec/open_struct_factory_spec.rb
72
+ - spec/spec_helper.rb
73
+ homepage: http://github.com/xavier/open_struct_factory
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.2.2
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: A factory to create OpenStruct objects from nested hashes and arrays
97
+ test_files:
98
+ - spec/open_struct_factory_spec.rb
99
+ - spec/spec_helper.rb