diff --git a/lib/entity_utils.rb b/lib/entity_utils.rb index 650a859..694ac4d 100644 --- a/lib/entity_utils.rb +++ b/lib/entity_utils.rb @@ -400,19 +400,12 @@ def transform_and_validate(fields, input, opts) # ) # # See rspec tests for more examples and output - def define_builder(*args) - specs, opts = extract_options(args) - EntityBuilder.new(parse_specs(specs), merge_configs(opts)) + def define_builder(*specs) + EntityBuilder.new(parse_specs(specs), merge_configs({})) end - def extract_options(args) - last = args.last - - if last.is_a?(Hash) - [args.first(args.size - 1), last] - else - [args, nil] - end + def define_builder_validate_always(*specs) + EntityBuilder.new(parse_specs(specs), merge_configs(validate: true)) end def reset_configurations! diff --git a/spec/entity_utils_spec.rb b/spec/entity_utils_spec.rb index d1640b1..30668c5 100644 --- a/spec/entity_utils_spec.rb +++ b/spec/entity_utils_spec.rb @@ -58,45 +58,33 @@ .to raise_error(ArgumentError) end - describe "validate option" do + describe "running validations" do - it "takes validate option as a builder parameter" do - person_entity = EntityUtils.define_builder( - [:sex, :to_symbol, one_of: [:m, :f]], - validate: false - ) - - expect{ person_entity.call(sex: "male") } - .not_to raise_error - - expect(person_entity.call(sex: "male")).to eq(sex: :male) + before(:each) { EntityUtils.reset_configurations! } + after(:each) { EntityUtils.reset_configurations! } - validate_person_entity = EntityUtils.define_builder( - [:sex, :to_symbol, one_of: [:m, :f]], - validate: true - ) - - expect{ validate_person_entity.call(sex: "male") } - .to raise_error(ArgumentError) + describe "#define_builder_validate_always" do - expect{ validate_person_entity.call(sex: :m) } - .not_to raise_error - end - - describe "global configuration" do - before(:each) { EntityUtils.reset_configurations! } - after(:each) { EntityUtils.reset_configurations! } + it "always runs validations" do + EntityUtils.configure!( + validate: false + ) - it "validate defaults to true" do - person_entity = EntityUtils.define_builder( + person_entity = EntityUtils.define_builder_validate_always( [:sex, :to_symbol, one_of: [:m, :f]], ) expect{ person_entity.call(sex: "male") } .to raise_error(ArgumentError) + + expect{ person_entity.call(sex: :m) } + .not_to raise_error end + end + + describe "#define_builder" do - it "uses global configurations" do + it "skips validations if global `validate` configuration is false" do EntityUtils.configure!( validate: false ) @@ -107,24 +95,24 @@ expect{ person_entity.call(sex: "male") } .not_to raise_error + + # Runs transformers + expect(person_entity.call(sex: "male")).to eq(sex: :male) end - it "local configurations override global configurations" do + it "runs validations if global `validate` configuration is true" do EntityUtils.configure!( - validate: false + validate: true ) person_entity = EntityUtils.define_builder( [:sex, :to_symbol, one_of: [:m, :f]], - validate: true ) expect{ person_entity.call(sex: "male") } .to raise_error(ArgumentError) end - end - end it "#define_builder supports nested entities" do