jacaranda 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in jacaranda.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Mauro George
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,70 @@
1
+ # Jacaranda
2
+
3
+ Generates helper methods based on your model validations creating common methods and scopes.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'jacaranda'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install jacaranda
18
+
19
+ ## Usage
20
+
21
+ ### Basic
22
+
23
+ Simply add the following line **after the validations**:
24
+
25
+ acts_as_jacaranda
26
+
27
+ like that
28
+
29
+ class Post < ActiveRecord::Base
30
+ validates :status, inclusion: { in: %w{published unpublished draft} }
31
+ acts_as_jacaranda
32
+ end
33
+
34
+ Now your model Post received some powers, we see them.
35
+
36
+ #### Predicate methods
37
+
38
+ Ask `@post` if it was published with `@post.published?` or it is a draft with `@post.draft?`.
39
+
40
+ #### Scopes
41
+
42
+ Find the posts based on column `status` using the generated scopes like `Post.published` e `Post.draft`.
43
+
44
+ ### Scoped
45
+
46
+ Perhaps model has 2 validations of inclusion that have valid values repeated
47
+
48
+ class Post < ActiveRecord::Base
49
+ validates :status, inclusion: { in: %w{published unpublished draft} }
50
+ validates :kind, inclusion: { in: %w{report unpublished draft} }
51
+ acts_as_jacaranda
52
+ end
53
+
54
+ in this case use Jacaranda with the param `scoped`:
55
+
56
+ acts_as_jacaranda scoped: true
57
+
58
+ which will generate methods based on column like `#unpublished_status?` and `#unpublished_kind?`.
59
+
60
+ ## Versioning
61
+
62
+ Jacaranda follow the [Semantic Versioning](http://semver.org/).
63
+
64
+ ## Contributing
65
+
66
+ 1. Fork it
67
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
68
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
69
+ 4. Push to the branch (`git push origin my-new-feature`)
70
+ 5. Create new Pull Request
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require "rspec/core/rake_task"
5
+ RSpec::Core::RakeTask.new
6
+
7
+ task default: :spec
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/jacaranda/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Mauro George"]
6
+ gem.email = ["maurogot@gmail.com"]
7
+ gem.description = %q{jacaranda generates helper methods based on your model validations.}
8
+ gem.summary = %q{jacaranda generates helper methods based on your model validations -
9
+ creating common methods and scopes.}
10
+ gem.homepage = "https://github.com/maurogeorge/jacaranda"
11
+
12
+ gem.files = `git ls-files`.split($\)
13
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15
+ gem.name = "jacaranda"
16
+ gem.require_paths = ["lib"]
17
+ gem.version = Jacaranda::VERSION
18
+
19
+ gem.add_dependency "activerecord", ">= 3.0.0"
20
+
21
+ gem.add_development_dependency "sqlite3"
22
+ gem.add_development_dependency "rspec"
23
+ gem.add_development_dependency "pry-meta"
24
+ end
@@ -0,0 +1,16 @@
1
+ en:
2
+ jacaranda:
3
+ errors:
4
+ messages:
5
+ unknown: "Maybe you found a bug, you got a not expected exception. Report this on https://github.com/maurogeorge/jacaranda/issues"
6
+ precedence: "Model %{model} has no validation of inclusion or the acts_as_jacaranda as included before the validations"
7
+ duplicated: "The following validators are in more than one field: %{validators}\n
8
+ in this case, use the scoped parameter as:\n\n
9
+ \_class %{model}\n
10
+ \_\_# Your validations\n
11
+ \_\_acts_as_jacaranda scoped: true\n
12
+ \_end"
13
+
14
+
15
+
16
+
@@ -0,0 +1,15 @@
1
+ require "active_record"
2
+
3
+ I18n.load_path << File.join(File.dirname(__FILE__), "config", "locales", "en.yml")
4
+
5
+ module Jacaranda
6
+ require "jacaranda/version"
7
+ require "jacaranda/errors"
8
+ require "jacaranda/base"
9
+ require "jacaranda/predicate"
10
+ require "jacaranda/scope"
11
+ end
12
+
13
+
14
+ ActiveRecord::Base.send(:include, Jacaranda)
15
+
@@ -0,0 +1,66 @@
1
+ module Jacaranda
2
+
3
+ def self.included(base)
4
+ base.extend(ClassMethods)
5
+ end
6
+
7
+ module ClassMethods
8
+
9
+ def acts_as_jacaranda(options = {})
10
+ configuration.update(options)
11
+ begin
12
+ verify!
13
+ create_predicate_methods
14
+ create_scope_methods
15
+ rescue => e
16
+ if e.kind_of?(Jacaranda::JacarandaError)
17
+ raise e
18
+ else
19
+ raise Jacaranda::JacarandaError, I18n.translate("jacaranda.errors.messages.unknown")
20
+ end
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def klazz
27
+ self
28
+ end
29
+
30
+ def configuration
31
+ @configuration ||= { scoped: false }
32
+ end
33
+
34
+ def inclusion_validators
35
+ klazz.validators.delete_if do |v|
36
+ v.class.to_s != "ActiveModel::Validations::InclusionValidator"
37
+ end
38
+ end
39
+
40
+ def validators_in
41
+ inclusion_validators.sum { |v| v.options[:in] }
42
+ end
43
+
44
+ def duplicate_validators
45
+ validators_in.select { |e| validators_in.count(e) > 1 }.uniq
46
+ end
47
+
48
+ def verify!
49
+ verify_precedence!
50
+ verify_duplicated!
51
+ end
52
+
53
+ def verify_precedence!
54
+ return if validators_in.kind_of?(Array)
55
+ if validators_in.zero?
56
+ raise JacarandaError, I18n.translate("jacaranda.errors.messages.precedence", model: klazz.to_s)
57
+ end
58
+ end
59
+
60
+ def verify_duplicated!
61
+ if duplicate_validators.any?
62
+ raise JacarandaError, I18n.translate("jacaranda.errors.messages.duplicated", model: klazz.to_s, validators: duplicate_validators.to_sentence)
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,6 @@
1
+ module Jacaranda
2
+
3
+ class JacarandaError < StandardError
4
+ end
5
+ end
6
+
@@ -0,0 +1,24 @@
1
+ module Jacaranda
2
+ module ClassMethods
3
+
4
+ private
5
+
6
+ def create_predicate_methods
7
+ inclusion_validators.each do |v|
8
+ v.options[:in].each do |content|
9
+ define_method build_predicate_name(v.attributes.first, content) do
10
+ send(v.attributes.first) == content
11
+ end
12
+ end
13
+ end
14
+ end
15
+
16
+ def build_predicate_name(column, name)
17
+ if configuration[:scoped]
18
+ "#{name}_#{column}?"
19
+ else
20
+ "#{name}?"
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,22 @@
1
+ module Jacaranda
2
+ module ClassMethods
3
+
4
+ private
5
+
6
+ def create_scope_methods
7
+ inclusion_validators.each do |v|
8
+ v.options[:in].each do |content|
9
+ scope(build_scope_name(v.attributes.first, content), lambda { where("#{v.attributes.first} = ? ", "#{content}") })
10
+ end
11
+ end
12
+ end
13
+
14
+ def build_scope_name(column, name)
15
+ if configuration[:scoped]
16
+ "#{name}_#{column}"
17
+ else
18
+ name
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ module Jacaranda
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,107 @@
1
+ require "spec_helper"
2
+
3
+ describe Jacaranda do
4
+
5
+ describe "predicate methods" do
6
+
7
+ describe "respond to created methods" do
8
+
9
+ it "with model with single validation" do
10
+ post = PostWithJacaranda.create(status: "published")
11
+ expect(post.respond_to?(:published?)).to be_true
12
+ end
13
+
14
+ it "with model with multiple validations" do
15
+ post = PostWithJacarandaAndTitleValidation.create(title: "Title", status: "published")
16
+ expect(post.respond_to?(:draft?)).to be_true
17
+ end
18
+
19
+ it "with param scoped" do
20
+ post = PostWithJacarandaScoped.create(status: "published")
21
+ expect(post.respond_to?(:published_status?)).to be_true
22
+ end
23
+ end
24
+
25
+ describe "return correct values" do
26
+
27
+ let(:post) do
28
+ PostWithJacaranda.create(status: "published")
29
+ end
30
+
31
+ it "when is true" do
32
+ expect(post).to be_published
33
+ end
34
+
35
+ it "when is false" do
36
+ expect(post).to_not be_draft
37
+ end
38
+ end
39
+ end
40
+
41
+ describe "scopes" do
42
+
43
+ it "have the scopes" do
44
+ expect(PostWithJacaranda.respond_to?(:published)).to be_true
45
+ end
46
+
47
+ it "create scoped scopes" do
48
+ expect(PostWithJacarandaScoped.respond_to?(:published_status)).to be_true
49
+ end
50
+
51
+ it "is a relation" do
52
+ expect(PostWithJacaranda.published).to be_kind_of(ActiveRecord::Relation)
53
+ end
54
+
55
+ describe "returned values" do
56
+
57
+ let!(:post_published) do
58
+ PostWithJacaranda.create(status: "published")
59
+ end
60
+
61
+ let!(:post_not_published) do
62
+ PostWithJacaranda.create(status: "draft")
63
+ end
64
+
65
+ it "return correct values" do
66
+ expect(PostWithJacaranda.published).to include(post_published)
67
+ end
68
+
69
+ it "not return incorrect values" do
70
+ expect(PostWithJacaranda.published).to_not include(post_not_published)
71
+ end
72
+ end
73
+ end
74
+
75
+ describe "errors" do
76
+
77
+ it "when included before the validations definitions" do
78
+ expect do
79
+ class PostWithJacarandaBeforeValidation < Post
80
+ acts_as_jacaranda
81
+ validates :status, inclusion: { in: %w{published unpublished draft} }
82
+ end
83
+ end.to raise_error(Jacaranda::JacarandaError, /Model PostWithJacarandaBeforeValidation has no validation of inclusion/)
84
+ end
85
+
86
+ it "raise error when has 2 inclusion with same name" do
87
+ expect do
88
+ class PostWithJacarandaDuplicateInclusionValue < Post
89
+ validates :status, inclusion: { in: %w{published unpublished draft} }
90
+ validates :kind, inclusion: { in: %w{report unpublished draft} }
91
+ acts_as_jacaranda
92
+ end
93
+ end.to raise_error(Jacaranda::JacarandaError, /The following validators are in more than one field: unpublished and draft/)
94
+ end
95
+
96
+ it "raise a Jacaranda::JacarandaError when has a unknown error" do
97
+ Post.stub(:create_predicate_methods).and_raise(StandardError)
98
+ expect do
99
+ class PostWithUnknownError < Post
100
+ validates :status, inclusion: { in: %w{published unplubished draft} }
101
+ acts_as_jacaranda
102
+ end
103
+ end.to raise_error(Jacaranda::JacarandaError, "Maybe you found a bug, you got a not expected exception. Report this on https://github.com/maurogeorge/jacaranda/issues")
104
+ end
105
+ end
106
+ end
107
+
@@ -0,0 +1,15 @@
1
+ require "bundler/setup"
2
+ Bundler.require(:default, :development)
3
+
4
+ require "jacaranda"
5
+ require "#{File.dirname(__FILE__)}/support/schema.rb"
6
+
7
+ Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each do |file|
8
+ require file
9
+ end
10
+
11
+ RSpec.configure do |config|
12
+ config.expect_with :rspec do |c|
13
+ c.syntax = :expect
14
+ end
15
+ end
@@ -0,0 +1,19 @@
1
+ class Post < ActiveRecord::Base
2
+ end
3
+
4
+ class PostWithJacaranda < Post
5
+ validates :status, inclusion: { in: %w{published unplubished draft} }
6
+ acts_as_jacaranda
7
+ end
8
+
9
+ class PostWithJacarandaAndTitleValidation < Post
10
+ validates :title, presence: true
11
+ validates :status, inclusion: { in: %w{published unplubished draft} }
12
+ acts_as_jacaranda
13
+ end
14
+
15
+ class PostWithJacarandaScoped < Post
16
+ validates :status, inclusion: { in: %w{published unpublished draft} }
17
+ acts_as_jacaranda scoped: true
18
+ end
19
+
@@ -0,0 +1,16 @@
1
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
2
+ ActiveRecord::Base.configurations = true
3
+
4
+ ActiveRecord::Schema.verbose = false
5
+ ActiveRecord::Schema.define(:version => 1) do
6
+
7
+ create_table :posts do |t|
8
+ t.datetime :created_at
9
+ t.datetime :updated_at
10
+ t.string :title
11
+ t.string :status
12
+ t.string :kind
13
+ end
14
+ end
15
+
16
+
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jacaranda
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mauro George
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-31 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: sqlite3
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: pry-meta
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: jacaranda generates helper methods based on your model validations.
79
+ email:
80
+ - maurogot@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - Gemfile
87
+ - LICENSE
88
+ - README.md
89
+ - Rakefile
90
+ - jacaranda.gemspec
91
+ - lib/config/locales/en.yml
92
+ - lib/jacaranda.rb
93
+ - lib/jacaranda/base.rb
94
+ - lib/jacaranda/errors.rb
95
+ - lib/jacaranda/predicate.rb
96
+ - lib/jacaranda/scope.rb
97
+ - lib/jacaranda/version.rb
98
+ - spec/jacaranda_spec.rb
99
+ - spec/spec_helper.rb
100
+ - spec/support/models.rb
101
+ - spec/support/schema.rb
102
+ homepage: https://github.com/maurogeorge/jacaranda
103
+ licenses: []
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ! '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 1.8.24
123
+ signing_key:
124
+ specification_version: 3
125
+ summary: jacaranda generates helper methods based on your model validations - creating
126
+ common methods and scopes.
127
+ test_files:
128
+ - spec/jacaranda_spec.rb
129
+ - spec/spec_helper.rb
130
+ - spec/support/models.rb
131
+ - spec/support/schema.rb