nd-enum 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0c3a846e4f2d4625cfa4defce4320e1b6a66d2328a5bab3aad579fee30bddd2f
4
- data.tar.gz: b872195ecf8bfaf2d381cf8eb07647c1317d71b7fd49a7044bcd8e8256f8b98a
3
+ metadata.gz: e8c39fe2ee839c606ce8bba0ed32181fff0ab45b39d091f6d5f4070d560eff9e
4
+ data.tar.gz: 190d2bd1a22ed5b7551b91a1ffe086e8f80d2010228cc568c86d1dc8b7d99fab
5
5
  SHA512:
6
- metadata.gz: bab3df4478ed3eda30c254a380c69d29c690abbc371e03fc923c353fb90e5c3259aef0839cde49402a9961d6da8035006356195df81a7b9cc43f9f5d626b5019
7
- data.tar.gz: d772f43733c33204c0acaafb9d0d38c797cebb603efc4ae311990f8afac2eb15a4538ade187b5f4df8dc489cf6e7ddd54548a0ef021663ad735f619cc37d1006
6
+ metadata.gz: f4adb87f1df4b296625c32c3eedaef521bc5fccf8de94ef8bf77644be19324e5395cbed035cb96bd023a8555c30ca7843a802c2289f553ef310f5fb2f9e788a3
7
+ data.tar.gz: 8df576525c8d46cdb705e7ade16a4158e0d0387baaf7429239fc9e74e7ddb6dd6878787d77dde8fa0f8a8d3c61b55d58e64aea82cd6994d447c30568336e5884
data/.rspec CHANGED
@@ -1,3 +1,6 @@
1
1
  --format documentation
2
2
  --color
3
3
  --require spec_helper
4
+ --require mocha
5
+ --require active_record
6
+ --require pry
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.1.2] - 2022-08-08
4
+
5
+ - Add specs
6
+
7
+ ## [0.1.1] - 2022-08-07
8
+
9
+ - Fix bugs
10
+
3
11
  ## [0.1.0] - 2022-08-07
4
12
 
5
13
  - Initial release
data/Gemfile CHANGED
@@ -1,10 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- source "https://rubygems.org"
3
+ source 'https://rubygems.org'
4
4
 
5
5
  # Specify your gem's dependencies in nd-enum.gemspec
6
6
  gemspec
7
7
 
8
- gem "rake", "~> 13.0"
9
-
10
- gem "rspec", "~> 3.0"
8
+ gem 'rake', '~> 13.0'
9
+ gem 'rspec', '~> 3.0'
10
+ gem 'mocha', '~> 1.13'
11
+ gem 'pry', '~> 0.13'
12
+ gem 'guard', '~> 2.18'
13
+ gem 'guard-rspec', '~> 4.7', require: false
data/Gemfile.lock CHANGED
@@ -1,13 +1,62 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- nd-enum (0.1.0)
4
+ nd-enum (0.1.1)
5
+ activerecord (>= 6.0.0)
6
+ activesupport (>= 6.0.0)
5
7
 
6
8
  GEM
7
9
  remote: https://rubygems.org/
8
10
  specs:
11
+ activemodel (7.0.3.1)
12
+ activesupport (= 7.0.3.1)
13
+ activerecord (7.0.3.1)
14
+ activemodel (= 7.0.3.1)
15
+ activesupport (= 7.0.3.1)
16
+ activesupport (7.0.3.1)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (>= 1.6, < 2)
19
+ minitest (>= 5.1)
20
+ tzinfo (~> 2.0)
21
+ coderay (1.1.3)
22
+ concurrent-ruby (1.1.10)
9
23
  diff-lcs (1.5.0)
