alder 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +35 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/.travis.yml +10 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +55 -0
- data/Rakefile +6 -0
- data/alder.gemspec +26 -0
- data/lib/alder.rb +9 -0
- data/lib/alder/mapping.rb +55 -0
- data/lib/alder/predicate.rb +48 -0
- data/lib/alder/rule.rb +14 -0
- data/lib/alder/transform.rb +41 -0
- data/lib/alder/version.rb +3 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
NGYwNjM0MzEzMjcyZTA3MDdhYmJlOWY2NWZkYmI4ODJjMTc1OGRkZg==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ZTk1MDIxZDFiNTViMjUzMDBmNWM3N2ZiMTFjNzAyY2I3OTZjMGQ5Ng==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
ZThkOGFlN2FlZDAyODY5MGFlMjVhNzA4ZDk0Y2NhMWY3ZDU0NDZkYzQ1ZGQw
|
10
|
+
Y2M3ZDczZTkxNjFmYzBiZDlkZDQ2MGZlMGIzMDAwYmY1Y2FkNGI1ZGFhZTg0
|
11
|
+
Y2VkYzE5NTg5ZmEwYzk4OWJjZTNhZmFmNTk4MDEzMmJkMzFlNzE=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
MWFlYjY5MTUwZDgxNWJiNTIxMzVlNDM4N2I0ZmM3NjBiNTQzM2MwMWRmYTFi
|
14
|
+
NTYyMDI1N2U3MmY5MGNlMzlhMzdlZGNlYTg0ZjkyMWJhYTM5NTk5ZjY2OTE5
|
15
|
+
YTdmMmNkNWJlYjE4OWUyYjJhOTkzNTVhMTI0NTI4YjJjNjVhOWQ=
|
data/.gitignore
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/test/tmp/
|
9
|
+
/test/version_tmp/
|
10
|
+
/tmp/
|
11
|
+
|
12
|
+
## Specific to RubyMotion:
|
13
|
+
.dat*
|
14
|
+
.repl_history
|
15
|
+
build/
|
16
|
+
|
17
|
+
## Documentation cache and generated files:
|
18
|
+
/.yardoc/
|
19
|
+
/_yardoc/
|
20
|
+
/doc/
|
21
|
+
/rdoc/
|
22
|
+
|
23
|
+
## Environment normalisation:
|
24
|
+
/.bundle/
|
25
|
+
/vendor/bundle
|
26
|
+
/lib/bundler/man/
|
27
|
+
|
28
|
+
# for a library or gem, you might want to ignore these files since the code is
|
29
|
+
# intended to run in multiple environments; otherwise, check them in:
|
30
|
+
# Gemfile.lock
|
31
|
+
# .ruby-version
|
32
|
+
# .ruby-gemset
|
33
|
+
|
34
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
35
|
+
.rvmrc
|
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.3.0
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Karl Higley
|
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.
|
22
|
+
|
data/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/karlhigley/alder.svg?branch=master)](https://travis-ci.org/karlhigley/alder)
|
2
|
+
|
3
|
+
# alder
|
4
|
+
A Ruby library for transforming hashes
|
5
|
+
|
6
|
+
## Usage
|
7
|
+
|
8
|
+
Mappings represent bidirectional hash transformations. The `Mapping` class provides a simple DSL, with `up`, `down`, and `mapping` methods.
|
9
|
+
|
10
|
+
By convention, the `up` transformation converts a hash into the form expected by Ruby domain classes, while the `down` transformation converts a hash into the form expected by data stores or serialization formats.
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
class KeyMapping < Alder::Mapping
|
14
|
+
# Key matching supports literals and regular expressions
|
15
|
+
up /key1/ => "value1" do |match|
|
16
|
+
match[:key3] = match.delete(:key1)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Value matching supports literal and wildcard values (:_)
|
20
|
+
down /key3/ => :_ do |match|
|
21
|
+
match[:key1] = match.delete(:key3)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
```
|
25
|
+
|
26
|
+
Mappings can be composed with the `mapping` method, which includes the transformations defined on another mapping class:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
class ExampleMapping < Alder::Mapping
|
30
|
+
mapping KeyMapping
|
31
|
+
|
32
|
+
up :key3 => :_ do |match|
|
33
|
+
match[:key3] += " is now value3"
|
34
|
+
end
|
35
|
+
|
36
|
+
down :key3 => :_ do |match|
|
37
|
+
match[:key3].sub!(" is now value3", "")
|
38
|
+
end
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
(Alternately, the `up` and `down` DSL methods may each be called multiple times in a single class to define a series of transformations.)
|
43
|
+
|
44
|
+
Mappings operate on deep copies to avoid mutating the supplied hash parameter:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
mapping = ExampleMapping.new
|
48
|
+
hash = {keyA: {key1: "value1", key2: "value2"}}
|
49
|
+
|
50
|
+
mapping.up(hash)
|
51
|
+
#=> {:keyA=>{:key2=>"value2", :key3=>"value1 is now value3"}}
|
52
|
+
|
53
|
+
hash
|
54
|
+
#=> {:keyA=>{:key1=>"value1", :key2=>"value2"}}
|
55
|
+
```
|
data/Rakefile
ADDED
data/alder.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'alder/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "alder"
|
8
|
+
spec.version = Alder::VERSION
|
9
|
+
spec.authors = ["Karl Higley"]
|
10
|
+
spec.email = ["kmhigley@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A library for transforming hashes}
|
13
|
+
spec.homepage = "https://github.com/karlhigley/alder"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "activesupport", "~> 4.0"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
26
|
+
end
|
data/lib/alder.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require 'active_support/core_ext/class/attribute'
|
3
|
+
|
4
|
+
module Alder
|
5
|
+
class Mapping
|
6
|
+
class_attribute :up_rules, :instance_writer => false
|
7
|
+
class_attribute :down_rules, :instance_writer => false
|
8
|
+
|
9
|
+
self.up_rules = []
|
10
|
+
self.down_rules = []
|
11
|
+
|
12
|
+
def self.inherited(subclass)
|
13
|
+
subclass.up_rules += self.up_rules
|
14
|
+
subclass.down_rules += self.down_rules
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.up(fragment = {}, &transform)
|
18
|
+
rule = Rule.new(fragment, &transform)
|
19
|
+
self.up_rules << rule
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.down(fragment = {}, &transform)
|
23
|
+
rule = Rule.new(fragment, &transform)
|
24
|
+
self.down_rules << rule
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.mapping(mapping)
|
28
|
+
self.up_rules += mapping.up_rules
|
29
|
+
self.down_rules += mapping.down_rules
|
30
|
+
end
|
31
|
+
|
32
|
+
def initialize(up_t = up_transform, down_t = down_transform)
|
33
|
+
@up = up_t
|
34
|
+
@down = down_t
|
35
|
+
end
|
36
|
+
|
37
|
+
def up(hash)
|
38
|
+
@up.apply(hash)
|
39
|
+
end
|
40
|
+
|
41
|
+
def down(hash)
|
42
|
+
@down.apply(hash)
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def up_transform
|
48
|
+
Transform.new(self.class.up_rules)
|
49
|
+
end
|
50
|
+
|
51
|
+
def down_transform
|
52
|
+
Transform.new(self.class.down_rules.reverse)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Alder
|
2
|
+
class Predicate
|
3
|
+
def initialize(fragment)
|
4
|
+
@fragment = fragment
|
5
|
+
end
|
6
|
+
|
7
|
+
def match(hash)
|
8
|
+
node_match(@fragment, hash)
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def node_match(fragment, hash)
|
14
|
+
fragment.all? do |f_key, f_value|
|
15
|
+
keys = key_match(f_key, hash)
|
16
|
+
next false if keys.empty?
|
17
|
+
|
18
|
+
values = value_match(f_value, keys, hash)
|
19
|
+
!values.empty?
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def key_match(key, hash)
|
24
|
+
matches = []
|
25
|
+
if key.is_a?(Regexp)
|
26
|
+
hash.each do |k, v|
|
27
|
+
matches << k if key =~ k
|
28
|
+
end
|
29
|
+
else
|
30
|
+
matches << key if hash.has_key?(key)
|
31
|
+
end
|
32
|
+
matches
|
33
|
+
end
|
34
|
+
|
35
|
+
def value_match(value, keys, hash)
|
36
|
+
matches = []
|
37
|
+
keys.each do |k|
|
38
|
+
v = hash[k]
|
39
|
+
if value == :_ || value == v
|
40
|
+
matches << v
|
41
|
+
elsif v.is_a?(Hash) && node_match(value, v)
|
42
|
+
matches << v
|
43
|
+
end
|
44
|
+
end
|
45
|
+
matches
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/lib/alder/rule.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require 'active_support/core_ext/object/deep_dup'
|
3
|
+
|
4
|
+
module Alder
|
5
|
+
class Transform
|
6
|
+
def initialize(rules)
|
7
|
+
@rules = rules
|
8
|
+
end
|
9
|
+
|
10
|
+
def apply(hash)
|
11
|
+
transform(hash.deep_dup)
|
12
|
+
end
|
13
|
+
|
14
|
+
alias_method :call, :apply
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def transform(hash)
|
19
|
+
descend_into(hash)
|
20
|
+
apply_rules(@rules, hash)
|
21
|
+
end
|
22
|
+
|
23
|
+
def descend_into(hash)
|
24
|
+
hash.each_with_object(hash) do |(key, value), result|
|
25
|
+
case
|
26
|
+
when value.is_a?(Hash)
|
27
|
+
result[key] = transform(value)
|
28
|
+
when value.is_a?(Array)
|
29
|
+
result[key] = value.map { |v| transform(v) }
|
30
|
+
end
|
31
|
+
result
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def apply_rules(rules, hash)
|
36
|
+
rules.each_with_object(hash) do |rule, result|
|
37
|
+
rule.apply(result)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: alder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Karl Higley
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.11'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.11'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- kmhigley@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- .rspec
|
78
|
+
- .ruby-version
|
79
|
+
- .travis.yml
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- alder.gemspec
|
85
|
+
- lib/alder.rb
|
86
|
+
- lib/alder/mapping.rb
|
87
|
+
- lib/alder/predicate.rb
|
88
|
+
- lib/alder/rule.rb
|
89
|
+
- lib/alder/transform.rb
|
90
|
+
- lib/alder/version.rb
|
91
|
+
homepage: https://github.com/karlhigley/alder
|
92
|
+
licenses:
|
93
|
+
- MIT
|
94
|
+
metadata: {}
|
95
|
+
post_install_message:
|
96
|
+
rdoc_options: []
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ! '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ! '>='
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
requirements: []
|
110
|
+
rubyforge_project:
|
111
|
+
rubygems_version: 2.5.1
|
112
|
+
signing_key:
|
113
|
+
specification_version: 4
|
114
|
+
summary: A library for transforming hashes
|
115
|
+
test_files: []
|