object_attorney 2.10.12 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (87) hide show
  1. checksums.yaml +5 -13
  2. data/.gitignore +48 -17
  3. data/.rubocop.yml +19 -0
  4. data/.ruby-version +1 -0
  5. data/.travis.yml +4 -0
  6. data/Gemfile +0 -13
  7. data/Gemfile.lock +83 -0
  8. data/README.md +25 -18
  9. data/Rakefile +7 -16
  10. data/lib/object_attorney.rb +57 -44
  11. data/lib/object_attorney/accusation.rb +46 -0
  12. data/lib/object_attorney/allegation.rb +20 -0
  13. data/lib/object_attorney/base.rb +17 -0
  14. data/lib/object_attorney/class_methods.rb +50 -0
  15. data/lib/object_attorney/errors.rb +32 -0
  16. data/lib/object_attorney/helpers.rb +22 -21
  17. data/lib/object_attorney/validations/custom.rb +23 -0
  18. data/lib/object_attorney/version.rb +3 -1
  19. data/object_attorney.gemspec +24 -17
  20. data/spec/object_attorney/base_spec.rb +21 -0
  21. data/spec/object_attorney/custom_validation_spec.rb +60 -0
  22. data/spec/object_attorney/object_attorney_spec.rb +64 -0
  23. data/spec/spec_helper.rb +17 -56
  24. data/spec/support/post.rb +7 -0
  25. data/spec/support/user.rb +5 -0
  26. metadata +102 -127
  27. data/.rvmrc +0 -1
  28. data/Guardfile +0 -5
  29. data/console.rb +0 -36
  30. data/db/migrate/20131205114000_create_users.rb +0 -13
  31. data/db/migrate/20131205114900_create_posts.rb +0 -14
  32. data/db/migrate/20131205114901_create_comments.rb +0 -13
  33. data/db/migrate/20131205114902_create_addresses.rb +0 -14
  34. data/db/schema.rb +0 -22
  35. data/lib/object_attorney/association_reflection.rb +0 -72
  36. data/lib/object_attorney/attribute_assignment.rb +0 -69
  37. data/lib/object_attorney/delegation.rb +0 -48
  38. data/lib/object_attorney/naming.rb +0 -16
  39. data/lib/object_attorney/nested_objects.rb +0 -311
  40. data/lib/object_attorney/orm_handlers/smooth_operator.rb +0 -69
  41. data/lib/object_attorney/record.rb +0 -117
  42. data/lib/object_attorney/reflection.rb +0 -40
  43. data/lib/object_attorney/representation.rb +0 -93
  44. data/lib/object_attorney/serialization.rb +0 -43
  45. data/lib/object_attorney/translation.rb +0 -25
  46. data/lib/object_attorney/validations.rb +0 -46
  47. data/not_used/nested_uniqueness_validator.rb +0 -33
  48. data/spec/object_attorney/address_form_spec.rb +0 -45
  49. data/spec/object_attorney/bulk_post_form_spec.rb +0 -52
  50. data/spec/object_attorney/bulk_posts_allow_only_existing_form_spec.rb +0 -37
  51. data/spec/object_attorney/bulk_posts_allow_only_new_form_spec.rb +0 -39
  52. data/spec/object_attorney/bulk_posts_with_form_objects_form_spec.rb +0 -91
  53. data/spec/object_attorney/delegation_spec.rb +0 -142
  54. data/spec/object_attorney/nested_objects_spec.rb +0 -63
  55. data/spec/object_attorney/post_form_spec.rb +0 -154
  56. data/spec/object_attorney/post_validations_form_spec.rb +0 -46
  57. data/spec/object_attorney/post_with_comment_form_spec.rb +0 -123
  58. data/spec/object_attorney/post_with_comment_validations_form_spec.rb +0 -46
  59. data/spec/object_attorney/post_with_comments_and_address_form_spec.rb +0 -54
  60. data/spec/object_attorney/post_with_only_existing_comments_form_spec.rb +0 -38
  61. data/spec/object_attorney/post_with_only_new_comments_form_spec.rb +0 -42
  62. data/spec/object_attorney/serialization_spec.rb +0 -40
  63. data/spec/object_attorney/test_spec.rb +0 -23
  64. data/spec/object_attorney/user_and_comments_form_spec.rb +0 -56
  65. data/spec/object_attorney/user_form_spec.rb +0 -70
  66. data/spec/require_helper.rb +0 -32
  67. data/spec/support/active_model/validations.rb +0 -21
  68. data/spec/support/database_setup.rb +0 -14
  69. data/spec/support/form_objects/address_form.rb +0 -9
  70. data/spec/support/form_objects/bulk_posts_allow_only_existing_form.rb +0 -19
  71. data/spec/support/form_objects/bulk_posts_allow_only_new_form.rb +0 -19
  72. data/spec/support/form_objects/bulk_posts_form.rb +0 -27
  73. data/spec/support/form_objects/bulk_posts_with_form_objects_form.rb +0 -27
  74. data/spec/support/form_objects/comment_form.rb +0 -11
  75. data/spec/support/form_objects/post_form.rb +0 -120
  76. data/spec/support/form_objects/post_validations_form.rb +0 -17
  77. data/spec/support/form_objects/post_with_comment_form.rb +0 -21
  78. data/spec/support/form_objects/post_with_comment_validations_form.rb +0 -17
  79. data/spec/support/form_objects/post_with_comments_and_address_form.rb +0 -13
  80. data/spec/support/form_objects/post_with_only_existing_comments_form.rb +0 -9
  81. data/spec/support/form_objects/post_with_only_new_comments_form.rb +0 -9
  82. data/spec/support/form_objects/user_and_comments_form.rb +0 -11
  83. data/spec/support/form_objects/user_form.rb +0 -11
  84. data/spec/support/models/address.rb +0 -5
  85. data/spec/support/models/comment.rb +0 -5
  86. data/spec/support/models/post.rb +0 -9
  87. data/spec/support/models/user.rb +0 -7
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- MTc4MmFkZGRlZGUyNWI0ZWJhNzBhNDNhMmRlZDc3MDJhOTRmZjcxYQ==
5
- data.tar.gz: !binary |-
6
- M2M1NGZjZDNhOTVjNTBkYTE1YWI4M2Y4ZmRiYmJhNzViNjg3MzQ5MQ==
2
+ SHA1:
3
+ metadata.gz: dbfd04fe687681f1030ef93cbbf32c177bb67f98
4
+ data.tar.gz: b2e7ff76d84de665db33f2275e082ad6dc24122c
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- NWQyNmZmZTM1OGE1NTA5MjQ4MTdiMDRjNjM4MzkzOWZjYjMzMGYxZjcwYmUy
10
- NDJkYTRmZjE1MzdhMGU4M2M0NzkzZmEyNjgyN2IzZmY1YzA3NGMxN2MxMWFj
11
- NjdhNDUwMWVlMmVjY2RhODk0OTY4M2I3YjM4ZTAzMDlhMzVjMjI=
12
- data.tar.gz: !binary |-
13
- YjVkZGQyMmNkYmQ1ZjkwMjc0ZjAwZDgwNTBlYmM0YzJhMjA0YTc5ZmYxMzQ2
14
- ZGJiMDhmODFmY2ExY2Q1OWFmNGMzYzRlMGNlM2EwNmVmM2FkOTJjODBkODBm
15
- OTAzNDRmODkwMTg4MzA2NGNkOTk1ZDViMjQ4NThlMjhhM2Q3YTY=
6
+ metadata.gz: 416ef86c1c665c0a20ef9a9efd78ed2ab21cacd7b254bbf5282e63cf54f214bb0757020f145a404730d0c63fd6fbd06a8457461d833f706c1a7a1c3e34e61ea8
7
+ data.tar.gz: 878e0a1ce4a1fbcabf50ac95dd2e7090f6f21548bf19b559fca4b1dc49ce0c939d76ca28da737cc6414c384f972a13126935dc73e2f4c9f63256a8dd83d2f92f
data/.gitignore CHANGED
@@ -1,19 +1,50 @@
1
1
  *.gem
2
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/*
18
- *.DS_Store
19
- db/test.sqlite3
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ ## Specific to RubyMotion:
14
+ .dat*
15
+ .repl_history
16
+ build/
17
+
18
+ ## Documentation cache and generated files:
19
+ /.yardoc/
20
+ /_yardoc/
21
+ /doc/
22
+ /rdoc/
23
+
24
+ ## Environment normalization:
25
+ /.bundle/
26
+ /vendor/bundle
27
+ /lib/bundler/man/
28
+
29
+ # for a library or gem, you might want to ignore these files since the code is
30
+ # intended to run in multiple environments; otherwise, check them in:
31
+ # Gemfile.lock
32
+ # .ruby-version
33
+ .ruby-gemset
34
+
35
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
36
+ .rvmrc
37
+
38
+ *.bundle
39
+ *.so
40
+ *.o
41
+ *.a
42
+ mkmf.log
43
+
44
+ .DS_Store
45
+
46
+ # Jetbrains Idea files
47
+ .idea/
48
+ api.iml
49
+
50
+ db/schema.rb
data/.rubocop.yml ADDED
@@ -0,0 +1,19 @@
1
+ Style/EmptyLinesAroundClassBody:
2
+ Enabled: false
3
+
4
+ Style/EmptyLinesAroundModuleBody:
5
+ Enabled: false
6
+
7
+ Style/FrozenStringLiteralComment:
8
+ Enabled: false
9
+
10
+ AllCops:
11
+ DisplayCopNames: true
12
+ Exclude:
13
+ - 'spec/**/*'
14
+ - 'Gemfile*'
15
+ - '*.gemspec'
16
+ - 'vendor/**/*'
17
+
18
+ Documentation:
19
+ Enabled: false
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.3.0
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.3.0
data/Gemfile CHANGED
@@ -1,16 +1,3 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in object_attorney.gemspec
4
3
  gemspec
