dry-transformer 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +11 -0
- data/LICENSE +20 -0
- data/README.md +29 -0
- data/dry-transformer.gemspec +29 -0
- data/lib/dry-transformer.rb +3 -0
- data/lib/dry/transformer.rb +23 -0
- data/lib/dry/transformer/all.rb +11 -0
- data/lib/dry/transformer/array.rb +183 -0
- data/lib/dry/transformer/array/combine.rb +65 -0
- data/lib/dry/transformer/class.rb +56 -0
- data/lib/dry/transformer/coercions.rb +196 -0
- data/lib/dry/transformer/compiler.rb +47 -0
- data/lib/dry/transformer/composite.rb +54 -0
- data/lib/dry/transformer/conditional.rb +76 -0
- data/lib/dry/transformer/constants.rb +7 -0
- data/lib/dry/transformer/error.rb +16 -0
- data/lib/dry/transformer/function.rb +109 -0
- data/lib/dry/transformer/hash.rb +454 -0
- data/lib/dry/transformer/pipe.rb +75 -0
- data/lib/dry/transformer/pipe/class_interface.rb +115 -0
- data/lib/dry/transformer/pipe/dsl.rb +58 -0
- data/lib/dry/transformer/proc.rb +46 -0
- data/lib/dry/transformer/recursion.rb +121 -0
- data/lib/dry/transformer/registry.rb +150 -0
- data/lib/dry/transformer/store.rb +128 -0
- data/lib/dry/transformer/version.rb +7 -0
- metadata +73 -0
@@ -0,0 +1,128 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Dry
|
4
|
+
module Transformer
|
5
|
+
# Immutable collection of named procedures from external modules
|
6
|
+
#
|
7
|
+
# @api private
|
8
|
+
#
|
9
|
+
class Store
|
10
|
+
# @!attribute [r] methods
|
11
|
+
#
|
12
|
+
# @return [Hash] The associated list of imported procedures
|
13
|
+
#
|
14
|
+
attr_reader :methods
|
15
|
+
|
16
|
+
# @!scope class
|
17
|
+
# @!name new(methods = {})
|
18
|
+
# Creates an immutable store with a hash of procedures
|
19
|
+
#
|
20
|
+
# @param [Hash] methods
|
21
|
+
#
|
22
|
+
# @return [Dry::Transformer::Store]
|
23
|
+
|
24
|
+
# @private
|
25
|
+
def initialize(methods = {})
|
26
|
+
@methods = methods.dup.freeze
|
27
|
+
freeze
|
28
|
+
end
|
29
|
+
|
30
|
+
# Returns a procedure by its key in the collection
|
31
|
+
#
|
32
|
+
# @param [Symbol] key
|
33
|
+
#
|
34
|
+
# @return [Proc]
|
35
|
+
#
|
36
|
+
def fetch(key)
|
37
|
+
methods.fetch(key)
|
38
|
+
end
|
39
|
+
|
40
|
+
# Returns wether the collection contains such procedure by its key
|
41
|
+
#
|
42
|
+
# @param [Symbol] key
|
43
|
+
#
|
44
|
+
# @return [Boolean]
|
45
|
+
#
|
46
|
+
def contain?(key)
|
47
|
+
methods.key?(key)
|
48
|
+
end
|
49
|
+
|
50
|
+
# Register a new function
|
51
|
+
#
|
52
|
+
# @example
|
53
|
+
# store.register(:to_json, -> v { v.to_json })
|
54
|
+
|
55
|
+
# store.register(:to_json) { |v| v.to_json }
|
56
|
+
#
|
57
|
+
def register(name, fn = nil, &block)
|
58
|
+
self.class.new(methods.merge(name => fn || block))
|
59
|
+
end
|
60
|
+
|
61
|
+
# Imports proc(s) to the collection from another module
|
62
|
+
#
|
63
|
+
# @private
|
64
|
+
#
|
65
|
+
def import(*args)
|
66
|
+
first = args.first
|
67
|
+
return import_all(first) if first.instance_of?(Module)
|
68
|
+
|
69
|
+
opts = args.pop
|
70
|
+
source = opts.fetch(:from)
|
71
|
+
rename = opts.fetch(:as) { first.to_sym }
|
72
|
+
|
73
|
+
return import_methods(source, args) if args.count > 1
|
74
|
+
|
75
|
+
import_method(source, first, rename)
|
76
|
+
end
|
77
|
+
|
78
|
+
protected
|
79
|
+
|
80
|
+
# Creates new immutable collection from the current one,
|
81
|
+
# updated with either the module's singleton method,
|
82
|
+
# or the proc having been imported from another module.
|
83
|
+
#
|
84
|
+
# @param [Module] source
|
85
|
+
# @param [Symbol] name
|
86
|
+
# @param [Symbol] new_name
|
87
|
+
#
|
88
|
+
# @return [Dry::Transformer::Store]
|
89
|
+
#
|
90
|
+
def import_method(source, name, new_name = name)
|
91
|
+
from = name.to_sym
|
92
|
+
to = new_name.to_sym
|
93
|
+
|
94
|
+
fn = source.is_a?(Registry) ? source.fetch(from) : source.method(from)
|
95
|
+
self.class.new(methods.merge(to => fn))
|
96
|
+
end
|
97
|
+
|
98
|
+
# Creates new immutable collection from the current one,
|
99
|
+
# updated with either the module's singleton methods,
|
100
|
+
# or the procs having been imported from another module.
|
101
|
+
#
|
102
|
+
# @param [Module] source
|
103
|
+
# @param [Array<Symbol>] names
|
104
|
+
#
|
105
|
+
# @return [Dry::Transformer::Store]
|
106
|
+
#
|
107
|
+
def import_methods(source, names)
|
108
|
+
names.inject(self) { |a, e| a.import_method(source, e) }
|
109
|
+
end
|
110
|
+
|
111
|
+
# Creates new immutable collection from the current one,
|
112
|
+
# updated with all singleton methods and imported methods
|
113
|
+
# from the other module
|
114
|
+
#
|
115
|
+
# @param [Module] source The module to import procedures from
|
116
|
+
#
|
117
|
+
# @return [Dry::Transformer::Store]
|
118
|
+
#
|
119
|
+
def import_all(source)
|
120
|
+
names = source.public_methods - Registry.instance_methods - Module.methods
|
121
|
+
names -= [:initialize] # for compatibility with Rubinius
|
122
|
+
names += source.store.methods.keys if source.is_a? Registry
|
123
|
+
|
124
|
+
import_methods(source, names)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dry-transformer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Piotr Solnica
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-01-14 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Data transformation toolkit
|
14
|
+
email:
|
15
|
+
- piotr.solnica@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- CHANGELOG.md
|
21
|
+
- LICENSE
|
22
|
+
- README.md
|
23
|
+
- dry-transformer.gemspec
|
24
|
+
- lib/dry-transformer.rb
|
25
|
+
- lib/dry/transformer.rb
|
26
|
+
- lib/dry/transformer/all.rb
|
27
|
+
- lib/dry/transformer/array.rb
|
28
|
+
- lib/dry/transformer/array/combine.rb
|
29
|
+
- lib/dry/transformer/class.rb
|
30
|
+
- lib/dry/transformer/coercions.rb
|
31
|
+
- lib/dry/transformer/compiler.rb
|
32
|
+
- lib/dry/transformer/composite.rb
|
33
|
+
- lib/dry/transformer/conditional.rb
|
34
|
+
- lib/dry/transformer/constants.rb
|
35
|
+
- lib/dry/transformer/error.rb
|
36
|
+
- lib/dry/transformer/function.rb
|
37
|
+
- lib/dry/transformer/hash.rb
|
38
|
+
- lib/dry/transformer/pipe.rb
|
39
|
+
- lib/dry/transformer/pipe/class_interface.rb
|
40
|
+
- lib/dry/transformer/pipe/dsl.rb
|
41
|
+
- lib/dry/transformer/proc.rb
|
42
|
+
- lib/dry/transformer/recursion.rb
|
43
|
+
- lib/dry/transformer/registry.rb
|
44
|
+
- lib/dry/transformer/store.rb
|
45
|
+
- lib/dry/transformer/version.rb
|
46
|
+
homepage: https://dry-rb.org/gems/dry-transformer
|
47
|
+
licenses:
|
48
|
+
- MIT
|
49
|
+
metadata:
|
50
|
+
allowed_push_host: https://rubygems.org
|
51
|
+
changelog_uri: https://github.com/dry-rb/dry-transformer/blob/master/CHANGELOG.md
|
52
|
+
source_code_uri: https://github.com/dry-rb/dry-transformer
|
53
|
+
bug_tracker_uri: https://github.com/dry-rb/dry-transformer/issues
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 2.4.0
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
requirements: []
|
69
|
+
rubygems_version: 3.0.3
|
70
|
+
signing_key:
|
71
|
+
specification_version: 4
|
72
|
+
summary: Data transformation toolkit
|
73
|
+
test_files: []
|