json-mapping 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
+ SHA256:
3
+ metadata.gz: 36115b277de00edeac034d137cd1c01260e6eaf481b43ff6fe92876c09f0dba0
4
+ data.tar.gz: 07bc837a965f0118fc0b3d5fd90e079e311df65db3298dcb9f52cd4b66d7eee6
5
+ SHA512:
6
+ metadata.gz: b7f016a61fdec360c354ee27a0d68cc15968e81cf124d5c3150df37f3c672cfb66ee5105601e2ba88c40d17ae11cbd504d89b93c8b710267f72b2fb12ae87d95
7
+ data.tar.gz: b84fc2b1f140bc7a9d4e1b2e197f757f5668c9869a61aaa2271815544088e7df7a7c2b80aa5370a10c29d2b4b55eab2f340d56c4a310a49ef6a43a1b503046fb
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ # Minor Release v0.1.0 (2019-10-23)
2
+ * **Kjarrigan**
3
+ * Basic mapping works { field_name => type }
4
+
5
+
6
+ *Released by Kjarrigan <github@kjarrigan.de>*
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ group :development do
6
+ gem 'rspec', '~> 3.0'
7
+ gem 'simplecov'
8
+ end
9
+
10
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Kjarrigan
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,36 @@
1
+ # JSON.mapping
2
+
3
+ A simple JSON mapper inspired by [Crystals](https://crystal-lang.org/api/JSON.html) `JSON.mapping`
4
+
5
+ ## Usage
6
+
7
+ ```ruby
8
+ require "json"
9
+
10
+ class Location
11
+ JSON.mapping(
12
+ lat: Float64,
13
+ lng: Float64,
14
+ )
15
+ end
16
+
17
+ class House
18
+ JSON.mapping(
19
+ address: String,
20
+ location: {type: Location, nilable: true},
21
+ )
22
+ end
23
+
24
+ house = House.from_json(%({"address": "Crystal Road 1234", "location": {"lat": 12.3, "lng": 34.5}}))
25
+ house.address # => "Crystal Road 1234"
26
+ house.location # => #<Location:0x10cd93d80 @lat=12.3, @lng=34.5>
27
+ house.to_json # => %({"address":"Crystal Road 1234","location":{"lat":12.3,"lng":34.5}})
28
+ ```
29
+
30
+ ## Contributing
31
+
32
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Kjarrigan/json-mapping.
33
+
34
+ ## License
35
+
36
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'json/mapping/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'json-mapping'
9
+ spec.version = JSON::Mapping::VERSION
10
+ spec.authors = ['Kjarrigan']
11
+ spec.email = ['github@kjarrigan.de']
12
+
13
+ spec.summary = 'Define ruby objects to directly parse from JSON.'
14
+ spec.description = "Inspired by Crystal's JSON.mapping."
15
+ spec.homepage = 'https://github.com/kjarrigan/json-mapping'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'json/mapping/version'
5
+
6
+ module JSON
7
+ class Mapping
8
+ class Error < StandardError; end
9
+ end
10
+
11
+ def self.mapping(klass, fields)
12
+ fields.each do |name, typ|
13
+ klass.attr_reader name
14
+ klass.define_method "#{name}=" do |value|
15
+ value = typ.new(value) if typ.respond_to?(:from_json)
16
+ unless value.is_a?(typ)
17
+ raise JSON::Mapping::Error, "Wrong field type #{self.class} for '#{name}' - should be '#{typ}'"
18
+ end
19
+
20
+ instance_variable_set "@#{name}", value
21
+ end
22
+ end
23
+
24
+ klass.define_method :initialize do |params = {}|
25
+ params.each do |name, value|
26
+ unless fields.key?(name.to_sym)
27
+ raise JSON::Mapping::Error, "Missing field definition for '#{name}'"
28
+ end
29
+
30
+ send("#{name}=", value)
31
+ end
32
+ end
33
+
34
+ klass.define_singleton_method :from_json do |string|
35
+ klass.new(JSON.parse(string))
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JSON
4
+ class Mapping
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json-mapping
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kjarrigan
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-10-23 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Inspired by Crystal's JSON.mapping.
14
+ email:
15
+ - github@kjarrigan.de
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".rspec"
22
+ - CHANGELOG.md
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - json-mapping.gemspec
28
+ - lib/json/mapping.rb
29
+ - lib/json/mapping/version.rb
30
+ homepage: https://github.com/kjarrigan/json-mapping
31
+ licenses:
32
+ - MIT
33
+ metadata: {}
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubygems_version: 3.0.4
50
+ signing_key:
51
+ specification_version: 4
52
+ summary: Define ruby objects to directly parse from JSON.
53
+ test_files: []