karta 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +6 -0
- data/README.md +6 -0
- data/lib/karta.rb +7 -5
- data/lib/karta/mapper.rb +8 -3
- data/lib/karta/version.rb +1 -1
- data/spec/karta/mapper_spec.rb +22 -6
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1be053bb9f20ca326f9ff6f90275dbdb8022ea25
|
4
|
+
data.tar.gz: 0aac93a6a00c5c8ce8e03c1de4f0918aa2f56652
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1b9be84401aa3a41075d57ffedea7f540adba58baa7af5c9a7eca4985a98a0b29ada4443c704925d3f636fbcdff6fd7d28e6e1bd56b524522f98fad9b5a08ba3
|
7
|
+
data.tar.gz: 97b00b3fa92575aa3bb60ef10fb620356cf6d1c192671ce4c21eb068c3980394fc62ffbbd4ba4d18004b3ffabe870a428b48044fda700ba5d8c599d3dcb6982a
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,4 +1,10 @@
|
|
1
1
|
# Karta
|
2
|
+
[![GitHub](http://img.shields.io/badge/github-samuel02/karta-blue.svg)](http://github.com/samuel02/karta)
|
3
|
+
[![Documentation](http://img.shields.io/badge/docs-rdoc.info-blue.svg)](http://rubydoc.org/gems/karta/frames)
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/karta.svg)](https://badge.fury.io/rb/karta)
|
5
|
+
[![Build Status](https://travis-ci.org/samuel02/karta.svg?branch=master)](https://travis-ci.org/samuel02/karta)
|
6
|
+
[![Code Climate](https://codeclimate.com/github/samuel02/karta/badges/gpa.svg)](https://codeclimate.com/github/samuel02/karta)
|
7
|
+
[![License](http://img.shields.io/badge/license-MIT-yellowgreen.svg)](#license)
|
2
8
|
|
3
9
|
Karta is a very light-weight Ruby library that makes it easy to create mapper objects in a Ruby application. The mapper object makes it easy to map or transform one object into an other. The main use case is to transform data objects from an domain to another domain, e.g. map a `Twitter::User` to `User`. Instead of having to, for example, define a method `#from_twitter_user` on the `User` class which sets all attributes correctly a mapper object is created which defines all mappings.
|
4
10
|
|
data/lib/karta.rb
CHANGED
@@ -38,10 +38,7 @@ module Karta
|
|
38
38
|
#
|
39
39
|
# @return [Object] a new instance of the same type as `to`
|
40
40
|
def self.map(from:, to:)
|
41
|
-
|
42
|
-
|
43
|
-
mapper_registry.find(from_klass: from_klass, to_klass: to_klass)
|
44
|
-
.map(from: from, to: to)
|
41
|
+
_map(from, to, :map)
|
45
42
|
end
|
46
43
|
|
47
44
|
# Map an object to another using a registered mapper. Performs the mapping
|
@@ -54,10 +51,15 @@ module Karta
|
|
54
51
|
#
|
55
52
|
# @return [Object] returns modified version of 'to'
|
56
53
|
def self.map!(from:, to:)
|
54
|
+
_map(from, to, :map!)
|
55
|
+
end
|
56
|
+
|
57
|
+
# @api private
|
58
|
+
def self._map(from, to, map_method)
|
57
59
|
to, to_klass, from, from_klass = *_handle_map_args(from, to)
|
58
60
|
|
59
61
|
mapper_registry.find(from_klass: from_klass, to_klass: to_klass)
|
60
|
-
.
|
62
|
+
.send(map_method, from: from, to: to)
|
61
63
|
end
|
62
64
|
|
63
65
|
# @api private
|
data/lib/karta/mapper.rb
CHANGED
@@ -43,9 +43,14 @@ module Karta
|
|
43
43
|
#
|
44
44
|
# A one-to-one-mapping is a mapping where the attribute names are equal
|
45
45
|
# and no transformation is supposed to take place. E.g. `foo.id = bar.id`.
|
46
|
-
|
47
|
-
|
48
|
-
|
46
|
+
#
|
47
|
+
# @overload one_to_one_mapping(attr, ...)
|
48
|
+
# @param attr [Symbol] attribute to define a one-to-one mapping for
|
49
|
+
def self.one_to_one_mapping(*attrs)
|
50
|
+
attrs.each do |attr|
|
51
|
+
define_method("map_#{attr}") do |from, to|
|
52
|
+
to.send("#{attr}=", from.send(attr))
|
53
|
+
end
|
49
54
|
end
|
50
55
|
end
|
51
56
|
|
data/lib/karta/version.rb
CHANGED
data/spec/karta/mapper_spec.rb
CHANGED
@@ -103,14 +103,30 @@ describe Karta::Mapper do
|
|
103
103
|
end
|
104
104
|
|
105
105
|
describe '.one_to_one_mapping' do
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
106
|
+
|
107
|
+
|
108
|
+
context 'when defining a single one to one mapping' do
|
109
|
+
let(:mapper) do
|
110
|
+
define_klass 'Foo', base: Karta::Mapper do
|
111
|
+
one_to_one_mapping :foo
|
112
|
+
end.new
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'defines a mapping method on the instance' do
|
116
|
+
expect(mapper).to respond_to :map_foo
|
117
|
+
end
|
110
118
|
end
|
111
119
|
|
112
|
-
|
113
|
-
|
120
|
+
context 'when defining many one to one mappings' do
|
121
|
+
let(:mapper) do
|
122
|
+
define_klass 'Foo', base: Karta::Mapper do
|
123
|
+
one_to_one_mapping :foo, :bar
|
124
|
+
end.new
|
125
|
+
end
|
126
|
+
|
127
|
+
it 'defines a mapping method on the instance' do
|
128
|
+
expect(mapper).to respond_to :map_foo, :map_bar
|
129
|
+
end
|
114
130
|
end
|
115
131
|
end
|
116
132
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: karta
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Nilsson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-12-
|
11
|
+
date: 2016-12-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -121,6 +121,7 @@ files:
|
|
121
121
|
- ".rubocop.yml"
|
122
122
|
- ".ruby-gemset"
|
123
123
|
- ".ruby-version"
|
124
|
+
- ".travis.yml"
|
124
125
|
- ".yardopts"
|
125
126
|
- Gemfile
|
126
127
|
- LICENSE
|
@@ -159,7 +160,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
159
160
|
version: '0'
|
160
161
|
requirements: []
|
161
162
|
rubyforge_project:
|
162
|
-
rubygems_version: 2.
|
163
|
+
rubygems_version: 2.6.8
|
163
164
|
signing_key:
|
164
165
|
specification_version: 4
|
165
166
|
summary: A simple Ruby gem for creating mappers which map one object to another.
|