model-mapper 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d815217a42e717db82be8fb900d733a5b74447fefb0276afaa6bee4e0a8bc1ca
4
+ data.tar.gz: 3c46647c66ce79bd951883461c283372f195d95a0addc65208390bb7fad940a3
5
+ SHA512:
6
+ metadata.gz: 8a2b61232b59e1fc1626a7a68cd1f52e9b14851e8252211af28419eb7b18b0f4e37be2d4150d98f39ecafae66b66a50037dcca24043565295b49ac823a3bad24
7
+ data.tar.gz: 392885dc229cc27060917c74c1aaeb6aca4f197366477993c60933fc2c05d4f115e91384ff22b17c08ba343c7a1b1bc62b98f8677bca6b8f36213f27bb364229
data/.gitignore ADDED
@@ -0,0 +1,51 @@
1
+ # rcov generated
2
+ coverage
3
+ coverage.data
4
+
5
+ # rdoc generated
6
+ rdoc
7
+
8
+ # yard generated
9
+ doc
10
+ .yardoc
11
+
12
+ # bundler
13
+ .bundle
14
+
15
+ # jeweler generated
16
+ pkg
17
+
18
+ # Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
19
+ #
20
+ # * Create a file at ~/.gitignore
21
+ # * Include files you want ignored
22
+ # * Run: git config --global core.excludesfile ~/.gitignore
23
+ #
24
+ # After doing this, these files will be ignored in all your git projects,
25
+ # saving you from having to 'pollute' every project you touch with them
26
+ #
27
+ # Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
28
+ #
29
+ # For MacOS:
30
+ #
31
+ .DS_Store
32
+
33
+ # For TextMate
34
+ #*.tmproj
35
+ #tmtags
36
+
37
+ # For emacs:
38
+ *~
39
+ \#*
40
+ .\#*
41
+
42
+ # For vim:
43
+ *.swp
44
+
45
+ # For redcar:
46
+ #.redcar
47
+
48
+ # For rubinius:
49
+ #*.rbc
50
+
51
+ *.gem
data/.rubocop.yml ADDED
@@ -0,0 +1,12 @@
1
+ Naming/FileName:
2
+ Enabled: false
3
+ Metrics/LineLength:
4
+ Max: 120
5
+ Metrics/ModuleLength:
6
+ Max: 120
7
+ Metrics/MethodLength:
8
+ Max: 20
9
+ AllCops:
10
+ Exclude:
11
+ - 'spec/spec_helper.rb'
12
+ - 'spec/**/*_spec.rb'
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ mappable
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.6.3
data/.travis.yml ADDED
@@ -0,0 +1,17 @@
1
+ env:
2
+ global:
3
+ - CC_TEST_REPORTER_ID=f54f3242f54a3040953c9127294fef862cb1851d12333d004619a8b7b7842054
4
+ rvm:
5
+ - 2.6.3
6
+ - 2.1.9
7
+ - 1.9.3
8
+ gemfile: gemfiles/travis.gemfile
9
+ language: ruby
10
+ before_script:
11
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
12
+ - chmod +x ./cc-test-reporter
13
+ - ./cc-test-reporter before-build
14
+ script:
15
+ - bundle exec rspec
16
+ after_script:
17
+ - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'http://rubygems.org'
4
+
5
+ gem 'inheritance-helper'
6
+
7
+ group :development do
8
+ gem 'rake'
9
+ gem 'rubocop'
10
+ end
11
+
12
+ group :spec do
13
+ gem 'rspec'
14
+ gem 'simplecov'
15
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,54 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ ast (2.4.0)
5
+ diff-lcs (1.3)
6
+ docile (1.3.2)
7
+ inheritance-helper (0.1.0)
8
+ jaro_winkler (1.5.3)
9
+ json (2.2.0)
10
+ parallel (1.17.0)
11
+ parser (2.6.4.0)
12
+ ast (~> 2.4.0)
13
+ rainbow (3.0.0)
14
+ rake (12.3.3)
15
+ rspec (3.8.0)
16
+ rspec-core (~> 3.8.0)
17
+ rspec-expectations (~> 3.8.0)
18
+ rspec-mocks (~> 3.8.0)
19
+ rspec-core (3.8.2)
20
+ rspec-support (~> 3.8.0)
21
+ rspec-expectations (3.8.4)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.8.0)
24
+ rspec-mocks (3.8.1)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.8.0)
27
+ rspec-support (3.8.2)
28
+ rubocop (0.74.0)
29
+ jaro_winkler (~> 1.5.1)
30
+ parallel (~> 1.10)
31
+ parser (>= 2.6)
32
+ rainbow (>= 2.2.2, < 4.0)
33
+ ruby-progressbar (~> 1.7)
34
+ unicode-display_width (>= 1.4.0, < 1.7)
35
+ ruby-progressbar (1.10.1)
36
+ simplecov (0.17.0)
37
+ docile (~> 1.1)
38
+ json (>= 1.8, < 3)
39
+ simplecov-html (~> 0.10.0)
40
+ simplecov-html (0.10.2)
41
+ unicode-display_width (1.6.0)
42
+
43
+ PLATFORMS
44
+ ruby
45
+
46
+ DEPENDENCIES
47
+ inheritance-helper
48
+ rake
49
+ rspec
50
+ rubocop
51
+ simplecov
52
+
53
+ BUNDLED WITH
54
+ 1.17.3
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 dougyouch
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # mappable
2
+
3
+ [![Build Status](https://travis-ci.org/dougyouch/mappable.svg?branch=master)](https://travis-ci.org/dougyouch/mappable)
4
+ [![Maintainability](https://api.codeclimate.com/v1/badges/ac5801c8775694186c58/maintainability)](https://codeclimate.com/github/dougyouch/mappable/maintainability)
5
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/ac5801c8775694186c58/test_coverage)](https://codeclimate.com/github/dougyouch/mappable/test_coverage)
@@ -0,0 +1 @@
1
+ gemfiles/../Gemfile
data/lib/mappable.rb ADDED
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'inheritance-helper'
4
+
5
+ # Transfer/Map data from one model to the next
6
+ module Mappable
7
+ autoload :Mapping, 'mappable/mapping'
8
+ autoload :Utils, 'mappable/utils'
9
+
10
+ def self.included(base)
11
+ base.extend InheritanceHelper::Methods
12
+ base.extend ClassMethods
13
+ end
14
+
15
+ # no-doc
16
+ module ClassMethods
17
+ def maps
18
+ {}.freeze
19
+ end
20
+
21
+ def map_to(name, options = {}, &block)
22
+ mapping = Mapping.create(self, name, options, &block)
23
+ add_value_to_class_method(:maps, name => mapping)
24
+
25
+ class_eval(
26
+ <<-STR, __FILE__, __LINE__ + 1
27
+ def map_to_#{name}(dest)
28
+ ::#{mapping.name}.new.map(self, dest)
29
+ dest
30
+ end
31
+ STR
32
+ )
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mappable
4
+ # Defines what fields to map
5
+ module Mapping
6
+ def self.included(base)
7
+ base.extend InheritanceHelper::Methods
8
+ base.extend ClassMethods
9
+ end
10
+
11
+ def self.default_mapping_options(src, dest)
12
+ {
13
+ src: src.to_sym,
14
+ src_getter: src.to_s.freeze,
15
+ src_setter: "#{src}=",
16
+ dest: dest.to_sym,
17
+ dest_getter: dest.to_s.freeze,
18
+ dest_setter: "#{dest}="
19
+ }
20
+ end
21
+
22
+ # no-doc
23
+ module ClassMethods
24
+ def mappings
25
+ {}.freeze
26
+ end
27
+
28
+ def map(src, dest = nil, options = {})
29
+ if dest.is_a?(Hash)
30
+ options = dest
31
+ dest = nil
32
+ end
33
+
34
+ dest ||= src
35
+
36
+ options = ::Mappable::Mapping.default_mapping_options(src, dest)
37
+ .merge(options)
38
+
39
+ add_value_to_class_method(:mappings, src.to_sym => options)
40
+ end
41
+ end
42
+
43
+ def map(src_model, dest_model)
44
+ self.class.mappings.each do |_, options|
45
+ next if skip?(src_model, dest_model, options)
46
+
47
+ dest_model.public_send(options[:dest_setter], src_model.public_send(options[:src_getter]))
48
+ end
49
+ dest_model
50
+ end
51
+
52
+ def skip?(src_model, dest_model, options)
53
+ return true if options[:if] && !call_method(dest_model, options[:if])
54
+ return true if options[:unless] && call_method(dest_model, options[:unless])
55
+
56
+ false
57
+ end
58
+
59
+ def call_method(model, method)
60
+ case method
61
+ when Symbol
62
+ model.public_send(method)
63
+ when Proc
64
+ model.instance_eval(&method)
65
+ else
66
+ raise("wrong type, failed to call method #{method}")
67
+ end
68
+ end
69
+
70
+ def self.create(base_module, name, options = {}, &block)
71
+ options[:class_name] ||= ::Mappable::Utils.classify_name(name.to_s) + 'Mapping'
72
+ kls = Class.new(options[:base_class] || Object)
73
+ kls = base_module.const_set(options[:class_name], kls)
74
+ kls.send(:include, ::Mappable::Mapping)
75
+ kls.class_eval(&block) if block
76
+ kls
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mappable
4
+ # General purpouse utility methods
5
+ module Utils
6
+ module_function
7
+
8
+ def classify_name(name)
9
+ name.gsub(/[^\da-z_-]/, '').gsub(/(^.|[_|-].)/) { |m| m[-1].upcase }
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mappable'
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'model-mapper'
5
+ s.version = '0.1.0'
6
+ s.licenses = ['MIT']
7
+ s.summary = 'Map data between models'
8
+ s.description = 'Easy way to configure what data is mapped between models'
9
+ s.authors = ['Doug Youch']
10
+ s.email = 'dougyouch@gmail.com'
11
+ s.homepage = 'https://github.com/dougyouch/mappable'
12
+ s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|script)/}) }
13
+
14
+ s.add_runtime_dependency 'inheritance-helper'
15
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: model-mapper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Doug Youch
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-09-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: inheritance-helper
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Easy way to configure what data is mapped between models
28
+ email: dougyouch@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - ".gitignore"
34
+ - ".rubocop.yml"
35
+ - ".ruby-gemset"
36
+ - ".ruby-version"
37
+ - ".travis.yml"
38
+ - Gemfile
39
+ - Gemfile.lock
40
+ - LICENSE
41
+ - README.md
42
+ - gemfiles/travis.gemfile
43
+ - lib/mappable.rb
44
+ - lib/mappable/mapping.rb
45
+ - lib/mappable/utils.rb
46
+ - lib/model-mapper.rb
47
+ - model-mapper.gemspec
48
+ homepage: https://github.com/dougyouch/mappable
49
+ licenses:
50
+ - MIT
51
+ metadata: {}
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 2.7.10
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: Map data between models
72
+ test_files: []