ru.Bee 2.3.3 → 2.4.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 23eb359b46246fbc2d1057d89e061984b56843972563e9525bd5b16bfd183ef6
4
- data.tar.gz: 6bb72e7420d97a5b7cf30752bc90b73ccf29b9335754a057ee56148ec2d2f425
3
+ metadata.gz: c4204ab4a146dba0a5c2be32845880e17d576b12d807f58de9024e8f31db8268
4
+ data.tar.gz: e5afe1588d9597034a3931a86631d1cc6f35cb63c4d03fa45a64bef30494c127
5
5
  SHA512:
6
- metadata.gz: 126206e8eb09b2a7902b2a9aee068f86599761b4ef76df56fe228de4a8cf3a649b5c2d1c9f693884f658d76c5646e27bfa8bf578021f8bcdea99d43375bcbbe5
7
- data.tar.gz: 626b1627c969e0d0f9f941b4676fc07f9ddc784e5321dea1d987d0708502036b88f98f2337b6a62d6b5a41a45df061de4fb9f8db9bbee52c48bfaa538e95eff5
6
+ metadata.gz: 814450e1ae8f4fd6e206aa7fcfc0b021901bd00a4c7e8831ff94df5cae97d4c019981b4870310048977a5ff4f5cc6160d876bd91fc035fdb236988759d550c1a
7
+ data.tar.gz: 389a39ca1bd6f3853a53aa918c4e658de405bf035b53b98f14aa9e428b9bf27b1fd0cb8ba8b62190f446d0e932b11d743d194b265c401bba37866df98280dbb1
data/lib/db/test.db CHANGED
Binary file
@@ -7,6 +7,7 @@ module Rubee
7
7
  # autoload all rbs
8
8
  root_directory = File.join(Rubee::ROOT_PATH, '/lib')
9
9
  priority_order_require(root_directory, black_list)
10
+
10
11
  load_inits(root_directory, black_list)
11
12
  # ensure sequel object is connected
12
13
  Rubee::SequelObject.reconnect!
@@ -38,6 +39,12 @@ module Rubee
38
39
  end
39
40
  end
40
41
 
42
+ def load_support(root_directory, black_list)
43
+ Dir[File.join(Rubee::ROOT_PATH, '/lib', '/rubee/support/**', '*.rb')].each do |file|
44
+ require_relative file unless black_list.include?("#{file}.rb")
45
+ end
46
+ end
47
+
41
48
  def priority_order_require(root_directory, black_list)
42
49
  # rubee pub sub
43
50
  Dir[File.join(root_directory, 'rubee/pubsub/**', '*.rb')].each do |file|
@@ -51,6 +58,7 @@ module Rubee
51
58
  Dir[File.join(root_directory, 'rubee/async/**', '*.rb')].each do |file|
52
59
  require_relative file unless black_list.include?("#{file}.rb")
53
60
  end
61
+ load_support(root_directory, black_list)
54
62
  # app config and routes
55
63
  unless black_list.include?('base_configuration.rb')
