ez_attributes 0.1.0 → 0.3.0

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: e1a1656644b5f4125ced596740a565eb787d9c55d9a355cf819f6064707edd3d
4
- data.tar.gz: f4ac601d6787ac40cee85efbc1fafe3f8664fbf5fbf90efc53ba0a92652bd30e
3
+ metadata.gz: 4d004aaed015e6616f82058b5eb28d543be87d27a489ce9736bb558273405a4f
4
+ data.tar.gz: 5cda1cf13ae92c1b390987d5b92bd1f2c3cd5f72aab9380d206198d20cf137d1
5
5
  SHA512:
6
- metadata.gz: aedc2203f94400db3327994c3b568e3d2e419b61429dec040ccb4b7bf932ced79433e2c07db92ae2c75c223ac73198c8061e7da6754b91031cf45227a3ad864c
7
- data.tar.gz: 81bc482c0f146b8e95a666e1bf2e4631eeaaa7b9ed18dd120b1a473ccf7796232ef24db28537b44c147ef1d27d97d3ff2ce3ba1bfc70c64b2607233398477521
6
+ metadata.gz: ce17bb4e9bfc325c49af790b476e77110c42a1f67cefe663ac80c44cb4dd0e2e30f407292fb7c446b259fa46e89f836682ec39f1eafe8c3c9550b6f443fc28ec
7
+ data.tar.gz: 84e2e39b8c054f2a1dccfbfa0160c2f83786f1bf91b73784e065c00338887913e07aa59a934ce3f6c29e36d7cc640a5fee128c1f7cdb850edf71717ccb27370d
@@ -11,12 +11,12 @@ jobs:
11
11
  runs-on: ubuntu-latest
12
12
  strategy:
13
13
  matrix:
14
- ruby: [2.5, 2.6, 2.7]
14
+ ruby: [2.5, 2.6, 2.7, '3.0']
15
15
 
16
16
  steps:
17
17
  - uses: actions/checkout@v2
18
18
  - name: Set up Ruby ${{ matrix.ruby }}
19
- uses: actions/setup-ruby@v1
19
+ uses: ruby/setup-ruby@v1
20
20
  with:
21
21
  ruby-version: ${{ matrix.ruby }}
22
22
  - name: Build and test with Rake
@@ -24,5 +24,5 @@ jobs:
24
24
  gem install bundler
25
25
  bundle install --jobs 4 --retry 3
26
26
  bundle exec rake
27
- - name: Rubocop Linter Action
28
- run: bundle exec rubocop --parallel
27
+ - name: Linter Action
28
+ run: bundle exec standardrb
data/.standard.yml ADDED
@@ -0,0 +1 @@
1
+ format: progress
data/CHANGELOG.md CHANGED
@@ -7,10 +7,56 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.3.0] - 2021-10-25
11
+
10
12
  <!-- ### Added -->
11
- <!-- ### Changed -->
12
13
 
