path_utilities 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
+ SHA1:
3
+ metadata.gz: 1dde60fc83340b54e2036fab5afeb5bcb6444fa1
4
+ data.tar.gz: 662aa813cfa1ce402655c6b228dea9450e7d3d32
5
+ SHA512:
6
+ metadata.gz: 549fe786c774302e5a8a44300bf77c085627e75414f244000f04c6e6377e3ee552c9bd4c1099e5e04cb41f33f0eacc64b753d33b421655eba3e3a5b3bd2144a7
7
+ data.tar.gz: 2a4fe2a99e642919a1f5679a60659e8d539ee2ffbbb08ee4dc91324a002288d019151fb721682696cd71433ab0e0e42bba319f712dbc4117356edec19d542bf5
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.3
4
+ - 2.2.0
5
+ services: mongodb
6
+ before_install:
7
+ - gem update bundler
8
+ script:
9
+ - bundle exec rake
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in path_utilities.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 European Travel S.L.
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,84 @@
1
+ # PathUtilities
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/path_utilities`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'path_utilities'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install path_utilities
22
+
23
+ ## Usage
24
+
25
+ Given a model like this
26
+
27
+ ```ruby
28
+ class User
29
+ include Mongoid::Document
30
+
31
+ field :login
32
+ field :password
33
+ field :name
34
+ end
35
+ ```
36
+
37
+ Add a form for validating it
38
+
39
+ ```ruby
40
+ class FormUser
41
+ include PathUtilities::Form
42
+
43
+ properties [:login, :name, :password], :user
44
+
45
+ validates_uniqueness_of :login
46
+ validates_presence_of :name, :password
47
+ end
48
+ ```
49
+
50
+ Perform user validation
51
+
52
+ ```ruby
53
+ class MyController < ApplicationController
54
+ before_action :build, on: [:new, :create]
55
+
56
+ def create
57
+ if @form.validate(params[:user])
58
+ @form.save
59
+ redirect_to sign_in_path
60
+ else
61
+ render :new
62
+ end
63
+ end
64
+
65
+ def build
66
+ @form = FormUser.new(user: User.new)
67
+ end
68
+ end
69
+ ```
70
+
71
+
72
+ ## Development
73
+
74
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
75
+
76
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
77
+
78
+ ## Contributing
79
+
80
+ 1. Fork it ( https://github.com/[my-github-username]/path_utilities/fork )
81
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
82
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
83
+ 4. Push to the branch (`git push origin my-new-feature`)
84
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "path_utilities"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,48 @@
1
+ require 'mongoid/validatable/uniqueness'
2
+
3
+ module PathUtilities
4
+ module Form
5
+ module UniquenessValidator
6
+ class Mongoid < ::Mongoid::Validatable::UniquenessValidator
7
+ def initialize(options)
8
+ @klass = options[:model] if options[:model]
9
+ super
10
+ end
11
+
12
+ def validate_each(record, attribute, value)
13
+ # UniquenessValidator can't be used outside of Mongoid::Document instances, here
14
+ # we return the exact same error, unless the 'model' option is given.
15
+ #
16
+ if !options[:model] && !record.class.ancestors.include?(Mongoid::Document)
17
+ fail ArgumentError, "Unknown validator: 'UniquenessValidator'"
18
+
19
+ # If we're inside an ActiveRecord class, and `model` isn't set, use the
20
+ # default behaviour of the validator.
21
+ #
22
+ elsif ! options[:model]
23
+ super
24
+
25
+ # Custom validator options. The validator can be called in any class, as
26
+ # long as it includes `ActiveModel::Validations`. You can tell the validator
27
+ # which Mongoid::Document based class to check against, using the `model`
28
+ # option. Also, if you are using a different attribute name, you can set the
29
+ # correct one for the ActiveRecord class using the `attribute` option.
30
+ #
31
+ else
32
+ record_org, attribute_org = record, attribute
33
+
34
+ attribute = options[:attribute].to_sym if options[:attribute]
35
+ record = options[:model].new(attribute => value)
36
+
37
+ super
38
+
39
+ if record.errors.any?
40
+ record_org.errors.add(attribute_org, :taken,
41
+ options.except(:case_sensitive, :scope).merge(value: value))
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,8 @@
1
+ module PathUtilities
2
+ module Form
3
+ module UniquenessValidator
4
+ autoload :Mongoid,
5
+ 'path_utilities/form/uniqueness_validator/mongoid'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,140 @@
1
+ require 'active_support/concern'
2
+ require 'active_support/inflector'
3
+ require 'active_model/naming'
4
+ require 'active_model/conversion'
5
+ require 'active_model/validations'
6
+
7
+ module PathUtilities
8
+ module Form
9
+ extend ActiveSupport::Concern
10
+
11
+ autoload :UniquenessValidator, 'path_utilities/form/uniqueness_validator'
12
+
13
+ included do
14
+ include Virtus.model
15
+ extend ActiveModel::Naming
16
+ include ActiveModel::Conversion
17
+ include ActiveModel::Validations
18
+
19
+ attr_reader :models_mapping
20
+
21
+ def initialize(models_mapping = {})
22
+ @models_mapping = models_mapping
23
+ validate_mapping!
24
+ end
25
+
26
+ def validate_mapping!
27
+ self.class.models.each do |model|
28
+ next if models_mapping.keys.include?(model)
29
+ fail "#{model.to_s.camelize} not mapped on initialization"
30
+ end
31
+ end
32
+
33
+ def validate(params)
34
+ self.class.fields.keys.each do |field|
35
+ send("#{field}=", params[field])
36
+ end
37
+
38
+ valid?
39
+ end
40
+
41
+ def persisted?
42
+ false
43
+ end
44
+
45
+ def save
46
+ if valid?
47
+ sync
48
+ persist!
49
+ true
50
+ else
51
+ false
52
+ end
53
+ end
54
+
55
+ def sync
56
+ self.class.fields.keys.each do |field|
57
+ form_field_value = send(field)
58
+ instance_model_for(field).send("#{field}=", form_field_value)
59
+ end
60
+ end
61
+
62
+ def instance_model_for(field)
63
+ model = self.class.fields[field].options[:klass].name.underscore.to_sym
64
+ models_mapping[model]
65
+ end
66
+
67
+ def persist!
68
+ models_mapping.values.all?(&:save)
69
+ end
70
+ private :persist!
71
+ end
72
+
73
+ class_methods do
74
+ def properties(attributes, model)
75
+ @attributes ||= {}
76
+ add_model(model)
77
+ attributes.each do |att|
78
+ already_define_attribute_warn(att, model) do
79
+ attribute att, String
80
+ end
81
+
82
+ @attributes[att] = model.to_s.camelize
83
+ .safe_constantize.fields[att.to_s]
84
+ end
85
+ end
86
+
87
+ def fields
88
+ @attributes
89
+ end
90
+
91
+ def already_define_attribute_warn(attr, new_model)
92
+ if @attributes[attr].nil?
93
+ yield
94
+ else
95
+ Rails.logger.warn "#{attr} param already defined " \
96
+ "for #{@attributes[attr]} model"
97
+
98
+ return unless @attributes[attr] != new_model
99
+ Rails.logger.warn "#{attr} now is mapped to #{new_model}"
100
+ end
101
+ end
102
+
103
+ def models
104
+ @models ||= []
105
+ end
106
+
107
+ def add_model(name)
108
+ @models ||= []
109
+ return if @models.include?(name.to_sym)
110
+ @models << name.to_sym
111
+ end
112
+
113
+ def validates_uniqueness_of(attribute, options = {})
114
+ options = { case_sensitive: false, attributes: [attribute],
115
+ model: model_for(attribute) }
116
+ .reverse_merge(options)
117
+ validates_with(validation_class_for(attribute), options)
118
+ end
119
+
120
+ def validation_class_for(attr)
121
+ model_klass = model_for(attr)
122
+ case
123
+ when model_klass.ancestors.include?(::Mongoid::Document)
124
+ 'PathUtilities::Form::UniquenessValidator::Mongoid'
125
+ else
126
+ fail "#{model_klass.name} is not currently supported"
127
+ end.safe_constantize
128
+ end
129
+
130
+ def model_for(attr)
131
+ @attributes[attr] && @attributes[attr].options[:klass]
132
+ end
133
+
134
+ # mongoid-encrypted-fields gem compatibility method
135
+ def database_field_name(attribute)
136
+ attribute.to_sym
137
+ end
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,3 @@
1
+ module PathUtilities
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,8 @@
1
+ require "path_utilities/version"
2
+
3
+ module PathUtilities
4
+ autoload :Form, 'path_utilities/form'
5
+ end
6
+
7
+ require 'mongoid'
8
+ require 'virtus'
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'path_utilities/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'path_utilities'
8
+ spec.version = PathUtilities::VERSION
9
+ spec.authors = ['Ricard Forniol Agustí']
10
+ spec.email = ['rforniol@path.travel']
11
+
12
+ spec.summary = %q{Libraries and common used development strategies}
13
+ spec.description = %q{At this moment includes a form validator strategy}
14
+ spec.homepage = 'http://github.com/3mundi/path_utilities'
15
+ spec.license = 'MIT'
16
+
17
+ files = ->(f) { f.match(%r{^(test|spec|features)/}) }
18
+ spec.files = `git ls-files -z`.split("\x0").reject(&files)
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = %w(lib)
22
+
23
+ spec.add_development_dependency 'byebug', '~> 5'
24
+ spec.add_development_dependency 'database_cleaner', '~> 1.4'
25
+ spec.add_development_dependency 'bundler', '~> 1.9'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'rspec', '~> 3.2'
28
+ spec.add_development_dependency 'rspec-its', '~> 1.2'
29
+
30
+ spec.add_dependency 'activemodel', '~> 4.2'
31
+ spec.add_dependency 'activesupport', '~> 4.2'
32
+ spec.add_dependency 'virtus', '~> 1'
33
+ spec.add_dependency 'mongoid', '~> 4'
34
+ end
metadata ADDED
@@ -0,0 +1,201 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: path_utilities
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ricard Forniol Agustí
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-06-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: byebug
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: database_cleaner
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.4'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.9'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.9'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.2'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.2'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec-its
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.2'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.2'
97
+ - !ruby/object:Gem::Dependency
98
+ name: activemodel
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '4.2'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '4.2'
111
+ - !ruby/object:Gem::Dependency
112
+ name: activesupport
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '4.2'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '4.2'
125
+ - !ruby/object:Gem::Dependency
126
+ name: virtus
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '1'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '1'
139
+ - !ruby/object:Gem::Dependency
140
+ name: mongoid
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '4'
146
+ type: :runtime
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '4'
153
+ description: At this moment includes a form validator strategy
154
+ email:
155
+ - rforniol@path.travel
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - ".gitignore"
161
+ - ".rspec"
162
+ - ".travis.yml"
163
+ - CODE_OF_CONDUCT.md
164
+ - Gemfile
165
+ - LICENSE.txt
166
+ - README.md
167
+ - Rakefile
168
+ - bin/console
169
+ - bin/setup
170
+ - lib/path_utilities.rb
171
+ - lib/path_utilities/form.rb
172
+ - lib/path_utilities/form/uniqueness_validator.rb
173
+ - lib/path_utilities/form/uniqueness_validator/mongoid.rb
174
+ - lib/path_utilities/version.rb
175
+ - path_utilities.gemspec
176
+ homepage: http://github.com/3mundi/path_utilities
177
+ licenses:
178
+ - MIT
179
+ metadata: {}
180
+ post_install_message:
181
+ rdoc_options: []
182
+ require_paths:
183
+ - lib
184
+ required_ruby_version: !ruby/object:Gem::Requirement
185
+ requirements:
186
+ - - ">="
187
+ - !ruby/object:Gem::Version
188
+ version: '0'
189
+ required_rubygems_version: !ruby/object:Gem::Requirement
190
+ requirements:
191
+ - - ">="
192
+ - !ruby/object:Gem::Version
193
+ version: '0'
194
+ requirements: []
195
+ rubyforge_project:
196
+ rubygems_version: 2.2.2
197
+ signing_key:
198
+ specification_version: 4
199
+ summary: Libraries and common used development strategies
200
+ test_files: []
201
+ has_rdoc: