objectification 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 57d0434a15d37d0ccc744e3cbbcd5e9e95587813
4
+ data.tar.gz: 2e004c692616bf790d60812d1b8cee98057d49d9
5
+ SHA512:
6
+ metadata.gz: 8239451c46f2261afcd72b71799b53eee0ab9c693a2dab85936a44c3eaa7a127aabecedc706254e30f8a9b3cace37849ae0361f55addc8234def14b6d339dc6e
7
+ data.tar.gz: 26258c4ff814efe8016154b482aae208cbebbfdaa97d4db89e7af27b9358ac717dd94d27204ca47ae8d8945972a41f1fa594fa50c0ba61f12c97d781dd95ac86
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,21 @@
1
+
2
+ The MIT License (MIT)
3
+ Copyright © 2016 Chris Olstrom <chris@olstrom.com>
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.
@@ -0,0 +1,28 @@
1
+ #+TITLE: Objectification
2
+ #+SUBTITLE: Arbitrary Objects from Hashes
3
+
4
+ * Installation
5
+
6
+ #+BEGIN_SRC shell
7
+ gem install objectification
8
+ #+END_SRC
9
+
10
+ * Usage
11
+
12
+ #+BEGIN_SRC ruby
13
+ require 'objectification'
14
+
15
+ some_hash = { 'foo' => 'bar' }
16
+ some_object = Objectification::FromHash.new some_hash
17
+ some_object.foo # => 'bar'
18
+ #+END_SRC
19
+
20
+ * License
21
+
22
+ [[https://tldrlegal.com/license/mit-license][MIT License]]
23
+
24
+ See ~LICENSE.txt~ for the full text.
25
+
26
+ * Contributors
27
+
28
+ - [[https://colstrom.github.io/][Chris Olstrom]] | [[mailto:chris@olstrom.com][e-mail]] | [[https://twitter.com/ChrisOlstrom][Twitter]]
@@ -0,0 +1,7 @@
1
+ ---
2
+ a_hash:
3
+ inner_string: inner
4
+ a_string: outer
5
+ an_array:
6
+ - value-a
7
+ - value-b
@@ -0,0 +1,5 @@
1
+ require_relative 'objectification/from_hash'
2
+
3
+ module Objectification
4
+ VERSION = '0.1.0'.freeze
5
+ end
@@ -0,0 +1,37 @@
1
+ require_relative 'refinements/hash'
2
+
3
+ module Objectification
4
+ class FromHash
5
+ using Hash_types
6
+
7
+ IGNORE = %w(to_h).freeze
8
+
9
+ def initialize(properties = {})
10
+ properties.each_pair do |key, value|
11
+ self.class.method(:attr_accessor).call key
12
+ instance_variable_set "@#{key}", value
13
+ end
14
+ self
15
+ end
16
+
17
+ def to_h
18
+ instance_methods.each_with_object({}) do |instance_method, hash|
19
+ hash[instance_method] = method(instance_method).call
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def instance_methods
26
+ ((self.class.instance_methods - self.class.private_methods) - Object.instance_methods)
27
+ .map(&:to_s)
28
+ .reject { |m| IGNORE.include? m }
29
+ .reject { |m| m.start_with? '__' }
30
+ .reject { |m| m.end_with? '=' }
31
+ end
32
+
33
+ def types
34
+ to_h.types
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,17 @@
1
+ module Hash_hmap
2
+ refine Hash do
3
+ def hmap(&block)
4
+ Hash[self.map { |k,v| block.call k,v }]
5
+ end
6
+ end
7
+ end
8
+
9
+ module Hash_types
10
+ refine Hash do
11
+ using Hash_hmap
12
+
13
+ def types
14
+ self.hmap { |k, v| [k, v.class.name] }
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ require_relative 'lib/objectification'
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = 'objectification'
5
+ gem.version = Objectification::VERSION
6
+ gem.license = 'MIT'
7
+ gem.summary = 'Builds arbitrary objects from hashes'
8
+
9
+ gem.author = 'Chris Olstrom'
10
+ gem.email = 'chris@olstrom.com'
11
+
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
14
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ end
@@ -0,0 +1,15 @@
1
+ require 'kintama'
2
+ require 'yaml'
3
+ require_relative '../lib/objectification'
4
+
5
+ hash_fixture = Hash[YAML.load_file('fixtures/hash.yaml').sort]
6
+
7
+ context Objectification::FromHash do
8
+ setup do
9
+ @object = Objectification::FromHash.new hash_fixture
10
+ end
11
+
12
+ should 'not deform the original data' do
13
+ assert_equal @object.to_h, hash_fixture
14
+ end
15
+ end
@@ -0,0 +1,10 @@
1
+ require 'riot'
2
+ require 'yaml'
3
+ require_relative '../lib/objectification'
4
+
5
+ hash_fixture = Hash[YAML.load_file('fixtures/hash.yaml').sort]
6
+
7
+ context Objectification::FromHash do
8
+ setup { Objectification::FromHash.new hash_fixture }
9
+ asserts(:to_h).equals(hash_fixture)
10
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: objectification
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Chris Olstrom
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: chris@olstrom.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - Gemfile
20
+ - LICENSE.txt
21
+ - README.org
22
+ - fixtures/hash.yaml
23
+ - lib/objectification.rb
24
+ - lib/objectification/from_hash.rb
25
+ - lib/objectification/refinements/hash.rb
26
+ - objectification.gemspec
27
+ - test/from_hash_kintama.rb
28
+ - test/from_hash_riot.rb
29
+ homepage:
30
+ licenses:
31
+ - MIT
32
+ metadata: {}
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project:
49
+ rubygems_version: 2.5.1
50
+ signing_key:
51
+ specification_version: 4
52
+ summary: Builds arbitrary objects from hashes
53
+ test_files:
54
+ - test/from_hash_kintama.rb
55
+ - test/from_hash_riot.rb
56
+ has_rdoc: