railjet 1.0.1

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.
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ # RSpec rake tasks
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+ task :default => :spec
8
+
9
+ # Version rake tasks
10
+ require 'rake-version'
11
+
12
+ RakeVersion::Tasks.new do |v|
13
+ v.copy 'lib/railjet/version.rb'
14
+ end
15
+
16
+ # Rubygems rake tasks
17
+ require "bundler/gem_tasks"
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.1
data/bin/bundler ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+ #
4
+ # This file was generated by Bundler.
5
+ #
6
+ # The application 'bundler' is installed as part of a gem, and
7
+ # this file is here to facilitate running it.
8
+ #
9
+
10
+ require "pathname"
11
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
12
+ Pathname.new(__FILE__).realpath)
13
+
14
+ require "rubygems"
15
+ require "bundler/setup"
16
+
17
+ load Gem.bin_path("bundler", "bundler")
data/bin/coderay ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+ #
4
+ # This file was generated by Bundler.
5
+ #
6
+ # The application 'coderay' is installed as part of a gem, and
7
+ # this file is here to facilitate running it.
8
+ #
9
+
10
+ require "pathname"
11
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
12
+ Pathname.new(__FILE__).realpath)
13
+
14
+ require "rubygems"
15
+ require "bundler/setup"
16
+
17
+ load Gem.bin_path("coderay", "coderay")
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "railjet"
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/htmldiff ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+ #
4
+ # This file was generated by Bundler.
5
+ #
6
+ # The application 'htmldiff' is installed as part of a gem, and
7
+ # this file is here to facilitate running it.
8
+ #
9
+
10
+ require "pathname"
11
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
12
+ Pathname.new(__FILE__).realpath)
13
+
14
+ require "rubygems"
15
+ require "bundler/setup"
16
+
17
+ load Gem.bin_path("diff-lcs", "htmldiff")
data/bin/ldiff ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+ #
4
+ # This file was generated by Bundler.
5
+ #
6
+ # The application 'ldiff' is installed as part of a gem, and
7
+ # this file is here to facilitate running it.
8
+ #
9
+
10
+ require "pathname"
11
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
12
+ Pathname.new(__FILE__).realpath)
13
+
14
+ require "rubygems"
15
+ require "bundler/setup"
16
+
17
+ load Gem.bin_path("diff-lcs", "ldiff")
data/bin/pry ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+ #
4
+ # This file was generated by Bundler.
5
+ #
6
+ # The application 'pry' is installed as part of a gem, and
7
+ # this file is here to facilitate running it.
8
+ #
9
+
10
+ require "pathname"
11
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
12
+ Pathname.new(__FILE__).realpath)
13
+
14
+ require "rubygems"
15
+ require "bundler/setup"
16
+
17
+ load Gem.bin_path("pry", "pry")
data/bin/rake ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+ #
4
+ # This file was generated by Bundler.
5
+ #
6
+ # The application 'rake' is installed as part of a gem, and
7
+ # this file is here to facilitate running it.
8
+ #
9
+
10
+ require "pathname"
11
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
12
+ Pathname.new(__FILE__).realpath)
13
+
14
+ require "rubygems"
15
+ require "bundler/setup"
16
+
17
+ load Gem.bin_path("rake", "rake")
data/bin/rspec ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+ #
4
+ # This file was generated by Bundler.
5
+ #
6
+ # The application 'rspec' is installed as part of a gem, and
7
+ # this file is here to facilitate running it.
8
+ #
9
+
10
+ require "pathname"
11
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
12
+ Pathname.new(__FILE__).realpath)
13
+
14
+ require "rubygems"
15
+ require "bundler/setup"
16
+
17
+ load Gem.bin_path("rspec-core", "rspec")
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,22 @@
1
+ ActiveModel::Errors.class_eval do
2
+ def <<(other)
3
+ copy_messages_from(other)
4
+ copy_details_from(other) if respond_to?(:details)
5
+
6
+ self
7
+ end
8
+
9
+ private
10
+
11
+ def copy_details_from(other)
12
+ details.merge!(other.details) do |_, val_one, val_two|
13
+ [*val_one] + [*val_two]
14
+ end
15
+ end
16
+
17
+ def copy_messages_from(other)
18
+ messages.merge!(other.messages) do |_, val_one, val_two|
19
+ [*val_one] + [*val_two]
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,73 @@
1
+ module GettextI18n
2
+ module TranslateAttributes
3
+ def self.store_form_attributes(file: "locale/form_attributes.rb")
4
+ store_attributes(file, FormAttributesFinder.new)
5
+ end
6
+
7
+ def self.store_policy_attributes(file: "locale/policy_attributes.rb")
8
+ store_attributes(file, PolicyAttributesFinder.new)
9
+ end
10
+
11
+ private
12
+
13
+ def self.store_attributes(file, finder)
14
+ File.open(file, 'w') do |f|
15
+ f.puts "# DO NOT MODIFY! AUTOMATICALLY GENERATED FILE!"
16
+
17
+ finder.find.each do |object, attributes|
18
+ f.puts("_('#{object.humanize_class_name}')")
19
+
20
+ attributes.each do |attribute|
21
+ translation = object.gettext_translation_for_attribute_name(attribute)
22
+ f.puts("_('#{translation}')")
23
+ end
24
+ end
25
+
26
+ f.puts "# DO NOT MODIFY! AUTOMATICALLY GENERATED FILE!"
27
+ end
28
+ rescue
29
+ puts "[Error] Attribute extraction failed. Removing incomplete file (#{file})"
30
+
31
+ File.delete(file)
32
+ raise
33
+ end
34
+
35
+ class AttributeFinder
36
+ def find
37
+ ActiveSupport::OrderedHash.new([]).tap do |found|
38
+ objects.each do |object|
39
+ found[object] = attributes(object)
40
+ end
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def objects
47
+ @objects ||= ObjectSpace.each_object(Class).select do |c|
48
+ c.included_modules.include?(base_class)
49
+ end
50
+ end
51
+
52
+ def attributes(object)
53
+ object.attribute_set.map(&:name).sort
54
+ end
55
+
56
+ def base_class
57
+ raise NotImplementedError
58
+ end
59
+ end
60
+
61
+ class PolicyAttributesFinder < AttributeFinder
62
+ def base_class
63
+ Railjet::Policy
64
+ end
65
+ end
66
+
67
+ class FormAttributesFinder < AttributeFinder
68
+ def base_class
69
+ Railjet::Form
70
+ end
71
+ end
72
+ end
73
+ end
data/lib/railjet.rb ADDED
@@ -0,0 +1,46 @@
1
+ require "railjet/version"
2
+
3
+ require "active_support/concern"
4
+ require "active_model"
5
+ require "active_model/merge_errors"
6
+ require "virtus"
7
+ require "validates_timeliness"
8
+
9
+ module Railjet
10
+ Error = Class.new(StandardError)
11
+ UnauthorizedError = Class.new(Error)
12
+ RecordNotFound = Class.new(Error)
13
+
14
+ class ValidationError < Error
15
+ attr_reader :errors
16
+
17
+ def initialize(errors)
18
+ @errors = errors
19
+ end
20
+
21
+ def error_messages
22
+ errors.try(:messages)
23
+ end
24
+ end
25
+
26
+ FormError = Class.new(ValidationError)
27
+ PolicyError = Class.new(ValidationError)
28
+ PolicyNotMetError = Class.new(PolicyError)
29
+ end
30
+
31
+ require "railjet/context"
32
+
33
+ require "railjet/util/use_case_helper"
34
+ require "railjet/util/policy_helper"
35
+ require "railjet/util/form_helper"
36
+
37
+ require "railjet/validator"
38
+ require "railjet/form"
39
+ require "railjet/policy"
40
+ require "railjet/composed_policy"
41
+ require "railjet/use_case"
42
+
43
+ require "railjet/repository/registry"
44
+ require "railjet/repository"
45
+
46
+ require "railjet/railtie" if defined?(Rails)
@@ -0,0 +1,20 @@
1
+ module Railjet
2
+ module Auth
3
+ class Ability
4
+ attr_reader :actor, :settings
5
+
6
+ def initialize(actor, settings)
7
+ @actor = actor
8
+ @settings = settings
9
+
10
+ @activities = {}
11
+ end
12
+
13
+ private
14
+
15
+ def activity(klass, object)
16
+ @activities[klass] ||= klass.new(object, settings)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,12 @@
1
+ module Railjet
2
+ module Auth
3
+ class Activity
4
+ attr_reader :settings, :object
5
+
6
+ def initialize(settings, object)
7
+ @settings = settings
8
+ @object = object
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,41 @@
1
+ module Railjet
2
+ class ComposedPolicy
3
+ class_attribute :policies
4
+
5
+ attr_reader :context, :object
6
+ attr_reader :policies
7
+
8
+ def self.add(policy, after: nil, before: nil)
9
+ self.policies ||= []
10
+
11
+ if after
12
+ self.policies = policies.dup.insert(policies.index(after) + 1, policy)
13
+ elsif before
14
+ self.policies = policies.dup.insert(policies.index(before), policy)
15
+ else
16
+ self.policies += [policy]
17
+ end
18
+ end
19
+
20
+ def initialize(context, object)
21
+ @context, @object = context, object
22
+
23
+ @policies = self.class.policies.map do |policy|
24
+ policy.new(context, object)
25
+ end
26
+ end
27
+
28
+ def valid?
29
+ policies.map(&:valid?).all?
30
+ end
31
+
32
+ def errors
33
+ valid?
34
+ policies.map(&:errors).inject(&:<<)
35
+ end
36
+
37
+ def validate!
38
+ policies.each(&:validate!)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,32 @@
1
+ module Railjet
2
+ class Context
3
+ def initialize(**kwargs)
4
+ define_accessors(kwargs)
5
+ end
6
+
7
+ # New values can be assigned to context on-the-fly,
8
+ # but it's not possible to change anything.
9
+ def method_missing(name, *args, &block)
10
+ getter_name = name[0..-2]
11
+
12
+ if name =~ /^[a-z]+=$/ && !respond_to?(getter_name)
13
+ define_accessor(getter_name, args.first)
14
+ else
15
+ super
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def define_accessors(kwargs)
22
+ kwargs.each do |name, val|
23
+ define_accessor(name, val)
24
+ end
25
+ end
26
+
27
+ def define_accessor(name, value)
28
+ instance_variable_set("@#{name}", value)
29
+ self.class.send(:attr_reader, name)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,10 @@
1
+ module Railjet
2
+ module Form
3
+ extend ::ActiveSupport::Concern
4
+ include Railjet::Validator
5
+
6
+ def validate!
7
+ valid? || (raise Railjet::FormError.new(errors) )
8
+ end
9
+ end
10
+ end