5
-
6
- group :development, :test do
7
- gem "rspec", "~> 2.11"
8
- gem "sqlite3"
9
- gem "activerecord"
10
- #gem 'database_cleaner'
11
- gem "pry"
12
-
13
- unless ENV["CI"]
14
- gem "guard-rspec", "~> 0.7"
15
- end
16
- end
data/Gemfile.lock ADDED
@@ -0,0 +1,83 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ object_attorney (3.0.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ activemodel (4.2.6)
10
+ activesupport (= 4.2.6)
11
+ builder (~> 3.1)
12
+ activesupport (4.2.6)
13
+ i18n (~> 0.7)
14
+ json (~> 1.7, >= 1.7.7)
15
+ minitest (~> 5.1)
16
+ thread_safe (~> 0.3, >= 0.3.4)
17
+ tzinfo (~> 1.1)
18
+ ast (2.3.0)
19
+ builder (3.2.2)
20
+ codeclimate-test-reporter (0.4.8)
21
+ simplecov (>= 0.7.1, < 1.0.0)
22
+ coderay (1.1.1)
23
+ diff-lcs (1.2.5)
24
+ docile (1.1.5)
25
+ i18n (0.7.0)
26
+ json (1.8.3)
27
+ method_source (0.8.2)
28
+ minitest (5.9.0)
29
+ parser (2.3.1.2)
30
+ ast (~> 2.2)
31
+ powerpack (0.1.1)
32
+ pry (0.10.3)
33
+ coderay (~> 1.1.0)
34
+ method_source (~> 0.8.1)
35
+ slop (~> 3.4)
36
+ rainbow (2.1.0)
37
+ rake (11.2.2)
38
+ rspec (3.4.0)
39
+ rspec-core (~> 3.4.0)
40
+ rspec-expectations (~> 3.4.0)
41
+ rspec-mocks (~> 3.4.0)
42
+ rspec-core (3.4.4)
43
+ rspec-support (~> 3.4.0)
44
+ rspec-expectations (3.4.0)
45
+ diff-lcs (>= 1.2.0, < 2.0)
46
+ rspec-support (~> 3.4.0)
47
+ rspec-mocks (3.4.1)
48
+ diff-lcs (>= 1.2.0, < 2.0)
49
+ rspec-support (~> 3.4.0)
50
+ rspec-support (3.4.1)
51
+ rubocop (0.37.2)
52
+ parser (>= 2.3.0.4, < 3.0)
53
+ powerpack (~> 0.1)
54
+ rainbow (>= 1.99.1, < 3.0)
55
+ ruby-progressbar (~> 1.7)
56
+ unicode-display_width (~> 0.3)
57
+ ruby-progressbar (1.8.1)
58
+ simplecov (0.11.2)
59
+ docile (~> 1.1.0)
60
+ json (~> 1.8)
61
+ simplecov-html (~> 0.10.0)
62
+ simplecov-html (0.10.0)
63
+ slop (3.6.0)
64
+ thread_safe (0.3.5)
65
+ tzinfo (1.2.2)
66
+ thread_safe (~> 0.1)
67
+ unicode-display_width (0.3.1)
68
+
69
+ PLATFORMS
70
+ ruby
71
+
72
+ DEPENDENCIES
73
+ activemodel (= 4.2.6)
74
+ codeclimate-test-reporter (= 0.4.8)
75
+ object_attorney!
76
+ pry (= 0.10.3)
77
+ rake (= 11.2.2)
78
+ rspec (= 3.4.0)
79
+ rubocop (= 0.37.2)
80
+ simplecov (= 0.11.2)
81
+
82
+ BUNDLED WITH
83
+ 1.12.5
data/README.md CHANGED
@@ -1,29 +1,36 @@
1
- # ObjectAttorney
1
+ # Object Attorney
2
2
 
3
- TODO: Write a gem description
4
3
 
5
- ## Installation
4
+ [![Code Climate](https://codeclimate.com/github/goncalvesjoao/object_attorney/badges/gpa.svg)](https://codeclimate.com/github/goncalvesjoao/object_attorney)
5
+ [![Test Coverage](https://codeclimate.com/github/goncalvesjoao/object_attorney/badges/coverage.svg)](https://codeclimate.com/github/goncalvesjoao/object_attorney/coverage)
6
+ [![Build Status](https://travis-ci.org/goncalvesjoao/object_attorney.svg?branch=master)](https://travis-ci.org/goncalvesjoao/object_attorney)
6
7
 
7
- Add this line to your application's Gemfile:
8
+ ## 1) Basic Usage
9
+ ```ruby
10
+ class User < Struct.new(:title, :first_name, :last_name)
11
+ end
12
+ ```
8
13
 
9
- gem 'object_attorney'
14
+ ```ruby
15
+ class UserValidator < Struct.new(:user)
16
+ include ObjectAttorney
10
17
 
11
- And then execute:
18
+ defend :user
12
19
 
13
- $ bundle
20
+ validates_presence_of :first_name
21
+ end
14
22
 
15
- Or install it yourself as:
23
+ # OR
16
24
 
17
- $ gem install object_attorney
25
+ class UserValidator < ObjectAttorney::Base
26
+ validates_presence_of :first_name
27
+ end
28
+ ```
18
29
 
19
- ## Usage
30
+ ```ruby
31
+ @user = User.new
20
32
 
21
- TODO: Write usage instructions here
33
+ UserValidator.new(@user).valid?
22
34
 
23
- ## Contributing
24
-
25
- 1. Fork it
26
- 2. Create your feature branch (`git checkout -b my-new-feature`)
27
- 3. Commit your changes (`git commit -am 'Add some feature'`)
28
- 4. Push to the branch (`git push origin my-new-feature`)
29
- 5. Create new Pull Request
35
+ @user.errors.messages # { first_name: ["can't be blank"] }
36
+ ```
data/Rakefile CHANGED
@@ -1,19 +1,10 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
2
 
3
- # see: http://blog.aizatto.com/2007/05/27/activerecord-migrations-without-rails/
4
- # require 'active_record'
5
- # require 'yaml'
3
+ require 'rspec/core/rake_task'
4
+ require 'rubocop/rake_task'
6
5
 
7
- # ENV["RAILS_ENV"] ||= 'test'
6
+ RSpec::Core::RakeTask.new
7
+ RuboCop::RakeTask.new
8
8
 
9
- # task :default => :migrate
10
-
11
- # desc "Migrate the database through scripts in db/migrate. Target specific version with VERSION=x"
12
- # task :migrate => :environment do
13
- # ActiveRecord::Migrator.migrate('db/migrate', ENV["VERSION"] ? ENV["VERSION"].to_i : nil )
14
- # end
15
-
16
- # task :environment do
17
- # ActiveRecord::Base.establish_connection(YAML::load(File.open('config/database.yml'))[ENV["RAILS_ENV"]])
18
- # ActiveRecord::Base.logger = Logger.new(File.open('tmp/database.log', 'a'))
19
- # end
9
+ task(:default).clear
10
+ task default: [:rubocop, :spec]
@@ -1,66 +1,79 @@
1
-
2
- require "object_attorney/attribute_assignment"
3
- require "object_attorney/delegation"
4
- require "object_attorney/helpers"
5
- require "object_attorney/naming"
6
- require "object_attorney/reflection"
7
- require "object_attorney/validations"
8
- require "object_attorney/nested_objects"
9
- require "object_attorney/record"
10
- require "object_attorney/translation"
11
- require "object_attorney/representation"
12
- require "object_attorney/serialization"
13
- require 'active_record'
14
-
15
- require "object_attorney/version"
1
+ require 'active_model'
2
+ require 'object_attorney/version'
3
+ require 'object_attorney/helpers'
4
+ require 'object_attorney/class_methods'
16
5
 
17
6
  module ObjectAttorney
18
7
 
19
- def initialize(attributes = {}, object = nil)
20
- initialize_nested_attributes
8
+ def self.included(base_class)
9
+ base_class.extend ClassMethods
10
+ base_class.extend ActiveModel::Validations::HelperMethods
11
+ end
21
12
 
22
- attributes, object = parsing_arguments(attributes, object)
13
+ def defendant_is_innocent?
14
+ proven_innocent = defendants.all? do |defendant|
15
+ innocent_of_all_accusations?(defendant)
16
+ end
23
17
 
24
- before_initialize(attributes)
18
+ make_the_parent_guilty unless proven_innocent
25
19
 
26
- @represented_object ||= object
20
+ proven_innocent
21
+ end
27
22
 
28
- assign_attributes attributes
23
+ alias valid? defendant_is_innocent?
29
24
 
30
- after_initialize(attributes)
25
+ def invalid?
26
+ !valid?
31
27
  end
32
28
 
33
- protected #################### PROTECTED METHODS DOWN BELOW ######################
29
+ protected ######################### PROTECTED ################################
34
30
 
35
- def before_initialize(attributes); end
31
+ def defendants
32
+ defendant = Helpers.safe_call_method(self, defendant_options[:name])
36
33
 
37
- def after_initialize(attributes); end
34
+ if parent_defendant
35
+ Helpers.extend_errors_if_necessary(parent_defendant)
38
36
 
39
- private #################### PRIVATE METHODS DOWN BELOW ######################
37
+ defendant ||= parent_defendant.send(defendant_options[:name])
38
+ end
40
39
 
41
- def self.included(base)
42
- base.class_eval do
43
- include ActiveModel::Validations
44
- include ActiveModel::Validations::Callbacks
45
- include ActiveModel::Conversion
40
+ [defendant].flatten.compact
41
+ end
46
42
 
47
- include AttributeAssignment
48
- include Validations
49
- include NestedObjects
50
- include Record
51
- include Representation
52
- include Serialization
43
+ def innocent_of_all_accusations?(defendant)
44
+ Helpers.extend_errors_if_necessary(defendant)
53
45
 
54
- validate :validate_represented_object
55
- end
46
+ return true if Helpers.marked_for_destruction?(defendant)
47
+
48
+ founded_accusations(defendant).all?(&:sustained?)
49
+
50
+ defendant.errors.empty?
51
+ end
52
+
53
+ def make_the_parent_guilty
54
+ return unless parent_defendant
56
55
 
57
- base.extend(ClassMethods)
56
+ parent_defendant.errors.add(defendant_options[:name], :invalid)
58
57
  end
59
58
 
60
- module ClassMethods
61
- include Naming
62
- include Delegation
63
- include Translation
59
+ private ############################ PRIVATE #################################
60
+
61
+ def parent_defendant
62
+ return nil unless defendant_options[:in]
63
+
64
+ @parent_defendant ||= send(defendant_options[:in])
65
+ end
66
+
67
+ def defendant_options
68
+ self.class.defendant_options
69
+ end
70
+
71
+ def founded_accusations(defendant)
72
+ self.class.allegations.values.flatten.uniq.map do |allegation|
73
+ allegation.founded_accusation(self, defendant)
74
+ end.compact
64
75
  end
65
76
 
66
77
  end
78
+
79
+ require 'object_attorney/base'
@@ -0,0 +1,46 @@
1
+ module ObjectAttorney
2
+
3
+ class Accusation
4
+
5
+ def initialize(validation, attorney, defendant)
6
+ @attorney = attorney
7
+ @defendant = defendant
8
+ @validation = validation
9
+ end
10
+
11
+ def sustained?
12
+ @validation.attorney = @attorney if @validation.respond_to?(:attorney=)
13
+
14
+ # expected to be an ActiveModel::Validations::<Class> instance
15
+ @validation.validate(@defendant)
16
+ end
17
+
18
+ def founded
19
+ return true if options[:if].nil? && options[:unless].nil?
20
+
21
+ if_condition_true || unless_condition_true
22
+ end
23
+
24
+ protected ######################## PROTECTED ###############################
25
+
26
+ def if_condition_true
27
+ return nil if options[:if].nil?
28
+
29
+ Helpers.call_proc_or_method(@attorney, options[:if], @defendant)
30
+ end
31
+
32
+ def unless_condition_true
33
+ return nil if options[:unless].nil?
34
+
35
+ !Helpers.call_proc_or_method(@attorney, options[:unless], @defendant)
36
+ end
37
+
38
+ private ########################### PRIVATE ################################
39
+
40
+ def options
41
+ @validation.options
42
+ end
43
+
44
+ end
45
+
46
+ end