just_enum 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4407bd54833c58ac81c23c2067b06e49fe7eb055978ee9132e2f590784db3bb6
4
+ data.tar.gz: 6aa6c654c9303f5e3852adb86f25d45d375ff897570164cd42dc9f98c3f82485
5
+ SHA512:
6
+ metadata.gz: 169b80283833e53040ba83c4dd67061c6f377544334c5f32fd941a6327e80995a4e0e419589ccbd65aea3c28e2638f911b0df12fa3306b6cea2a9a02d28dc10e
7
+ data.tar.gz: 89783ddc98bce0c488b3b39966e51298c9179fed58401560fac5fc40eb5ab2934a5be7f7c3eeda6e33887f3f7262fcf86ba30408573aaa4115f90f63da5fafcf
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ .idea
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in just_enum.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.7"
data/Gemfile.lock ADDED
@@ -0,0 +1,55 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ just_enum (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ diff-lcs (1.5.0)
11
+ parallel (1.21.0)
12
+ parser (3.0.3.2)
13
+ ast (~> 2.4.1)
14
+ rainbow (3.0.0)
15
+ rake (13.0.6)
16
+ regexp_parser (2.2.0)
17
+ rexml (3.2.5)
18
+ rspec (3.10.0)
19
+ rspec-core (~> 3.10.0)
20
+ rspec-expectations (~> 3.10.0)
21
+ rspec-mocks (~> 3.10.0)
22
+ rspec-core (3.10.1)
23
+ rspec-support (~> 3.10.0)
24
+ rspec-expectations (3.10.1)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.10.0)
27
+ rspec-mocks (3.10.2)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.10.0)
30
+ rspec-support (3.10.3)
31
+ rubocop (1.23.0)
32
+ parallel (~> 1.10)
33
+ parser (>= 3.0.0.0)
34
+ rainbow (>= 2.2.2, < 4.0)
35
+ regexp_parser (>= 1.8, < 3.0)
36
+ rexml
37
+ rubocop-ast (>= 1.12.0, < 2.0)
38
+ ruby-progressbar (~> 1.7)
39
+ unicode-display_width (>= 1.4.0, < 3.0)
40
+ rubocop-ast (1.15.0)
41
+ parser (>= 3.0.1.1)
42
+ ruby-progressbar (1.11.0)
43
+ unicode-display_width (2.1.0)
44
+
45
+ PLATFORMS
46
+ arm64-darwin-21
47
+
48
+ DEPENDENCIES
49
+ just_enum!
50
+ rake (~> 13.0)
51
+ rspec (~> 3.0)
52
+ rubocop (~> 1.7)
53
+
54
+ BUNDLED WITH
55
+ 2.2.22
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # JustEnum
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/just_enum`. 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 'just_enum'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install just_enum
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. Then, run `rake spec` to run the tests. 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 the created tag, 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]/just_enum.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "just_enum"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
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
data/just_enum.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/just_enum/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "just_enum"
7
+ spec.version = JustEnum::VERSION
8
+ spec.authors = ["Rafal Piekara"]
9
+ spec.email = ["rafal@piekara.me"]
10
+
11
+ spec.summary = "Simple enum module extending Ruby with TypeScript like enums"
12
+ spec.description = "Module extends PORO classes in enums and generate helpers for other POROS using those enums"
13
+ spec.homepage = "https://grubykodzi.pl"
14
+ spec.required_ruby_version = ">= 2.4.0"
15
+
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/rafpiek/just_enum"
19
+ spec.metadata["changelog_uri"] = "https://github.com/rafpiek/just_enum"
20
+
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+ end
@@ -0,0 +1,25 @@
1
+ module JustEnum
2
+ class Base
3
+ class << self
4
+ def enum(options, mirror: false)
5
+ if mirror
6
+ options = options.each_with_object({}) do |i, hash|
7
+ hash[i] = i.to_s
8
+ end
9
+ end
10
+ define_singleton_method(:options) { options }
11
+ case options
12
+ when Array
13
+ options.each_with_index do |opt, index|
14
+ define_singleton_method(opt) { index }
15
+ end
16
+ when Hash
17
+ options.each_pair do |key, value|
18
+ define_singleton_method(key) { value }
19
+ end
20
+ end
21
+
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,18 @@
1
+ require_relative 'string'
2
+ module JustEnum::Enum
3
+ def enumerate(field_key, enum_class, field_value)
4
+ has_hash_options = enum_class.options.is_a? Hash
5
+ if has_hash_options
6
+ define_method("str_#{field_key.to_s}") { enum_class.options[field_value.to_sym] }
7
+ enum_class.options.each_pair do |key, value|
8
+ define_method("#{enum_class.name.snakecase}_#{key.to_s}?") { field_value == value }
9
+ end
10
+ else
11
+ define_method("str_#{field_key.to_s}") { enum_class.options[field_value].to_s }
12
+ enum_class.options.each_with_index do |opt, index|
13
+ define_method("#{enum_class.name.snakecase}_#{opt.to_s}?") { field_value == index }
14
+ end
15
+ end
16
+ define_method("_#{field_key.to_s}") { field_value }
17
+ end
18
+ end
@@ -0,0 +1,31 @@
1
+
2
+ class String
3
+
4
+ # Underscore a string such that camelcase, dashes and spaces are
5
+ # replaced by underscores. This is the reverse of {#camelcase},
6
+ # albeit not an exact inverse.
7
+ #
8
+ # "SnakeCase".snakecase #=> "snake_case"
9
+ # "Snake-Case".snakecase #=> "snake_case"
10
+ # "Snake Case".snakecase #=> "snake_case"
11
+ # "Snake - Case".snakecase #=> "snake_case"
12
+ #
13
+ # Note, this method no longer converts `::` to `/`, in that case
14
+ # use the {#pathize} method instead.
15
+
16
+ def snakecase
17
+ #gsub(/::/, '/').
18
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
19
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
20
+ tr('-', '_').
21
+ gsub(/\s/, '_').
22
+ gsub(/__+/, '_').
23
+ downcase
24
+ end
25
+
26
+ #
27
+ alias_method :underscore, :snakecase
28
+
29
+ # TODO: Add *separators to #snakecase, like camelcase.
30
+
31
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JustEnum
4
+ VERSION = "0.1.0"
5
+ end
data/lib/just_enum.rb ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "just_enum/version"
4
+ require_relative "just_enum/base"
5
+ require_relative "just_enum/enum"
6
+
7
+ module JustEnum
8
+ class Error < StandardError; end
9
+ # Your code goes here...
10
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: just_enum
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Rafal Piekara
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-12-24 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Module extends PORO classes in enums and generate helpers for other POROS
14
+ using those enums
15
+ email:
16
+ - rafal@piekara.me
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".gitignore"
22
+ - ".rspec"
23
+ - ".rubocop.yml"
24
+ - Gemfile
25
+ - Gemfile.lock
26
+ - README.md
27
+ - Rakefile
28
+ - bin/console
29
+ - bin/setup
30
+ - just_enum.gemspec
31
+ - lib/just_enum.rb
32
+ - lib/just_enum/base.rb
33
+ - lib/just_enum/enum.rb
34
+ - lib/just_enum/string.rb
35
+ - lib/just_enum/version.rb
36
+ homepage: https://grubykodzi.pl
37
+ licenses: []
38
+ metadata:
39
+ homepage_uri: https://grubykodzi.pl
40
+ source_code_uri: https://github.com/rafpiek/just_enum
41
+ changelog_uri: https://github.com/rafpiek/just_enum
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 2.4.0
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubygems_version: 3.1.4
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Simple enum module extending Ruby with TypeScript like enums
61
+ test_files: []