Skip to content

Commit

Permalink
Merge pull request #62 from Vagab/cooler-with
Browse files Browse the repository at this point in the history
  • Loading branch information
Vagab committed Nov 30, 2022
2 parents ddf6ca5 + b624e0c commit 18b5f4d
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 127 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ source "https://rubygems.org"

gem "pry", "~> 0.14"
gem "rspec", "~> 3.11"
gem "activesupport", "~> 7.0.4"
gem "rubocop-shopify"
gem "rubocop-rspec"
gem "rubocop"
Expand Down
12 changes: 12 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
GEM
remote: https://rubygems.org/
specs:
activesupport (7.0.4)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
ast (2.4.2)
coderay (1.1.3)
concurrent-ruby (1.1.10)
diff-lcs (1.5.0)
i18n (1.12.0)
concurrent-ruby (~> 1.0)
json (2.6.2)
method_source (1.0.0)
minitest (5.16.3)
parallel (1.22.1)
parser (3.1.2.1)
ast (~> 2.4.1)
Expand Down Expand Up @@ -51,13 +60,16 @@ GEM
ruby2_keywords (0.0.5)
saharspec (0.0.9)
ruby2_keywords
tzinfo (2.0.5)
concurrent-ruby (~> 1.0)
unicode-display_width (2.3.0)

PLATFORMS
arm64-darwin-21
x86_64-linux

DEPENDENCIES
activesupport (~> 7.0.4)
pry (~> 0.14)
rspec (~> 3.11)
rspec-its
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.4.7
0.5.0
2 changes: 0 additions & 2 deletions lib/core_ext.rb

This file was deleted.

53 changes: 0 additions & 53 deletions lib/core_ext/deep_dup.rb

This file was deleted.

42 changes: 0 additions & 42 deletions lib/core_ext/duplicable.rb

This file was deleted.

4 changes: 3 additions & 1 deletion lib/statinize.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
require "set"
require "pry"
require "active_support"
require "active_support/core_ext/object/deep_dup"
require "active_support/core_ext/object/with_options"
require "bigdecimal/util"
require "core_ext"
require "statinize/statinizable"
require "statinize/dsl"
require "statinize/statinizer"
Expand Down
22 changes: 2 additions & 20 deletions lib/statinize/dsl.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Defines DSL for statinize block
module Statinize
module DSL
alias_method :with, :with_options

def attribute(*attrs, **options)
attrs.each do |attr|
Attribute.create(klass, attr, options) unless attribute? attr
Expand All @@ -15,26 +17,6 @@ def validate(*attrs, **options)
end
end

def with(**options, &block)
# create new statinizer instance
instance = self.class.new(klass)
instance.force(force)

# execute block in the context of that statinizer
# while it's attached to the klass
# then rewind and attach the original statinizer(self)
# back to the klass
klass.instance_variable_set(:@statinizer, instance)
instance.instance_exec(&block)
klass.instance_variable_set(:@statinizer, self)

# merge the newly created statinizer with the options
instance.merge_options(**options)

# populate self with the instance's attributes
populate(instance.attributes)
end

def before(&block)
return unless block_given?

Expand Down
8 changes: 0 additions & 8 deletions lib/statinize/statinizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,6 @@ def check_validators_exist!
raise NoSuchValidatorError unless all_validators_defined?
end

def merge_options(**options)
attributes.each do |attribute|
attribute.options_collection.each do |option|
option.merge!(options)
end
end
end

def populate(attrs)
attrs.each do |attr|
attribute attr.arg_name, name: attr.name, default: attr.default, default_exec: attr.default_exec
Expand Down
40 changes: 40 additions & 0 deletions spec/statinize/with_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
RSpec.describe "With" do
subject { WithDummy.new(params) }

context "two with blocks" do
context "first validation is violated" do
let!(:params) { { a: "a", c: :a } }

its(:valid?) { should be_truthy }

context "both validations are calls to validate" do
let!(:params) { { a: :a, c: "c" } }

its(:valid?) { should be_falsey }
end
end
end
end

class WithDummy
include Statinize

statinize do
with type: Symbol do
attribute :a
end

with presence: true do
attribute :a
end

attribute :c
with type: Symbol do
validate :c
end

with presence: true do
validate :c
end
end
end
2 changes: 2 additions & 0 deletions statinize.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ Gem::Specification.new do |s|
s.files = Dir["lib/**/*.rb"]
s.license = "MIT"
s.platform = Gem::Platform::RUBY

s.add_runtime_dependency "activesupport", "~> 7.0.0"
end

0 comments on commit 18b5f4d

Please sign in to comment.