24
+ ffi (1.15.5)
25
+ formatador (1.1.0)
26
+ guard (2.18.0)
27
+ formatador (>= 0.2.4)
28
+ listen (>= 2.7, < 4.0)
29
+ lumberjack (>= 1.0.12, < 2.0)
30
+ nenv (~> 0.1)
31
+ notiffany (~> 0.0)
32
+ pry (>= 0.13.0)
33
+ shellany (~> 0.0)
34
+ thor (>= 0.18.1)
35
+ guard-compat (1.2.1)
36
+ guard-rspec (4.7.3)
37
+ guard (~> 2.1)
38
+ guard-compat (~> 1.1)
39
+ rspec (>= 2.99.0, < 4.0)
40
+ i18n (1.12.0)
41
+ concurrent-ruby (~> 1.0)
42
+ listen (3.7.1)
43
+ rb-fsevent (~> 0.10, >= 0.10.3)
44
+ rb-inotify (~> 0.9, >= 0.9.10)
45
+ lumberjack (1.2.8)
46
+ method_source (1.0.0)
47
+ minitest (5.16.2)
48
+ mocha (1.14.0)
49
+ nenv (0.3.0)
50
+ notiffany (0.1.3)
51
+ nenv (~> 0.1)
52
+ shellany (~> 0.0)
53
+ pry (0.14.1)
54
+ coderay (~> 1.1)
55
+ method_source (~> 1.0)
10
56
  rake (13.0.6)
57
+ rb-fsevent (0.11.1)
58
+ rb-inotify (0.10.1)
59
+ ffi (~> 1.0)
11
60
  rspec (3.11.0)
12
61
  rspec-core (~> 3.11.0)
13
62
  rspec-expectations (~> 3.11.0)
@@ -21,12 +70,20 @@ GEM
21
70
  diff-lcs (>= 1.2.0, < 2.0)
22
71
  rspec-support (~> 3.11.0)
23
72
  rspec-support (3.11.0)
73
+ shellany (0.0.1)
74
+ thor (1.2.1)
75
+ tzinfo (2.0.5)
76
+ concurrent-ruby (~> 1.0)
24
77
 
25
78
  PLATFORMS
26
79
  x86_64-darwin-21
27
80
 
28
81
  DEPENDENCIES
82
+ guard (~> 2.18)
83
+ guard-rspec (~> 4.7)
84
+ mocha (~> 1.13)
29
85
  nd-enum!
86
+ pry (~> 0.13)
30
87
  rake (~> 13.0)
31
88
  rspec (~> 3.0)
32
89
 
