safe_values 1.0.1 → 1.1.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: ba03abf1ce02d1e319edf07d93dfdd9c95c7985a03085a964b75159672483143
4
- data.tar.gz: 1aa9bb295da60cca721fdbb3c8826399845159c4ea5ef7364e3c1cd999494b20
3
+ metadata.gz: 9d47e1d0a43f8ade84d71e875436d7a4fda9ff4a7a8862e085b6ea7e721ceccd
4
+ data.tar.gz: 7582e0bec8b0f3c0aac29f4d8a6119f6e0cfe2281f3d390796250df08eaa5b05
5
5
  SHA512:
6
- metadata.gz: 5848e9df32a06a5a611f8bdc6cc47f1ffec2edcd989c10a834fbaa57588b0a42d73df9f327fd8776fe4c8e2772b8e785f32653ebc713d861ed4d8c84ff044017
7
- data.tar.gz: 7b830c4a56c889e7c64cfeb7bfabfcdc3a90b6b53c27fb99d621ef3cf7e4e1accd745ecaf2e26a19031a19d98b171aa4873797f6eada6c384b2a96cfcb76aef2
6
+ metadata.gz: 4ba2340b5061fadd5c58b263d0d8394f11199503ffeb3e0a03bb46ec6128a4032eb15a838139ee537247ee5a8e8b2269a96eee1d3f335a9274825c7d07425b19
7
+ data.tar.gz: 83c28430ddc41419e1d46082acb92da08cf229ba178aba473e685faedee4029638a64f24c5c505b9bad8a119c32b550e2ef22d758d94ac9cc0bef38945c4dae8
@@ -1,3 +1,3 @@
1
1
  module SafeValues
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
data/lib/value.rb CHANGED
@@ -13,7 +13,9 @@
13
13
  # `ValueType = Value.new(:a, :b, c: default_value)`. The default values to
14
14
  # optional arguments are saved at class creation time and supplied as default
15
15
  # constructor arguments to instances. Default values are aliased, so providing
16
- # mutable defaults is discouraged.
16
+ # mutable defaults is discouraged. Lazily-evaluated defaults can be provided by
17
+ # supplying `Value.lazy { ... }` as a default. The lazy block will be evaluated
18
+ # at each instantiation time.
17
19
  #
18
20
  # Two instance constructors are provided, with positional and keyword arguments.
19
21
  #
@@ -25,6 +27,8 @@
25
27
  # Value types may be constructed with keyword arguments using `with`.
26
28
  # For example: `ValueType.with(a: 1, b: 2, c: 3)`
27
29
  class Value < Struct
30
+ LazyDefault = Struct.new(:proc)
31
+
28
32
  class << self
29
33
  def new(*required_args, **optional_args, &block)
30
34
  arguments = {}
@@ -39,8 +43,14 @@ class Value < Struct
39
43
  keyword_constructor = generate_keyword_constructor(arguments)
40
44
  class_method_module = Module.new do
41
45
  module_eval(keyword_constructor)
42
- define_method(:__constructor_default) do |name|
43
- optional_args.fetch(name)
46
+
47
+ optional_args.each do |name, value|
48
+ method_name = :"__constructor_default_#{name}"
49
+ if value.is_a?(LazyDefault)
50
+ define_method(method_name, &value.proc)
51
+ else
52
+ define_method(method_name) { value }
53
+ end
44
54
  end
45
55
  end
46
56
  clazz.extend(class_method_module)
@@ -57,6 +67,10 @@ class Value < Struct
57
67
  clazz
58
68
  end
59
69
 
70
+ def lazy(&proc)
71
+ LazyDefault.new(proc)
72
+ end
73
+
60
74
  private
61
75
 
62
76
  def validate_names(*params)
@@ -73,7 +87,7 @@ class Value < Struct
73
87
  #
74
88
  # For a Value.new(:a, b: x), will define the method:
75
89
  #