56
64
  require_relative File.join(Rubee::APP_ROOT, Rubee::LIB,
@@ -32,6 +32,19 @@ module Rubee
32
32
  yield(self)
33
33
  end
34
34
 
35
+ def rubee_support=(value)
36
+ all = value.fetch(:all, false)
37
+ classes = value.fetch(:classes, [])
38
+ if all
39
+ Rubee::RUBEE_SUPPORT.each do |support_string, klass|
40
+ support_klass = Object.const_get(support_string)
41
+ klass.include(support_klass)
42
+ end
43
+ else
44
+ classes.each { |klass| Rubee::RUBEE_SUPPORT[klass.to_s].include(klass) }
45
+ end
46
+ end
47
+
35
48
  def database_url=(args)
36
49
  args[:app] ||= :app
37
50
  @configuraiton[args[:app].to_sym][args[:env].to_sym][:database_url] = args[:url].gsub("//", "//#{Rubee::LIB}")
@@ -0,0 +1,62 @@
1
+ module Rubee
2
+ module Support
3
+ module Hash
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ base.prepend(InstanceMethods)
7
+ end
8
+
9
+ module ClassMethods
10
+ end
11
+
12
+ module InstanceMethods
13
+ def [](key)
14
+ return wrap(super(key)) if key?(key)
15
+
16
+ alt_key =
17
+ case key
18
+ when ::Symbol then key.to_s
19
+ when ::String then key.to_sym
20
+ else key
21
+ end
22
+
23
+ wrap(super(alt_key))
24
+ end
25
+
26
+ private
27
+
28
+ def wrap(value)
29
+ value.is_a?(::Hash) ? value.extend(Rubee::Support::Hash::InstanceMethods) : value
30
+ end
31
+
32
+ def keys_to_string!
33
+ keys_to(:string, self)
34
+ end
35
+
36
+ def keys_to_sym!
37
+ keys_to(:symbol, self)
38
+ end
39
+
40
+ def keys_to(type, obj)
41
+ case obj
42
+ when ::Hash
43
+ obj.each_with_object({}) do |(k, v), result|
44
+ key =
45
+ case type
46
+ when :string then k.to_s
47
+ when :symbol then k.to_sym
48
+ else k
49
+ end
50
+
51
+ result[key] = keys_to(type, v)
52
+ end
53
+ when ::Array
54
+ obj.map { |v| keys_to(type, v) }
55
+ else
56
+ obj
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,59 @@
1
+ module Rubee
2
+ module Support
3
+ module String
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ base.include(InstanceMethods)
7
+ end
8
+
9
+ module ClassMethods
10
+ end
11
+
12
+ module InstanceMethods
13
+ def pluralize
14
+ if end_with?('y') && !%w[a e i o u].include?(self[-2])
15
+ "#{self[0..-2]}ies" # Replace "y" with "ies"
16
+ elsif end_with?('s', 'x', 'z', 'ch', 'sh')
17
+ "#{self}es" # Add "es" for certain endings
18
+ else
19
+ "#{self}s" # Default to adding "s"
20
+ end
21
+ end
22
+
23
+ def plural?
24
+ return true if end_with?('s') && !end_with?('ss')
25
+
26
+ false
27
+ end
28
+
29
+ def singular?
30
+ !plural?
31
+ end
32
+
33
+ def camelize
34
+ split('_').map { _1.capitalize }.join
35
+ end
36
+
37
+ def snakeize
38
+ gsub(/::/, '_')
39
+ .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
40
+ .gsub(/([a-z\d])([A-Z])/, '\1_\2')
41
+ .tr('-', '_')
42
+ .downcase
43
+ end
44
+
45
+ def singularize
46
+ if end_with?('ies') && length > 3
47
+ "#{self[0..-4]}y" # Convert "ies" to "y"
48
+ elsif end_with?('es') && %w[s x z ch sh].any? { |ending| self[-(ending.length + 2)..-3] == ending }
49
+ self[0..-3] # Remove "es" for selfs like "foxes", "buses"
50
+ elsif end_with?('s') && length > 1
51
+ self[0..-2] # Remove "s" for regular plurals
52
+ else
53
+ self # Return as-is if no plural form is detected
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
data/lib/rubee.rb CHANGED
@@ -16,8 +16,11 @@ module Rubee
16
16
  JS_DIR = File.join(APP_ROOT, LIB, 'js') unless defined?(JS_DIR)
17
17
  CSS_DIR = File.join(APP_ROOT, LIB, 'css') unless defined?(CSS_DIR)
18
18
  ROOT_PATH = File.expand_path(File.join(__dir__, '..')) unless defined?(ROOT_PATH)
19
+ unless defined?(RUBEE_SUPPORT)
20
+ RUBEE_SUPPORT = { "Rubee::Support::Hash" => Hash, "Rubee::Support::String" => String }
21
+ end
19
22
 
20
- VERSION = '2.3.3'
23
+ VERSION = '2.4.1'
21
24
 
22
25
  require_relative 'rubee/router'
23
26
  require_relative 'rubee/logger'
@@ -0,0 +1,23 @@
1
+ require_relative 'test_helper'
2
+
3
+ describe 'Configuration' do
4
+ describe 'rubee_support Hash only' do
5
+ it 'patches Hash only' do
6
+ String.send(:undef_method, :plural?) if "".respond_to?(:plural?)
7
+ Rubee::Configuration.setup(env = :test) do
8
+ _1.rubee_support = { classes: [Rubee::Support::Hash] }
9
+ end
10
+ _({ one: 1 }['one']).must_equal(1)
11
+
12
+ _(raise_error { "apples".plural? }.is_a?(NoMethodError)).must_equal(true)
13
+ end
14
+ end
15
+
16
+ describe 'rubee_support all' do
17
+ it 'patches Hash and String' do
18
+ Rubee::Configuration.setup(env = :test) { _1.rubee_support = { all: true } }
19
+ _({ one: 1 }['one']).must_equal(1)
20
+ _("apples".plural?).must_equal(true)
21
+ end
22
+ end
23
+ end
@@ -96,7 +96,7 @@ describe 'Comment model' do
96
96
  comment = Comment.new(user_id: 1)
97
97
 
98
98
  _(comment.valid?).must_equal(true)
99
- _(comment.errors[:test]).must_equal(nil)
99
+ _(comment.errors[:test].nil?).must_equal(true)
100
100
  end
101
101
 
102
102
  it 'text is a number should be invalid' do
data/readme.md CHANGED
@@ -84,6 +84,7 @@ The comparison is based on a very generic and subjective information open in the
84
84
  - [Generate commands](#generate-commands)
85
85
  - [Migration commands](#migration-commands)
86
86
  - [Rubee console](#rubee-console)
87
+ - [Rubee::Support](#rubee-support)
87
88
  - [Testing](#testing)
88
89
  - [Background jobs](#background-jobs)
89
90
  - [Modular](#modualar-application)
@@ -979,7 +980,7 @@ Model example
979
980
  ```ruby
980
981
  class User < Rubee::SequelObject
981
982
  attr_accessor :id, :email, :password, :created
982
-
983
+
983
984
  validate_after_setters # This will run validation after each setter.
984
985
  validate_before_persist! # This will validate and raise error in case invalid before saving to DB
985
986
  validate do
@@ -1019,7 +1020,7 @@ irb(main):081> user
1019
1020
  ```
1020
1021
  If you want to apply validation_before_persist! and validation_after_setters globally,
1021
1022
  add init/sequle_object_preloader.rb(you can chose any name)
1022
- file withing set up those methods
1023
+ file withing set up those methods
1023
1024
  for Rubee::SequelObject parent class by adding:
1024
1025
  ```
1025
1026
  Rubee::SequelObject.validate_befor_persist!
@@ -1028,6 +1029,45 @@ Rubee::SequelObject.validate_after_setters
1028
1029
  So you shouldn't add it to each model again and again.
1029
1030
  [Back to content](#content)
1030
1031
 
1032
+ ## Rubee support
1033
+
1034
+ In Rubee you have an optional way to charge your code with usefull methods that added to base Ruby classes.
1035
+ You can add them to your project globally by setting up it in configuration.
1036
+
1037
+ ```ruby
1038
+ # To include all support methods
1039
+ Rubee::Configuration.setup do |config|
1040
+ config.rubee_support = { all: true }
1041
+ end
1042
+
1043
+ # To include only those who belong to a indicated class
1044
+ Rubee::Configuration.setup do |config|
1045
+ config.rubee_support = { classes: [Rubee::Support::String] }
1046
+ # So only String will be charged with support methods
1047
+ end
1048
+ ```
1049
+ Here are list of additionl APIs extended for:
1050
+
1051
+ ```ruby
1052
+ # By adding to configuration Rubee::Suport::Hash class or just applying all, Hash will tollerate string or symbol keys.
1053
+ {one: 1}[:one] # => 1
1054
+ {one: 1}["one"] # => 1
1055
+ ```
1056
+
1057
+ # By adding to configuration Rubee::Support::String or just applying all, String will be enriched with handy helper methods.
1058
+ ```ruby
1059
+ "test".pluralize # => "tests"
1060
+ "test".singularize # => "test"
1061
+ "test".camelize # => "Test"
1062
+ "TestMe".snakeize # => "test_mei"
1063
+ "test".singular? # => true
1064
+ "test".plural? # => false
1065
+ ```
1066
+
1067
+ [Back to content](#content)
1068
+
1069
+
1070
+
1031
1071
  ## JWT based authentification
1032
1072
 
1033
1073
  Charge you rpoject with token based authentification system and customize it for your needs.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ru.Bee
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.3
4
+ version: 2.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oleg Saltykov
@@ -273,11 +273,14 @@ files:
273
273
  - lib/rubee/pubsub/subscriber.rb
274
274
  - lib/rubee/pubsub/test_one.rb
275
275
  - lib/rubee/router.rb
276
+ - lib/rubee/support/hash.rb
277
+ - lib/rubee/support/string.rb
276
278
  - lib/rubee/websocket/websocket.rb
277
279
  - lib/rubee/websocket/websocket_connections.rb
278
280
  - lib/tests/async/thread_async_test.rb
279
281
  - lib/tests/cli/attach_test.rb
280
282
  - lib/tests/cli/db_test.rb
283
+ - lib/tests/configuration_test.rb
281
284
  - lib/tests/controllers/auth_tokenable_test.rb
282
285
  - lib/tests/controllers/base_controller_test.rb
283
286
  - lib/tests/controllers/hookable_test.rb