data/Guardfile ADDED
@@ -0,0 +1,70 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ # directories %w(app lib config test spec features) \
6
+ # .select{|d| Dir.exist?(d) ? d : UI.warning("Directory #{d} does not exist")}
7
+
8
+ ## Note: if you are using the `directories` clause above and you are not
9
+ ## watching the project directory ('.'), then you will want to move
10
+ ## the Guardfile to a watched dir and symlink it back, e.g.
11
+ #
12
+ # $ mkdir config
13
+ # $ mv Guardfile config/
14
+ # $ ln -s config/Guardfile .
15
+ #
16
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
17
+
18
+ # Note: The cmd option is now required due to the increasing number of ways
19
+ # rspec may be run, below are examples of the most common uses.
20
+ # * bundler: 'bundle exec rspec'
21
+ # * bundler binstubs: 'bin/rspec'
22
+ # * spring: 'bin/rspec' (This will use spring if running and you have
23
+ # installed the spring binstubs per the docs)
24
+ # * zeus: 'zeus rspec' (requires the server to be started separately)
25
+ # * 'just' rspec: 'rspec'
26
+
27
+ guard :rspec, cmd: "bundle exec rspec" do
28
+ require "guard/rspec/dsl"
29
+ dsl = Guard::RSpec::Dsl.new(self)
30
+
31
+ # Feel free to open issues for suggestions and improvements
32
+
33
+ # RSpec files
34
+ rspec = dsl.rspec
35
+ watch(rspec.spec_helper) { rspec.spec_dir }
36
+ watch(rspec.spec_support) { rspec.spec_dir }
37
+ watch(rspec.spec_files)
38
+
39
+ # Ruby files
40
+ ruby = dsl.ruby
41
+ dsl.watch_spec_files_for(ruby.lib_files)
42
+
43
+ # Rails files
44
+ rails = dsl.rails(view_extensions: %w(erb haml slim))
45
+ dsl.watch_spec_files_for(rails.app_files)
46
+ dsl.watch_spec_files_for(rails.views)
47
+
48
+ watch(rails.controllers) do |m|
49
+ [
50
+ rspec.spec.call("routing/#{m[1]}_routing"),
51
+ rspec.spec.call("controllers/#{m[1]}_controller"),
52
+ rspec.spec.call("acceptance/#{m[1]}")
53
+ ]
54
+ end
55
+
56
+ # Rails config changes
57
+ watch(rails.spec_helper) { rspec.spec_dir }
58
+ watch(rails.routes) { "#{rspec.spec_dir}/routing" }
59
+ watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }
60
+
61
+ # Capybara features specs
62
+ watch(rails.view_dirs) { |m| rspec.spec.call("features/#{m[1]}") }
63
+ watch(rails.layouts) { |m| rspec.spec.call("features/#{m[1]}") }
64
+
65
+ # Turnip features and steps
66
+ watch(%r{^spec/acceptance/(.+)\.feature$})
67
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
68
+ Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
69
+ end
70
+ end
data/lib/nd/enum/base.rb CHANGED
@@ -1,50 +1,35 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'forwardable'
4
+ require 'active_support'
5
+
3
6
  module ND
4
- module Base
5
- extend ActiveSupport::Concern
7
+ module Enum
8
+ module Base
9
+ extend ActiveSupport::Concern
6
10
 
7
- included do
8
- class << self
9
- include Enumerable
10
- extend Forwardable
11
+ included do
12
+ class << self
13
+ include Enumerable
14
+ extend Forwardable
11
15
 
12
- def_delegators :all, :size, :length, :[], :empty?, :last, :index
16
+ def_delegators :all, :size, :length, :[], :empty?, :last, :index
13
17
 
14
- def each(&block)
15
- all.each(&block)
16
- end
18
+ def each(&block)
19
+ all.each(&block)
20
+ end
17
21
 
18
- def to_h
19
- all.map { |value| [value.to_sym, value] }.to_h
20
- end
22
+ def to_h
23
+ all.map { |value| [value.to_sym, value] }.to_h
24
+ end
21
25
 
22
- def [](value)
23
- value.is_a?(Integer) ? all[value] : to_h[value.to_sym]
24
- end
26
+ def [](value)
27
+ value.is_a?(Integer) ? all[value] : to_h[value.to_sym]
28
+ end
25
29
 
26
- # TODO: Warning if not defined
27
- # `t(:foo)`
28
- # fr:
29
- # table_name:
30
- # attribute:
31
- # base:
32
- # foo: Foo
33
- #
34
- # or
35
- #
36
- # `t(:foo, :scope_a)`
37
- # fr:
38
- # table_name:
39
- # attribute:
40
- # base:
41
- # foo: Foo
42
- # scope_a:
43
- # foo: Foo A
44
- # scope_b:
45
- # foo: Foo B
46
- def t(value, scope = :base)
47
- I18n.t(value, scope: "#{model.table_name}.#{attribute}.#{scope}")
30
+ def t(value, scope = :base)
31
+ I18n.t(value, scope: "#{model.table_name}.#{attribute}.#{scope}")
32
+ end
48
33
  end
49
34
  end
50
35
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ND
4
4
  module Enum
5
- VERSION = '0.1.1'
5
+ VERSION = '0.1.2'
6
6
  end
7
7
  end