76
- # def initialize(a, b = self.class.__constructor_default(:b))
90
+ # def initialize(a, b = self.class.__constructor_default_b)
77
91
  # super(a, b)
78
92
  # freeze
79
93
  # end
@@ -82,7 +96,7 @@ class Value < Struct
82
96
  if required
83
97
  arg_name
84
98
  else
85
- "#{arg_name} = self.class.__constructor_default(:#{arg_name})"
99
+ "#{arg_name} = self.class.__constructor_default_#{arg_name}"
86
100
  end
87
101
  end
88
102
 
@@ -100,7 +114,7 @@ class Value < Struct
100
114
  #
101
115
  # For a Value.new(:a, b: x), will define the (class) method:
102
116
  #
103
- # def with(a:, b: __constructor_default(:b))
117
+ # def with(a:, b: __constructor_default_b)
104
118
  # self.new(a, b)
105
119
  # end
106
120
  def generate_keyword_constructor(arguments)
@@ -108,7 +122,7 @@ class Value < Struct
108
122
  if required
109
123
  "#{arg_name}:"
110
124
  else
111
- "#{arg_name}: __constructor_default(:#{arg_name})"
125
+ "#{arg_name}: __constructor_default_#{arg_name}"
112
126
  end
113
127
  end
114
128
 
@@ -120,8 +134,8 @@ class Value < Struct
120
134
  end
121
135
  end
122
136
 
123
- def with(hash = {})
124
- return self if hash.empty?
125
- self.class.with(to_h.merge(hash))
137
+ def with(**kwargs)
138
+ return self if kwargs.empty?
139
+ self.class.with(**to_h.merge(kwargs))
126
140
  end
127
141
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: safe_values
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - DMM Eikaiwa
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-04-08 00:00:00.000000000 Z
11
+ date: 2024-10-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -59,20 +59,11 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
- - ".circleci/config.yml"
63
- - ".gitignore"
64
- - ".rspec"
65
- - ".travis.yml"
66
- - Gemfile
67
62
  - LICENSE.txt
68
63
  - README.md
69
- - Rakefile
70
- - bin/console
71
- - bin/setup
72
64
  - lib/safe_values.rb
73
65
  - lib/safe_values/version.rb
74
66
  - lib/value.rb
75
- - safe_values.gemspec
76
67
  homepage: http://github.com/iknow/safe_values
77
68
  licenses:
78
69
  - MIT
@@ -92,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
83
  - !ruby/object:Gem::Version
93
84
  version: '0'
94
85
  requirements: []
95
- rubygems_version: 3.0.3
86
+ rubygems_version: 3.1.6
96
87
  signing_key:
97
88
  specification_version: 4
98
89
  summary: Struct classes with safer constructors.
