-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cast boolean values #45
base: master
Are you sure you want to change the base?
Changes from all commits
fda173b
13f9d8e
feb75b6
f0c06be
65f593a
dcd2152
9a634d1
9022932
b583c00
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
--color | ||
--require spec_helper |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
## [2.2.0] - 2024-11-22 | ||
|
||
- Predicate methods now cast the value to a boolean | ||
```ruby | ||
Global.foo.enabled # => "0" | ||
Global.foo.enabled? # => false | ||
``` | ||
- Dropped Ruby 2.7 support |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rubygems' | ||
require 'bundler' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did not find why these were needed. |
||
|
||
Bundler.require | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://bundler.io/guides/bundler_setup.html
|
||
|
||
require 'rspec/core/rake_task' | ||
require 'bundler/gem_tasks' | ||
require 'rspec/core/rake_task' | ||
require 'rubocop/rake_task' | ||
|
||
RSpec::Core::RakeTask.new(:spec) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
$LOAD_PATH.push File.expand_path('lib', __dir__) | ||
require 'global/version' | ||
require_relative 'lib/global/version' | ||
|
||
Gem::Specification.new do |s| | ||
s.name = 'global' | ||
s.version = Global::VERSION | ||
s.required_ruby_version = '>= 3.0.0' | ||
s.authors = ['Railsware LLC'] | ||
s.email = 'contact@railsware.com' | ||
|
||
s.description = 'Simple way to load your configs from yaml/aws/gcp' | ||
|
||
s.homepage = 'https://github.com/railsware/global' | ||
s.licenses = ['MIT'] | ||
s.summary = 'Simple way to load your configs from yaml/aws/gcp' | ||
|
||
s.metadata['rubygems_mfa_required'] = 'true' | ||
|
||
s.files = `git ls-files`.split("\n") | ||
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") | ||
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) } | ||
s.require_paths = ['lib'] | ||
|
||
s.homepage = 'https://github.com/railsware/global' | ||
s.licenses = ['MIT'] | ||
|
||
s.add_development_dependency 'aws-sdk-ssm', '~> 1' | ||
s.add_development_dependency 'google-cloud-secret_manager', '~> 0' | ||
s.add_development_dependency 'rake', '~> 12.3.1' | ||
s.add_development_dependency 'rspec', '>= 3.0' | ||
s.add_development_dependency 'rubocop', '~> 0.81.0' | ||
s.add_development_dependency 'simplecov', '~> 0.16.1' | ||
|
||
s.add_runtime_dependency 'activesupport', '>= 2.0' | ||
s.add_dependency 'activesupport', '>= 2.0' | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,7 +95,7 @@ def load_parameters_from_ssm(next_token = nil) | |
def build_configuration_from_parameters(parameters) | ||
configuration = {} | ||
parameters.each do |parameter| | ||
parameter_parts = parameter.name[@prefix.length..-1].split(PATH_SEPARATOR).map(&:to_sym) | ||
parameter_parts = parameter.name[@prefix.length..].split(PATH_SEPARATOR).map(&:to_sym) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rubocop autofix. applies to other as well. |
||
param_container = parameter_parts[0..-2].reduce(configuration) do |container, part| | ||
container[part] ||= {} | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,21 @@ class Configuration | |
:member?, :[], :[]=, :to_hash, :to_json, | ||
:inspect, :fetch | ||
|
||
# rubocop:disable Lint/BooleanSymbol | ||
# @see ActiveModel::Type::Boolean::FALSE_VALUES | ||
FALSE_VALUES = [ | ||
false, 0, | ||
'0', :'0', | ||
'f', :f, | ||
'F', :F, | ||
'false', :false, | ||
'FALSE', :FALSE, | ||
'off', :off, | ||
'OFF', :OFF | ||
].to_set.freeze | ||
private_constant :FALSE_VALUES | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
# rubocop:enable Lint/BooleanSymbol | ||
|
||
def initialize(hash) | ||
@hash = hash.respond_to?(:with_indifferent_access) ? hash.with_indifferent_access : hash | ||
end | ||
|
@@ -49,9 +64,10 @@ def respond_to_missing?(method_name, include_private = false) | |
end | ||
|
||
def method_missing(method, *args, &block) | ||
method = normalize_key_by_method(method) | ||
if key?(method) | ||
get_configuration_value(method) | ||
normalized_method = normalize_key_by_method(method) | ||
if key?(normalized_method) | ||
value = get_configuration_value(normalized_method) | ||
boolean_method?(method) ? cast_boolean(value) : value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is what this PR is all about. |
||
else | ||
super | ||
end | ||
|
@@ -61,6 +77,11 @@ def boolean_method?(method) | |
'?' == method.to_s[-1] | ||
end | ||
|
||
# @see ActiveModel::Type::Boolean#cast_value | ||
def cast_boolean(value) | ||
!FALSE_VALUES.include?(value) | ||
end | ||
|
||
def normalize_key_by_method(method) | ||
boolean_method?(method) ? method.to_s[0..-2].to_sym : method | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,6 @@ | |
|
||
module Global | ||
|
||
VERSION = '2.1.0' | ||
VERSION = '2.2.0' | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
require 'aws-sdk-ssm' | ||
require 'global/backend/aws_parameter_store' | ||
|
||
RSpec.describe Global::Backend::AwsParameterStore do | ||
subject(:parameter_store) do | ||
described_class.new(prefix: '/testapp/', client: client) | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed rubocop abuses. applies to other spec files. |
||
|
||
let(:client) do | ||
Aws::SSM::Client.new(stub_responses: true) | ||
end | ||
subject do | ||
described_class.new(prefix: '/testapp/', client: client) | ||
end | ||
|
||
it 'reads parameters from the parameter store' do | ||
it 'reads parameters from the parameter store' do # rubocop:disable RSpec/ExampleLength, RSpec/MultipleExpectations | ||
client.stub_responses( | ||
:get_parameters_by_path, | ||
[ | ||
|
@@ -37,7 +37,7 @@ | |
} | ||
] | ||
) | ||
expect(subject.load).to eq( | ||
expect(parameter_store.load).to eq( | ||
foo: 'foo-value', | ||
bar: { | ||
baz: 'baz-value', | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
require 'google/cloud/secret_manager' | ||
require 'global/backend/gcp_secret_manager' | ||
|
||
RSpec.describe Global::Backend::GcpSecretManager do | ||
let(:client) { double } | ||
|
||
subject do | ||
subject(:secret_manager) do | ||
described_class.new(prefix: 'prod-myapp-', client: client, project_id: 'example') | ||
end | ||
|
||
before do | ||
@match_item = double | ||
allow(@match_item).to receive(:name).and_return('prod-myapp-example-test_key') | ||
let(:client) { double } | ||
|
||
@secret_data = double | ||
allow(@secret_data).to receive_message_chain(:payload, :data).and_return('secret value') | ||
before do | ||
# rubocop:disable RSpec/VerifiedDoubles | ||
match_item = double(name: 'prod-myapp-example-test_key') | ||
not_match_item = double(name: 'different_key') | ||
|
||
@not_match_item = double | ||
allow(@not_match_item).to receive(:name).and_return('different_key') | ||
secret_data = double(data: 'secret value') | ||
secret_version_response = double(payload: secret_data) | ||
|
||
@list = double | ||
allow(@list).to receive(:next_page_token).and_return('') | ||
allow(@list).to receive(:each).and_yield(@match_item).and_yield(@not_match_item) | ||
list = double | ||
allow(list).to receive(:next_page_token).and_return('') | ||
allow(list).to receive(:each).and_yield(match_item).and_yield(not_match_item) | ||
|
||
allow(client).to receive(:project_path).and_return('projects/example') | ||
allow(client).to receive(:secret_version_path) | ||
.with(project: 'example', secret: 'prod-myapp-example-test_key', secret_version: 'latest') | ||
.and_return('some_key_path') | ||
allow(client).to receive(:access_secret_version).with(name: 'some_key_path').and_return(@secret_data) | ||
allow(client).to receive(:list_secrets).and_return(@list) | ||
allow(client).to receive(:access_secret_version).with(name: 'some_key_path').and_return(secret_version_response) | ||
allow(client).to receive_messages(project_path: 'projects/example', list_secrets: list) | ||
# rubocop:enable RSpec/VerifiedDoubles | ||
end | ||
|
||
it 'reads parameters from the secret manager' do | ||
expect(subject.load).to eq({ example: { test_key: 'secret value' }}) | ||
expect(secret_manager.load).to eq({ example: { test_key: 'secret value' }}) | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.