data/lib/nd/enum.rb CHANGED
@@ -1,27 +1,38 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'active_record'
4
+ require 'active_record/enum.rb'
5
+
6
+ require 'active_support'
7
+ require 'active_support/core_ext/string/inflections.rb'
8
+
3
9
  require_relative 'enum/version'
10
+ require_relative 'enum/base'
4
11
 
5
12
  module ND
6
13
  module Enum
7
- class << self
8
- def nd_enum(db: false, i18n: {}, **configuration)
9
- set_options(binding)
10
- enum_module = define_module(@options)
14
+ extend ActiveSupport::Concern
11
15
 
12
- define_db_enum(db, enum_module) if @options[:db]
16
+ included do
17
+ def self.nd_enum(db: false, i18n: {}, **configuration)
18
+ options = ND::Enum.set_options(binding, self)
19
+ enum_module = ND::Enum.define_module(options)
13
20
 
14
- const_set(@options[:attribute].to_s.camelize, enum_module)
15
- end
21
+ ND::Enum.define_db_enum(options, enum_module) if options[:db]
16
22
 
17
- private
23
+ const_set(options[:attribute].to_s.camelize, enum_module)
24
+ end
25
+ end
18
26
 
19
- def set_options(caller_binding)
20
- @options = method(:nd_enum).parameters.each_with_object({}) do |(_, name), options|
27
+ class << self
28
+ def set_options(caller_binding, caller_class)
29
+ options = caller_class.method(:nd_enum).parameters.each_with_object({}) do |(_, name), options|
21
30
  options[name.to_sym] = caller_binding.local_variable_get(name)
22
31
  end
23
- @options[:attribute], @options[:values] = @options.delete(:configuration).to_a.first
24
- @options[:model] = self
32
+ options[:attribute], options[:values] = options.delete(:configuration).to_a.first
33
+ options[:model] = caller_class
34
+
35
+ options
25
36
  end
26
37
 
27
38
  def define_module(options)
@@ -30,7 +41,7 @@ module ND
30
41
 
31
42
  # Public methods
32
43
 
33
- define_singleton_method(:all) { options[:values] }
44
+ define_singleton_method(:all) { options[:values] }
34
45
 
35
46
  # Private methods
36
47
 
@@ -52,9 +63,13 @@ module ND
52
63
  end
53
64
 
54
65
  def define_db_enum(options, enum_module)
55
- options = options.is_a?(Hash) ? options : {}
56
- enum(@attribute => enum_module.to_h, **options)
66
+ enum_options = options[:db].is_a?(Hash) ? options[:db] : {}
67
+ options[:model].enum(options[:attribute] => enum_module.to_h, **enum_options)
57
68
  end
58
69
  end
59
70
  end
60
71
  end
72
+
73
+ class ActiveRecord::Base
74
+ include ND::Enum
75
+ end
data/nd-enum.gemspec CHANGED
@@ -29,7 +29,8 @@ Gem::Specification.new do |spec|
29
29
  spec.require_paths = ['lib']
30
30
 
31
31
  # Uncomment to register a new dependency of your gem
32
- # spec.add_dependency 'example-gem', '~> 1.0'
32
+ spec.add_dependency 'activesupport', '>= 6.0.0'
33
+ spec.add_dependency 'activerecord', '>= 6.0.0'
33
34
 
34
35
  # For more information and examples about making a new gem, check out our
35
36
  # guide at: https://bundler.io/guides/creating_gem.html
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nd-enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Romain Clavel
@@ -9,7 +9,35 @@ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
11
  date: 2022-08-08 00:00:00.000000000 Z
12
- dependencies: []
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 6.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 6.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 6.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 6.0.0
13
41
  description:
14
42
  email:
15
43
  - romain@clavel.io
@@ -21,6 +49,7 @@ files:
21
49
  - CHANGELOG.md
22
50
  - Gemfile
23
51
  - Gemfile.lock
52
+ - Guardfile
24
53
  - LICENSE.txt
25
54
  - README.md
26
55
  - Rakefile