ruby_us 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +41 -0
- data/Rakefile +2 -0
- data/lib/ruby_us.rb +25 -0
- data/lib/ruby_us/abstract_interface.rb +84 -0
- data/lib/ruby_us/extensions/array.rb +33 -0
- data/lib/ruby_us/extensions/class.rb +39 -0
- data/lib/ruby_us/extensions/float.rb +7 -0
- data/lib/ruby_us/extensions/module.rb +78 -0
- data/lib/ruby_us/extensions/object.rb +7 -0
- data/lib/ruby_us/extensions/string.rb +22 -0
- data/lib/ruby_us/version.rb +3 -0
- data/ruby_us.gemspec +21 -0
- metadata +58 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e756763c53955ef739ff97be67a0ecf49bdd5fdf3299545802f6220b0255104b
|
4
|
+
data.tar.gz: bebb0df2f8e6623e4d5d1cb1b93ec2276728490902443fdeb2f830c57620d154
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5e41ed6179e53193b17881eb31f3aa062e7f777f956e4b2e8e17b3c063f89cf159f1d58f417250513af1433791a40dc1a90ab179def7c2ccdccd7c83a287cd2e
|
7
|
+
data.tar.gz: 127c52d81d0d02b3f5bd145ac5c2b53d7d499f15673c1efb0f526fc405aae05050e903da27db5de16e38f6ae6f108bd33c8a2e3bcad436dad2999697ce867344
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# RubyUs
|
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/ruby_us`. 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 'ruby_us'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install ruby_us
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
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`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ruby_us. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
data/lib/ruby_us.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
module RubyUs
|
2
|
+
extend self
|
3
|
+
|
4
|
+
def root_path
|
5
|
+
File.expand_path("../..", __FILE__)
|
6
|
+
end
|
7
|
+
|
8
|
+
def lib_path
|
9
|
+
File.join(root_path, "lib")
|
10
|
+
end
|
11
|
+
|
12
|
+
def spec_name
|
13
|
+
File.basename __FILE__, ".rb"
|
14
|
+
end
|
15
|
+
private :root_path, :lib_path, :spec_name
|
16
|
+
|
17
|
+
def timestamp
|
18
|
+
Time.now.strftime "%y%m%d%H%M%S%L%N"
|
19
|
+
end
|
20
|
+
|
21
|
+
Dir[File.join(lib_path, spec_name, "**/*.rb")].each do |file|
|
22
|
+
require file
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
module AbstractInterface
|
2
|
+
|
3
|
+
class Interface
|
4
|
+
def initialize constant, interface
|
5
|
+
@constant = constant
|
6
|
+
@interface = interface
|
7
|
+
end
|
8
|
+
|
9
|
+
def define_method method_name, &block
|
10
|
+
@constant.module_eval do
|
11
|
+
define_method method_name, &block
|
12
|
+
end if block_given?
|
13
|
+
end
|
14
|
+
|
15
|
+
def define_singleton_method method_name, &block
|
16
|
+
if block_given?
|
17
|
+
if @constant.is_a? Class
|
18
|
+
@constant.class_eval do
|
19
|
+
define_singleton_method method_name, &block
|
20
|
+
end
|
21
|
+
elsif @constant != interface
|
22
|
+
@constant::SingletonMethods.module_eval do
|
23
|
+
define_method method_name, &block
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
class << self
|
32
|
+
|
33
|
+
def included base
|
34
|
+
base.extend SingletonMethods
|
35
|
+
base.include SingletonMethods
|
36
|
+
end
|
37
|
+
|
38
|
+
def extended base
|
39
|
+
base.is_a?(Module) ? base.include(self) : base.class.include(self) unless base.included_modules.include? self
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
module SingletonMethods
|
45
|
+
|
46
|
+
def implements interface, &block
|
47
|
+
interface_instance = Interface.new self, interface
|
48
|
+
interface.module_eval do
|
49
|
+
block.call interface_instance
|
50
|
+
end if block_given?
|
51
|
+
end
|
52
|
+
|
53
|
+
def needs_implement_method method_name
|
54
|
+
interface = self.name
|
55
|
+
define_method method_name do |*args|
|
56
|
+
exception_message interface, method_name
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def needs_implement_singleton_method method_name
|
61
|
+
self.module_eval do
|
62
|
+
interface_name = self.name
|
63
|
+
constant = self::SingletonMethods
|
64
|
+
constant.module_eval do
|
65
|
+
define_method method_name do |*args|
|
66
|
+
singleton_exception_message interface_name, method_name
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
def exception_message interface, method_name
|
74
|
+
constant = self.is_a?(Module) ? self : self.class
|
75
|
+
raise NotImplementedError, "#{constant} needs to implement '#{method_name}' method of interface #{interface}"
|
76
|
+
end
|
77
|
+
|
78
|
+
def singleton_exception_message interface, method_name
|
79
|
+
constant = self.is_a?(Module) ? self : self.class
|
80
|
+
raise NotImplementedError, "#{constant} needs to implement '#{method_name}' singleton method of interface #{interface}"
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
class Array
|
2
|
+
|
3
|
+
def intersection set
|
4
|
+
all = [self, set]
|
5
|
+
|
6
|
+
smaller = (all = all.sort do |first, second|
|
7
|
+
first.count <=> second.count
|
8
|
+
end).first
|
9
|
+
|
10
|
+
larger = all[1]
|
11
|
+
|
12
|
+
smaller.select do |item|
|
13
|
+
larger.include? item
|
14
|
+
end.uniq
|
15
|
+
end
|
16
|
+
|
17
|
+
def intersections *sets
|
18
|
+
all = sets.push(self)
|
19
|
+
|
20
|
+
smaller = all.sort do |first, second|
|
21
|
+
first.count <=> second.count
|
22
|
+
end.first
|
23
|
+
|
24
|
+
all.delete smaller
|
25
|
+
|
26
|
+
intersections = smaller.intersection(all.first)
|
27
|
+
|
28
|
+
sets.reduce do |list|
|
29
|
+
intersections = intersections.intersection list
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
class Class
|
2
|
+
|
3
|
+
def attr_accessor *vars
|
4
|
+
@attributes ||= []
|
5
|
+
@attributes.concat vars
|
6
|
+
super *vars
|
7
|
+
end
|
8
|
+
|
9
|
+
def attr_reader *vars
|
10
|
+
@attributes ||= []
|
11
|
+
@attributes.concat vars
|
12
|
+
super *vars
|
13
|
+
end
|
14
|
+
|
15
|
+
def attr_writer *vars
|
16
|
+
@attributes ||= []
|
17
|
+
@attributes.concat vars
|
18
|
+
super *vars
|
19
|
+
end
|
20
|
+
|
21
|
+
def attributes
|
22
|
+
@attributes.nil? ? self.new.instance_variables : (self.new.instance_variables | @attributes)
|
23
|
+
end
|
24
|
+
|
25
|
+
def extend? constant
|
26
|
+
(ancestors - [self]).include? constant
|
27
|
+
end
|
28
|
+
|
29
|
+
def descendants
|
30
|
+
ObjectSpace.each_object(Class).select do |c|
|
31
|
+
c.extend? self
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def is_extended_by? constant
|
36
|
+
descendants.include? constant
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
class Module
|
2
|
+
|
3
|
+
def demodulize
|
4
|
+
splitted_trail = self.to_s.split("::")
|
5
|
+
constant = splitted_trail.last
|
6
|
+
|
7
|
+
const_get constant if defines?(constant)
|
8
|
+
end
|
9
|
+
private :demodulize
|
10
|
+
|
11
|
+
def deconstantize
|
12
|
+
ancestor = ancestors.first
|
13
|
+
splitted_trail = ancestor.to_s.split("::")
|
14
|
+
trail_name = splitted_trail.slice(0, splitted_trail.length - 1).join("::")
|
15
|
+
|
16
|
+
const_get(trail_name) if !trail_name.empty? && self.to_s != trail_name
|
17
|
+
end
|
18
|
+
|
19
|
+
def defines? constant, verbose=false
|
20
|
+
splitted_trail = constant.split("::")
|
21
|
+
trail_name = splitted_trail.first
|
22
|
+
|
23
|
+
# try
|
24
|
+
begin
|
25
|
+
trail = const_get(trail_name) if Object.send(:const_defined?, trail_name)
|
26
|
+
splitted_trail.slice(1, splitted_trail.length - 1).each do |constant_name|
|
27
|
+
trail = trail.send(:const_defined?, constant_name) ? trail.const_get(constant_name) : nil
|
28
|
+
end
|
29
|
+
true if trail
|
30
|
+
# catch (e)
|
31
|
+
rescue Exception => e
|
32
|
+
$stderr.puts "Exception recovered when trying to check if the constant \"#{constant}\" is defined: #{e}" if verbose
|
33
|
+
end unless constant.empty?
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
def has_constants?
|
38
|
+
true if constants.any?
|
39
|
+
end
|
40
|
+
|
41
|
+
# def nestings counted=[], &block
|
42
|
+
# trail = self.to_s
|
43
|
+
# collected = []
|
44
|
+
# recursivityQueue = []
|
45
|
+
|
46
|
+
# constants.each do |const_name|
|
47
|
+
# const_name = const_name.to_s
|
48
|
+
# const_for_try = "#{trail}::#{const_name}"
|
49
|
+
# constant = const_for_try.constantize
|
50
|
+
|
51
|
+
# begin
|
52
|
+
# constant_sym = constant.to_s.to_sym
|
53
|
+
# if constant && !counted.include?(constant_sym)
|
54
|
+
# counted << constant_sym
|
55
|
+
# if (constant.is_a?(Module) || constant.is_a?(Class))
|
56
|
+
# value = block_given? ? block.call(constant) : constant
|
57
|
+
# collected << value if value
|
58
|
+
|
59
|
+
# recursivityQueue.push({
|
60
|
+
# constant: constant,
|
61
|
+
# counted: counted,
|
62
|
+
# block: block
|
63
|
+
# }) if constant.has_constants?
|
64
|
+
# end
|
65
|
+
# end
|
66
|
+
# rescue Exception
|
67
|
+
# end
|
68
|
+
|
69
|
+
# end
|
70
|
+
|
71
|
+
# recursivityQueue.each do |data|
|
72
|
+
# collected.concat data[:constant].nestings(data[:counted], &data[:block])
|
73
|
+
# end
|
74
|
+
|
75
|
+
# collected
|
76
|
+
# end
|
77
|
+
|
78
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class String
|
2
|
+
|
3
|
+
def present?
|
4
|
+
empty? ? false : true
|
5
|
+
end
|
6
|
+
|
7
|
+
def snikize
|
8
|
+
self.gsub(/::/, '/')
|
9
|
+
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
|
10
|
+
.downcase
|
11
|
+
end
|
12
|
+
|
13
|
+
def constantize
|
14
|
+
if Module.defines?(self)
|
15
|
+
Module.const_get self
|
16
|
+
else
|
17
|
+
demodulized = self.split("::").last
|
18
|
+
Module.const_get(demodulized) if Module.defines?(demodulized)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
data/ruby_us.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
$:.push File.expand_path("../lib", __FILE__)
|
4
|
+
|
5
|
+
require 'ruby_us/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "ruby_us"
|
9
|
+
s.version = RubyUs::VERSION
|
10
|
+
s.authors = ["rplaurindo"]
|
11
|
+
s.email = ["rafaelplaurindo@gmail.com"]
|
12
|
+
|
13
|
+
s.homepage = "https://rubygems.org/gems/ruby_us"
|
14
|
+
s.summary = %q{Summary of RubyUs}
|
15
|
+
s.description = %q{OO Abstractions and helpers for Ruby.}
|
16
|
+
s.license = "MIT"
|
17
|
+
s.test_files = Dir["test/**/*"]
|
18
|
+
|
19
|
+
s.files = Dir["{config,lib,vendor}/**/*", "MIT-LICENSE", "Rakefile", "README.md", "ruby_us.gemspec"]
|
20
|
+
s.require_paths = %w{config lib vendor}
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby_us
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- rplaurindo
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-11-28 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: OO Abstractions and helpers for Ruby.
|
14
|
+
email:
|
15
|
+
- rafaelplaurindo@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- README.md
|
21
|
+
- Rakefile
|
22
|
+
- lib/ruby_us.rb
|
23
|
+
- lib/ruby_us/abstract_interface.rb
|
24
|
+
- lib/ruby_us/extensions/array.rb
|
25
|
+
- lib/ruby_us/extensions/class.rb
|
26
|
+
- lib/ruby_us/extensions/float.rb
|
27
|
+
- lib/ruby_us/extensions/module.rb
|
28
|
+
- lib/ruby_us/extensions/object.rb
|
29
|
+
- lib/ruby_us/extensions/string.rb
|
30
|
+
- lib/ruby_us/version.rb
|
31
|
+
- ruby_us.gemspec
|
32
|
+
homepage: https://rubygems.org/gems/ruby_us
|
33
|
+
licenses:
|
34
|
+
- MIT
|
35
|
+
metadata: {}
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
require_paths:
|
39
|
+
- config
|
40
|
+
- lib
|
41
|
+
- vendor
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 2.7.3
|
55
|
+
signing_key:
|
56
|
+
specification_version: 4
|
57
|
+
summary: Summary of RubyUs
|
58
|
+
test_files: []
|