data/.circleci/config.yml DELETED
@@ -1,94 +0,0 @@
1
- version: 2.1
2
-
3
- executors:
4
- ruby:
5
- parameters:
6
- ruby-version:
7
- type: string
8
- default: "2.6"
9
- gemfile:
10
- type: string
11
- default: "Gemfile"
12
- docker:
13
- - image: circleci/ruby:<< parameters.ruby-version >>
14
- environment:
15
- BUNDLE_JOBS: 3
16
- BUNDLE_RETRY: 3
17
- BUNDLE_PATH: vendor/bundle
18
- RAILS_ENV: test
19
- BUNDLE_GEMFILE: << parameters.gemfile >>
20
-
21
- jobs:
22
- test:
23
- parameters:
24
- ruby-version:
25
- type: string
26
- executor:
27
- name: ruby
28
- ruby-version: << parameters.ruby-version >>
29
- parallelism: 1
30
- steps:
31
- - checkout
32
-
33
- - run:
34
- # Remove the non-appraisal gemfile for safety: we never want to use it.
35
- name: Prepare bundler
36
- command: bundle -v
37
-
38
- - run:
39
- name: Compute a gemfile lock
40
- command: bundle lock && cp "${BUNDLE_GEMFILE}.lock" /tmp/gem-lock
41
-
42
- - restore_cache:
43
- keys:
44
- - safe_values-<< parameters.ruby-version >>-{{ checksum "/tmp/gem-lock" }}
45
- - safe_values-
46
-
47
- - run:
48
- name: Bundle Install
49
- command: bundle check || bundle install
50
-
51
- - save_cache:
52
- key: safe_values-<< parameters.ruby-version >>-{{ checksum "/tmp/gem-lock" }}
53
- paths:
54
- - vendor/bundle
55
-
56
- - run:
57
- name: Run rspec
58
- command: bundle exec rspec --profile 10 --format RspecJunitFormatter --out test_results/rspec.xml --format progress
59
-
60
- - store_test_results:
61
- path: test_results
62
-
63
- publish:
64
- executor: ruby
65
- steps:
66
- - checkout
67
- - run:
68
- name: Setup Rubygems
69
- command: |
70
- mkdir ~/.gem &&
71
- echo -e "---\r\n:rubygems_api_key: $RUBYGEMS_API_KEY" > ~/.gem/credentials &&
72
- chmod 0600 ~/.gem/credentials
73
- - run:
74
- name: Publish to Rubygems
75
- command: |
76
- gem build safe_values.gemspec
77
- gem push safe_values-*.gem
78
-
79
- workflows:
80
- version: 2.1
81
- build:
82
- jobs:
83
- - test:
84
- name: 'ruby 2.5'
85
- ruby-version: "2.5"
86
- - test:
87
- name: 'ruby 2.6'
88
- ruby-version: "2.6"
89
- - publish:
90
- filters:
91
- branches:
92
- only: master
93
- tags:
94
- ignore: /.*/
data/.gitignore DELETED
@@ -1,10 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /_yardoc/
4
- /coverage/
5
- /doc/
6
- /pkg/
7
- /spec/reports/
8
- /tmp/
9
- Gemfile.lock
10
-
data/.rspec DELETED
@@ -1 +0,0 @@
1
- --require spec_helper
data/.travis.yml DELETED
@@ -1,13 +0,0 @@
1
- dist: trusty
2
- sudo: false
3
- language: ruby
4
- cache: bundler
5
- rvm:
6
- - 2.5
7
- - 2.6
8
- before_install:
9
- # Travis' Ruby 2.5.0 ships broken rubygems, won't run rake.
10
- # Workaround: update rubygems. See travis-ci issue 8978
11
- - gem install bundler
12
- notifications:
13
- email: false
data/Gemfile DELETED
@@ -1,8 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
-
5
- # Specify your gem's dependencies in safe_values.gemspec
6
- gemspec
7
-
8
- gem 'rspec_junit_formatter'
data/Rakefile DELETED
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "bundler/gem_tasks"
4
- require 'rspec/core/rake_task'
5
- RSpec::Core::RakeTask.new(:spec)
6
-
7
- task :default => :spec
data/bin/console DELETED
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "safe_values"
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
- require "irb"
14
- IRB.start(__FILE__)
data/bin/setup DELETED
@@ -1,8 +0,0 @@
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/safe_values.gemspec DELETED
@@ -1,26 +0,0 @@
1
-
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'safe_values/version'
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = 'safe_values'
8
- spec.version = SafeValues::VERSION
9
- spec.authors = ['DMM Eikaiwa']
10
- spec.email = ['dev@iknow.jp']
11
-
12
- spec.summary = %q{Struct classes with safer constructors.}
13
- spec.homepage = 'http://github.com/iknow/safe_values'
14
- spec.license = 'MIT'
15
-
16
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
- f.match(%r{^(test|spec|features)/})
18
- end
19
- spec.bindir = 'exe'
20
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
- spec.require_paths = ['lib']
22
-
23
- spec.add_development_dependency 'bundler'
24
- spec.add_development_dependency 'rake', '~> 10.0'
25
- spec.add_development_dependency 'rspec'
26
- end