normalizator 0.0.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: fc2052bf00ec23dacf70899f62bb42a8f914cb46ecf6c72fca18dcb2c6f336aa
4
+ data.tar.gz: 92b1070b6c4391896d168b44e6221f4f649d3d0771fb843b75a69f193d43dc11
5
+ SHA512:
6
+ metadata.gz: f55a58c621930017b7cd99b7ba53ecf542cc37726ad9cac61219d69c678f3670b7782e81a696b38ce43176e0b782043767207df500620d06e5d9234a9e786b3d
7
+ data.tar.gz: 03af6853cee5b839b0a16029251a58281c98195547f8cc6c93941d3b67c020f929d88b6297396d3a58677314b6bdb37e1b7618b2ef4dcb1bf8fc5fea120d4bc2
data/.codeclimate.yml ADDED
@@ -0,0 +1,20 @@
1
+ engines:
2
+ rubocop:
3
+ enabled: true
4
+ golint:
5
+ enabled: false
6
+ gofmt:
7
+ enabled: false
8
+ eslint:
9
+ enabled: false
10
+ csslint:
11
+ enabled: false
12
+
13
+ ratings:
14
+ paths:
15
+ - lib/**
16
+ - "**.rb"
17
+
18
+ exclude_paths:
19
+ - spec/**/*
20
+ - vendor/**/*
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ .idea/
2
+ coverage/
3
+ .DS_Store
4
+ .rbenv-gemsets
5
+ .ruby-version
6
+ .vscode/
7
+ .bundle
8
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,98 @@
1
+ AllCops:
2
+ Exclude:
3
+ - 'bin/**/*'
4
+ - 'db/**/*'
5
+ - 'config/**/*'
6
+ - 'script/**/*'
7
+
8
+ Rails:
9
+ Enabled: true
10
+
11
+ Style/AlignParameters:
12
+ EnforcedStyle: with_fixed_indentation
13
+
14
+ Style/CaseIndentation:
15
+ EnforcedStyle: end
16
+
17
+ Style/AsciiComments:
18
+ Enabled: false
19
+
20
+ Style/IndentHash:
21
+ Enabled: false
22
+
23
+ Style/CollectionMethods:
24
+ Enabled: true
25
+ PreferredMethods:
26
+ inject: 'inject'
27
+
28
+ Style/Documentation:
29
+ Enabled: false
30
+
31
+ Style/BlockDelimiters:
32
+ Exclude:
33
+ - spec/**/*_spec.rb
34
+
35
+ Style/BracesAroundHashParameters:
36
+ Exclude:
37
+ - spec/**/*_spec.rb
38
+
39
+ Style/GuardClause:
40
+ Enabled: false
41
+
42
+ Style/IfUnlessModifier:
43
+ Enabled: false
44
+
45
+ Style/SpaceInsideHashLiteralBraces:
46
+ Enabled: false
47
+
48
+ Style/Lambda:
49
+ Enabled: false
50
+
51
+ Style/RaiseArgs:
52
+ Enabled: false
53
+
54
+ Style/SignalException:
55
+ Enabled: false
56
+
57
+ Metrics/AbcSize:
58
+ Max: 20
59
+
60
+ Metrics/ClassLength:
61
+ Max: 100
62
+
63
+ Metrics/ModuleLength:
64
+ Max: 100
65
+
66
+ Metrics/LineLength:
67
+ Enabled: false
68
+
69
+ Metrics/MethodLength:
70
+ Max: 15
71
+
72
+ Style/SingleLineBlockParams:
73
+ Enabled: false
74
+
75
+ Lint/EndAlignment:
76
+ EnforcedStyleAlignWith: variable
77
+
78
+ Style/FormatString:
79
+ Enabled: false
80
+
81
+ Style/MultilineMethodCallIndentation:
82
+ EnforcedStyle: indented
83
+
84
+ Style/MultilineOperationIndentation:
85
+ EnforcedStyle: indented
86
+
87
+ Style/WordArray:
88
+ Enabled: false
89
+
90
+ Style/RedundantSelf:
91
+ Enabled: false
92
+
93
+ Style/AlignHash:
94
+ Enabled: true
95
+ EnforcedLastArgumentHashStyle: always_ignore
96
+
97
+ Style/TrivialAccessors:
98
+ AllowPredicates: true
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ sudo: required
2
+ cache: bundler
3
+ dist: trusty
4
+ language: ruby
5
+ rvm:
6
+ - 2.3
7
+ - 2.4
8
+ - 2.5
9
+ - 2.6
10
+ gemfiles:
11
+ - normalizator.gemspec
12
+ script: "bundle exec rspec && bundle exec codeclimate-test-reporter"
13
+ before_install:
14
+ - gem install bundler
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.md ADDED
@@ -0,0 +1,9 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019-present David De Anda
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,7 @@
1
+ <h1 align="center">
2
+ <img src="https://raw.githubusercontent.com/omarandstuff/normalizator/master/media/normalizator-logo.png" alt="Normalizator" title="Normalizator" width="512">
3
+ </h1>
4
+
5
+ [![Build Status](https://travis-ci.org/omarandstuff/normalizator.svg?branch=master)](https://travis-ci.org/omarandstuff/normalizator)
6
+ [![Maintainability](https://api.codeclimate.com/v1/badges/8ac96249f76f2001c036/maintainability)](https://codeclimate.com/github/omarandstuff/normalizator/maintainability)
7
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/8ac96249f76f2001c036/test_coverage)](https://codeclimate.com/github/omarandstuff/normalizator/test_coverage)
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:test)
7
+
8
+ task default: :test
9
+ rescue LoadError
10
+ puts 'RSpec rake tasks not available. Please run "bundle install" to install missing dependencies.'
11
+ end
@@ -0,0 +1,17 @@
1
+ require 'normalizator/default_options'
2
+ require 'normalizator/error'
3
+ require 'normalizator/normalize'
4
+ require 'rules/base_rule'
5
+ require 'rules/enum_rule'
6
+ require 'rules/numeric_rule'
7
+
8
+ # Normalizator normizlize your data
9
+ module Normalizator
10
+ include Normalizator::DefaultOptions
11
+
12
+ module_function
13
+
14
+ def normalize(rules, data, options = {})
15
+ Normalize.new(rules, data, DEFAULT_OPTIONS.merge(options)).normalize
16
+ end
17
+ end
@@ -0,0 +1,9 @@
1
+ module Normalizator
2
+ module DefaultOptions
3
+ DEFAULT_OPTIONS = {
4
+ exclude_fileds_without_rule: true,
5
+ ignore_unmatched_rules: true,
6
+ default_unmatched_rules_value: nil
7
+ }.freeze
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module Normalizator
2
+ NormalizeError = Class.new(StandardError)
3
+
4
+ RuleError = Class.new(NormalizeError)
5
+ end
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Normalizator::Normalize module
4
+ module Normalizator
5
+ # Normalization logic for Normalizator
6
+ class Normalize
7
+ def initialize(rules, data, options)
8
+ raise(Normalizator::NormalizeError, 'Nil data') unless data
9
+ raise(Normalizator::NormalizeError, 'Nil rules') unless rules
10
+ @rules = rules
11
+ @data = data
12
+ @rules_keys = rules.keys
13
+ @options = options
14
+ end
15
+
16
+ def normalize
17
+ normalized_data = []
18
+
19
+ @data.each do |original_row|
20
+ normalized_row = run_rules_on_row original_row
21
+
22
+ normalized_data.push(normalized_row)
23
+ end
24
+
25
+ normalized_data
26
+ end
27
+
28
+ private
29
+
30
+ def run_rules_on_row(original_row)
31
+ new_row = @options[:exclude_fileds_without_rule] ? {} : original_row.clone
32
+
33
+ @rules_keys.each do |field|
34
+ if field.instance_of? Array
35
+ next if should_skip_mutly_field_rule(field, original_row)
36
+ run_rules_on_multy_value(field, new_row, original_row)
37
+ else
38
+ next if should_skip_rule(field, new_row, original_row)
39
+ new_row[field] = run_rules_on_value(@rules[field], original_row[field], original_row)
40
+ end
41
+ end
42
+
43
+ new_row
44
+ end
45
+
46
+ def run_rules_on_multy_value(multy_field, new_row, original_row)
47
+ values = multy_field.map { |sub_field| original_row[sub_field] }
48
+
49
+ normalized_values = run_rules_on_value(@rules[multy_field], values, original_row)
50
+
51
+ multy_field.each_with_index do |sub_field, index|
52
+ new_row[sub_field] = normalized_values[index]
53
+ end
54
+ end
55
+
56
+ def run_rules_on_value(rules, values, original_row)
57
+ if rules.instance_of? Array
58
+ ruled_field = values
59
+
60
+ rules.each do |rule|
61
+ ruled_field = rule.apply(ruled_field, original_row)
62
+ end
63
+
64
+ ruled_field
65
+ else
66
+ rules.apply(values, original_row)
67
+ end
68
+ end
69
+
70
+ def should_skip_rule(field, new_row, original_row)
71
+ unless original_row.key? field
72
+ return true if @options[:ignore_unmatched_rules]
73
+
74
+ new_row[field] = @options[:default_unmatched_rules_value]
75
+ true
76
+ end
77
+ end
78
+
79
+ def should_skip_mutly_field_rule(multy_field, original_row)
80
+ multy_field.any? { |sub_field| !original_row.key?(sub_field) }
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Normalizator
4
+ def self.gem_version
5
+ Gem::Version.new VERSION::STRING
6
+ end
7
+
8
+ # Version builder module
9
+ module VERSION
10
+ # major version
11
+ MAJOR = 0
12
+ # minor version
13
+ MINOR = 0
14
+ # tiny version
15
+ TINY = 0
16
+ # alpha, beta, etc. tag
17
+ PRE = nil
18
+
19
+ # Build version string
20
+ STRING = [[MAJOR, MINOR, TINY].compact.join('.'), PRE].compact.join('-')
21
+ end
22
+ end
@@ -0,0 +1,19 @@
1
+ # Normalizator::BaseRule module
2
+ module Normalizator
3
+ class BaseRule
4
+ DEFAULT_OPTIONS = {
5
+ return_original_on_failure: true,
6
+ default_value_on_failure: nil
7
+ }.freeze
8
+
9
+ def initialize(options = {})
10
+ @options = DEFAULT_OPTIONS.merge(options)
11
+ end
12
+
13
+ private
14
+
15
+ def get_value_on_failure(value)
16
+ @options[:return_original_on_failure] ? value : @options[:default_value_on_failure]
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,48 @@
1
+ # Normalizator::EnumRule module
2
+ module Normalizator
3
+ class EnumRule < BaseRule
4
+ def apply(value, _original_row)
5
+ value_on_failure = get_value_on_failure(value)
6
+ best_match = nil
7
+
8
+ raise(RuleError, 'Array of enumerators is expected in the rule constructor options') unless @options[:enumerators].instance_of? Array
9
+
10
+ @options[:enumerators].each do |enum|
11
+ (best_match, perfect_match) = match_agains_enumerator(enum, value, best_match)
12
+
13
+ break if perfect_match
14
+ end
15
+
16
+ best_match ? best_match : value_on_failure
17
+ end
18
+
19
+ private
20
+
21
+ def match_agains_enumerator(enum, value, best_match)
22
+ if @options[:case_sensitive]
23
+ (best_match, perfect_match) = case_sensitive_matching(enum, value, best_match)
24
+ else
25
+ (best_match, perfect_match) = case_insensitive_matching(enum, value, best_match)
26
+ end
27
+
28
+ [best_match, perfect_match]
29
+ end
30
+
31
+ def case_sensitive_matching(enum, value, best_match)
32
+ return [value, true] if value.strip == enum.strip
33
+ return [enum, false] if @options[:diffuse] && enum.include?(value)
34
+
35
+ [best_match, false]
36
+ end
37
+
38
+ def case_insensitive_matching(enum, value, best_match)
39
+ sanitized_value = value.downcase.strip
40
+ sanitized_enum = enum.downcase.strip
41
+
42
+ return [enum, true] if sanitized_value == sanitized_enum
43
+ return [enum, false] if @options[:diffuse] && sanitized_enum.include?(sanitized_value)
44
+
45
+ [best_match, false]
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,21 @@
1
+ # Normalizator::NumericRule module
2
+ module Normalizator
3
+ class NumericRule < BaseRule
4
+ def apply(value, _original_row)
5
+ value_on_failure = get_value_on_failure(value)
6
+
7
+ apply_numeric_rules value_on_failure, value.to_i
8
+ end
9
+
10
+ private
11
+
12
+ def apply_numeric_rules(value_on_failure, value)
13
+ return value_on_failure if @options[:positive] && value < 0
14
+ return value_on_failure if @options[:negative] && value >= 0
15
+ return value_on_failure if @options[:greater_than] && value < @options[:greater_than]
16
+ return value_on_failure if @options[:less_than] && value > @options[:less_than]
17
+
18
+ value
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,27 @@
1
+ lib = File.expand_path('../lib/', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'normalizator/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'normalizator'
7
+ spec.version = Normalizator.gem_version
8
+ spec.summary = 'Normalizator normalize your unnormalized data'
9
+ spec.authors = ['David De Anda']
10
+ spec.email = 'timrudat@gmail.com'
11
+ spec.description = ''
12
+ spec.homepage = 'https://github.com/omarandstuff/normalizator'
13
+ spec.license = 'MIT'
14
+ spec.required_ruby_version = '>= 2.1'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(spec|gemfiles|coverage|bin|media|examples)/}) }
17
+ spec.executables = []
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = %w[lib]
20
+
21
+ spec.add_development_dependency 'bundler'
22
+ spec.add_development_dependency 'codeclimate-test-reporter'
23
+ spec.add_development_dependency 'rake'
24
+ spec.add_development_dependency 'rspec'
25
+ spec.add_development_dependency 'simplecov'
26
+ spec.add_development_dependency 'simplecov-json'
27
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: normalizator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - David De Anda
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-07-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: codeclimate-test-reporter
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '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: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov-json
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: ''
98
+ email: timrudat@gmail.com
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - ".codeclimate.yml"
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".rubocop.yml"
107
+ - ".travis.yml"
108
+ - Gemfile
109
+ - LICENSE.md
110
+ - README.md
111
+ - Rakefile
112
+ - lib/normalizator.rb
113
+ - lib/normalizator/default_options.rb
114
+ - lib/normalizator/error.rb
115
+ - lib/normalizator/normalize.rb
116
+ - lib/normalizator/version.rb
117
+ - lib/rules/base_rule.rb
118
+ - lib/rules/enum_rule.rb
119
+ - lib/rules/numeric_rule.rb
120
+ - normalizator.gemspec
121
+ homepage: https://github.com/omarandstuff/normalizator
122
+ licenses:
123
+ - MIT
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '2.1'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubygems_version: 3.0.3
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Normalizator normalize your unnormalized data
144
+ test_files: []