13
- ## <!-- ### Removed -->
14
+ ### Changed
15
+
16
+ - Allow configuring EzAttributes (PR #3)
17
+
18
+ ```ruby
19
+ class User
20
+ extend EzAttributes.configure(getters: false)
21
+
22
+ attributes :name, :age, email: 'guest@user.com'
23
+ end
24
+
25
+ User.new(name: 'Matz', age: 22).name
26
+ # NoMethodError (undefined method `name' for #<User:0x000055bac152f130>)
27
+ ```
28
+
29
+ - Replace Rubocop with StandardRB
30
+
31
+ <!-- ### Removed -->
32
+
33
+ ## [0.2.2] - 2021-04-30
34
+
35
+ ### Changed
36
+
37
+ - Fix shared state between class instances. (Commit 759abc7cb971e6a30448f5e9557be4542128d7cf)
38
+
39
+ ## [0.2.1] - 2021-01-20
40
+
41
+ ### Changed
42
+
43
+ - Do not generate getter for attribute named `class`. This prevents overriding the `Object#class` method.
44
+
45
+ ## [0.2.0] - 2021-01-19
46
+
47
+ ### Added
48
+
49
+ - Allow using reserved words as attributes.
50
+
51
+ ```ruby
52
+ Class.new do
53
+ extend EzAttributes
54
+
55
+ # This would break in 0.1.0.
56
+ # Now it works.
57
+ attributes :class, :if
58
+ end
59
+ ```
14
60
 
15
61
  ## [0.1.0] - 2020-11-24
16
62
 
@@ -18,5 +64,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
18
64
 
19
65
  - Basic module to define class initializers with keyword args.
20
66
 
21
- [unreleased]: https://github.com/MatheusRich/ez_attributes/compare/v0.1.0...HEAD
22
- [0.1.0]: https://github.com/MatheusRich/ez_attributes/releases/tag/v0.1.0
67
+ [unreleased]: https://github.com/MatheusRich/ez_attributes/compare/v0.3.0...HEAD
68
+ [0.3.0]: https://github.com/MatheusRich/ez_attributes/releases/tag/v0.3.0
69
+ [0.2.2]: https://github.com/MatheusRich/ez_attributes/releases/tag/v0.2.2
70
+ [0.2.1]: https://github.com/MatheusRich/ez_attributes/releases/tag/v0.2.1
71
+ [0.2.0]: https://github.com/MatheusRich/ez_attributes/releases/tag/v0.2.0
72
+ [0.1.0]: https://github.com/MatheusRich/ez_attributes/releases/tag/v0.2.0
data/Gemfile CHANGED
@@ -1,21 +1,19 @@
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 f_service.gemspec
6
6
  gemspec
7
7
 
8
- gem 'rake', '~> 13.0.0'
9
- gem 'rubocop', require: false
10
- gem 'rubocop-performance', require: false
11
- gem 'rubocop-rspec', require: false
12
- gem 'solargraph'
8
+ gem "rake", "~> 13.0.0"
9
+ gem "standard"
10
+ gem "solargraph"
13
11
 
14
12
  group :docs do
15
- gem 'yard'
13
+ gem "yard"
16
14
  end
17
15
 
18
16
  group :test do
19
- gem 'rspec', '~> 3.0'
20
- gem 'simplecov', require: false
17
+ gem "rspec", "~> 3.0"
18
+ gem "simplecov", require: false
21
19
  end
data/Gemfile.lock CHANGED
@@ -1,12 +1,12 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ez_attributes (0.1.0)
4
+ ez_attributes (0.3.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- ast (2.4.1)
9
+ ast (2.4.2)
10
10
  backport (1.1.2)
11
11
  benchmark (0.1.0)
12
12
  diff-lcs (1.4.4)
@@ -17,14 +17,15 @@ GEM
17
17
  mini_portile2 (2.4.0)
18
18
  nokogiri (1.10.10)
19
19
  mini_portile2 (~> 2.4.0)
20
- parallel (1.20.1)
20
+ parallel (1.21.0)
21
21
  parser (2.7.2.0)
22
22
  ast (~> 2.4.1)
23
23
  rainbow (3.0.0)
24
24
  rake (13.0.1)
25
+ regexp_parser (2.1.1)
25
26
  reverse_markdown (2.0.0)
26
27
  nokogiri
27
- rexml (3.2.4)
28
+ rexml (3.2.5)
28
29
  rspec (3.10.0)
29
30
  rspec-core (~> 3.10.0)
30
31
  rspec-expectations (~> 3.10.0)
@@ -38,19 +39,21 @@ GEM
38
39
  diff-lcs (>= 1.2.0, < 2.0)
39
40
  rspec-support (~> 3.10.0)
40
41
  rspec-support (3.10.0)
41
- rubocop (0.82.0)
42
- jaro_winkler (~> 1.5.1)
42
+ rubocop (0.92.0)
43
43
  parallel (~> 1.10)
44
- parser (>= 2.7.0.1)
44
+ parser (>= 2.7.1.5)
45
45
  rainbow (>= 2.2.2, < 4.0)
46
+ regexp_parser (>= 1.7)
46
47
  rexml
48
+ rubocop-ast (>= 0.5.0)
47
49
  ruby-progressbar (~> 1.7)
48
50
  unicode-display_width (>= 1.4.0, < 2.0)
49
- rubocop-performance (1.7.1)
50
- rubocop (>= 0.82.0)
51
- rubocop-rspec (1.41.0)
52
- rubocop (>= 0.68.1)
53
- ruby-progressbar (1.10.1)
51
+ rubocop-ast (1.4.1)
52
+ parser (>= 2.7.1.5)
53
+ rubocop-performance (1.8.1)
54
+ rubocop (>= 0.87.0)
55
+ rubocop-ast (>= 0.4.0)
56
+ ruby-progressbar (1.11.0)
54
57
  simplecov (0.19.1)
55
58
  docile (~> 1.1)
56
59
  simplecov-html (~> 0.11)
@@ -69,9 +72,12 @@ GEM
69
72
  thor (~> 1.0)
70
73
  tilt (~> 2.0)
71
74
  yard (~> 0.9, >= 0.9.24)
75
+ standard (0.7)
76
+ rubocop (= 0.92)
77
+ rubocop-performance (= 1.8.1)
72
78
  thor (1.0.1)
73
79
  tilt (2.0.10)
74
- unicode-display_width (1.7.0)
80
+ unicode-display_width (1.8.0)
75
81
  yard (0.9.25)
76
82
 
77
83
  PLATFORMS
@@ -81,12 +87,10 @@ DEPENDENCIES
81
87
  ez_attributes!
82
88
  rake (~> 13.0.0)
83
89
  rspec (~> 3.0)
84
- rubocop
85
- rubocop-performance
86
- rubocop-rspec
87
90
  simplecov
88
91
  solargraph
92
+ standard
89
93
  yard
90
94
 
91
95
  BUNDLED WITH
92
- 2.1.4
96
+ 2.2.29
data/README.md CHANGED
@@ -50,24 +50,51 @@ class User
50
50
  attributes :name, :age, email: 'guest@user.com'
51
51
  end
52
52
 
53
- User.new(name: 'Matz', age: 22)
53
+ user = User.new(name: 'Matz', age: 22)
54
54
  # => #<User:0x000055bac152f130 @name="Matz", @age=22, @email="guest@user.com">
55
55
 
56
56
  # EzAttributes will add getters for all fields too.
57
- User.name
57
+ user.name
58
58
  # => "Matz"
59
59
  ```
60
60
 
61
+ ### Configuration
62
+
63
+ You can disable getters:
64
+
65
+ ```ruby
66
+ class User
67
+ extend EzAttributes.configure(getters: false)
68
+
69
+ attributes :name, :age, email: 'guest@user.com'
70
+ end
71
+
72
+ u = User.new(name: 'Matz', age: 22)
73
+ # => #<User:0x000055bac152f130 @name="Matz", @age=22, @email="guest@user.com">
74
+
75
+ u.name
76
+ # NoMethodError (undefined method `name' for #<User:0x000055bac152f130>)
77
+
78
+ ```
79
+
61
80
  ## Development
62
81
 
63
- 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.
82
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
83
+ `rake spec` to run the tests. You can also run `bin/console` for an interactive
84
+ prompt that will allow you to experiment.
64
85
 
65
- 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).
86
+ To install this gem onto your local machine, run `bundle exec rake install`. To
87
+ release a new version, update the version number in `version.rb`, and then run
88
+ `bundle exec rake release`, which will create a git tag for the version, push
89
+ git commits and tags, and push the `.gem` file to
90
+ [rubygems.org](https://rubygems.org).
66
91
 
67
92
  ## Contributing
68
93
 
69
- Bug reports and pull requests are welcome on GitHub at https://github.com/MatheusRich/ez_attributes.
94
+ Bug reports and pull requests are welcome on GitHub at
95
+ https://github.com/MatheusRich/ez_attributes.
70
96
 
71
97
  ## License
72
98
 
73
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
99
+ The gem is available as open source under the terms of the [MIT
100
+ License](https://opensource.org/licenses/MIT).
data/Rakefile CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'bundler/gem_tasks'
4
- require 'rspec/core/rake_task'
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
5
 
6
6
  RSpec::Core::RakeTask.new(:spec)
7
7
 
@@ -1,29 +1,29 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'lib/ez_attributes'
3
+ require_relative "lib/ez_attributes"
4
4
 
5
5
  Gem::Specification.new do |spec|
6
- spec.name = 'ez_attributes'
7
- spec.version = EzAttributes::VERSION
8
- spec.authors = ['Matheus Richard']
9
- spec.email = ['matheusrichardt@gmail.com']
6
+ spec.name = "ez_attributes"
7
+ spec.version = EzAttributes::VERSION
8
+ spec.authors = ["Matheus Richard"]
9
+ spec.email = ["matheusrichardt@gmail.com"]
10
10
 
11
- spec.summary = 'Easily define initializers with keyword args.'
12
- spec.description = 'Easily define initializers with keyword args. It supports required and optional args.'
13
- spec.homepage = 'https://github.com/MatheusRich/ez_attributes.git'
14
- spec.license = 'MIT'
15
- spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
11
+ spec.summary = "Easily define initializers with keyword args."
12
+ spec.description = "Easily define initializers with keyword args. It supports required and optional args."
13
+ spec.homepage = "https://github.com/MatheusRich/ez_attributes.git"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
16
16
 
17
- spec.metadata['homepage_uri'] = spec.homepage
18
- spec.metadata['source_code_uri'] = spec.homepage
19
- spec.metadata['changelog_uri'] = 'https://github.com/MatheusRich/ez_attributes/blob/main/CHANGELOG.md'
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = spec.homepage
19
+ spec.metadata["changelog_uri"] = "https://github.com/MatheusRich/ez_attributes/blob/main/CHANGELOG.md"
20
20
 
21
21
  # Specify which files should be added to the gem when it is released.
22
22
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
23
  spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
24
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
25
  end
26
- spec.bindir = 'exe'
27
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
- spec.require_paths = ['lib']
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
29
  end
data/lib/ez_attributes.rb CHANGED
@@ -15,29 +15,44 @@
15
15
  # => #<User:0x000055bac152f130 @name="Matheus", @age=22, @email="guest@user.com">
16
16
  module EzAttributes
17
17
  # Gem version
18
- VERSION = '0.1.0'
18
+ VERSION = "0.3.0"
19
19
 
20
- # Defines multiple keyword arguments for a class initializer
21
- def attributes(*args, **args_with_default)
22
- required_args = args.map { |name| "#{name}:" }
23
- optional_args = args_with_default.map { |name, _| "#{name}: __args_with_default[:#{name}]" }
24
- init_args = (required_args + optional_args).join(', ')
20
+ # Attributes that won't have a getter to prevent conflicts with default methods
21
+ EXCEPTIONS = [:class].freeze
25
22
 
26
- define_method('__args_with_default', -> { args_with_default.dup })
27
- private :__args_with_default
23
+ def self.extended(other)
24
+ other.extend configure
25
+ end
26
+
27
+ def self.configure(getters: true)
28
+ mod = Module.new
29
+
30
+ mod.module_eval do
31
+ # Defines multiple keyword arguments for a class initializer
32
+ define_method :attributes do |*args, **args_with_default|
33
+ required_args = args.map { |name| "#{name}:" }
34
+ optional_args = args_with_default.map { |name, _| "#{name}: __args_with_default[:#{name}]" }
35
+ init_args = (required_args + optional_args).join(", ")
36
+
37
+ define_method("__args_with_default", -> { Marshal.load(Marshal.dump(args_with_default)) })
38
+ private :__args_with_default
28
39
 
29
- all_args = args + args_with_default.keys
30
- attr_reader(*all_args)
40
+ all_args = args + args_with_default.keys
41
+ attr_reader(*(all_args - EXCEPTIONS)) if getters
31
42
 
32
- class_eval <<~RUBY, __FILE__, __LINE__ + 1
33
- def initialize(#{init_args})
34
- #{all_args.map { |name| "@#{name} = #{name}" }.join('; ')}
43
+ class_eval <<~RUBY, __FILE__, __LINE__ + 1
44
+ def initialize(#{init_args})
45
+ #{all_args.map { |name| "@#{name} = binding.local_variable_get(:#{name})" }.join("; ")}
46
+ end
47
+ RUBY
35
48
  end
36
- RUBY
37
- end
38
49
 
39
- # Defines a single keyword argument for a class initializer
40
- def attribute(name)
41
- attributes(name)
50
+ # Defines a single keyword argument for a class initializer
51
+ define_method :attribute do |name|
52
+ attributes(name)
53
+ end
54
+ end
55
+
56
+ mod
42
57
  end
43
58
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ez_attributes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matheus Richard
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-11-25 00:00:00.000000000 Z
11
+ date: 2021-10-25 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Easily define initializers with keyword args. It supports required and
14
14
  optional args.
@@ -21,8 +21,8 @@ files:
21
21
  - ".github/workflows/tests-and-linter.yml"
22
22
  - ".gitignore"
23
23
  - ".rspec"
24
- - ".rubocop.yml"
25
24
  - ".ruby-version"
25
+ - ".standard.yml"
26
26
  - CHANGELOG.md
27
27
  - Gemfile
28
28
  - Gemfile.lock
@@ -40,7 +40,7 @@ metadata:
40
40
  homepage_uri: https://github.com/MatheusRich/ez_attributes.git
41
41
  source_code_uri: https://github.com/MatheusRich/ez_attributes.git
42
42
  changelog_uri: https://github.com/MatheusRich/ez_attributes/blob/main/CHANGELOG.md
43
- post_install_message:
43
+ post_install_message:
44
44
  rdoc_options: []
45
45
  require_paths:
46
46
  - lib
@@ -55,9 +55,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
55
55
  - !ruby/object:Gem::Version
56
56
  version: '0'
57
57
  requirements: []
58
- rubyforge_project:
59
- rubygems_version: 2.7.6.2
60
- signing_key:
58
+ rubygems_version: 3.2.25
59
+ signing_key:
61
60
  specification_version: 4
62
61
  summary: Easily define initializers with keyword args.
63
62
  test_files: []
data/.rubocop.yml DELETED
@@ -1,20 +0,0 @@
1
- require:
2
- - rubocop-rspec
3
- - rubocop-performance
4
-
5
- AllCops:
6
- NewCops: enable
7
-
8
- Layout/LineLength:
9
- Max: 120
10
-
11
- Metrics/BlockLength:
12
- Exclude:
13
- - "spec/**/*"
14
-
15
- Metrics/AbcSize:
16
- Enabled: false
17
-
18
- Style/DocumentationMethod:
19
- Enabled: true
20
-