From b8b21f59003f49b7db209a34fec2f6aea29b5d41 Mon Sep 17 00:00:00 2001 From: Timur Ramazanov Date: Sun, 18 Dec 2022 19:08:48 +0300 Subject: [PATCH] chore: remove `ActiveModel` dependency --- Gemfile.lock | 3 --- lib/xdr.rb | 1 - lib/xdr/dsl/struct.rb | 2 -- lib/xdr/dsl/union.rb | 1 - lib/xdr/struct.rb | 13 ++++--------- lib/xdr/struct_validator.rb | 6 ------ lib/xdr/union.rb | 23 +++++++++++++---------- lib/xdr/union_validator.rb | 7 ------- spec/lib/xdr/dsl/union_spec.rb | 2 +- spec/spec_helper.rb | 2 ++ xdr.gemspec | 1 - 11 files changed, 20 insertions(+), 41 deletions(-) delete mode 100644 lib/xdr/struct_validator.rb delete mode 100644 lib/xdr/union_validator.rb diff --git a/Gemfile.lock b/Gemfile.lock index fbc3767..0c51e6e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,14 +2,11 @@ PATH remote: . specs: xdr (3.0.3) - activemodel (>= 4.2, < 8.0) activesupport (>= 4.2, < 8.0) GEM remote: https://rubygems.org/ specs: - activemodel (7.0.2.2) - activesupport (= 7.0.2.2) activesupport (7.0.2.2) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (>= 1.6, < 2) diff --git a/lib/xdr.rb b/lib/xdr.rb index 5c9bdac..98bdafe 100644 --- a/lib/xdr.rb +++ b/lib/xdr.rb @@ -1,5 +1,4 @@ require "xdr/version" -require "active_model" require "active_support/concern" require "active_support/dependencies/autoload" require "active_support/core_ext/object/blank" diff --git a/lib/xdr/dsl/struct.rb b/lib/xdr/dsl/struct.rb index 689f83d..2620243 100644 --- a/lib/xdr/dsl/struct.rb +++ b/lib/xdr/dsl/struct.rb @@ -13,7 +13,5 @@ def attribute(name, type) define_method "#{name}=" do |v| write_attribute(name, v) end - - define_attribute_methods name end end diff --git a/lib/xdr/dsl/union.rb b/lib/xdr/dsl/union.rb index 6d3a66d..1cfd7d0 100644 --- a/lib/xdr/dsl/union.rb +++ b/lib/xdr/dsl/union.rb @@ -18,7 +18,6 @@ def attribute(name, type) raise ArgumentError, "#{type} does not convert to xdr" unless type.is_a?(XDR::Concerns::ConvertsToXDR) self.arms = arms.merge(name => type) - define_attribute_methods name end private diff --git a/lib/xdr/struct.rb b/lib/xdr/struct.rb index 0bceace..0abcc2e 100644 --- a/lib/xdr/struct.rb +++ b/lib/xdr/struct.rb @@ -1,20 +1,12 @@ require "base64" class XDR::Struct - include ActiveModel::Model - include ActiveModel::AttributeMethods - extend XDR::Concerns::ConvertsToXDR extend XDR::DSL::Struct - attribute_method_prefix "read_" - attribute_method_suffix "write_" - class_attribute :fields self.fields = ActiveSupport::OrderedHash.new - validates_with XDR::StructValidator - attr_reader :attributes def self.read(io) @@ -38,7 +30,10 @@ def self.valid?(val) def initialize(attributes = {}) @attributes = {} - super + + attributes.each do |name, value| + write_attribute(name, value) + end end # diff --git a/lib/xdr/struct_validator.rb b/lib/xdr/struct_validator.rb deleted file mode 100644 index 0b8ea4d..0000000 --- a/lib/xdr/struct_validator.rb +++ /dev/null @@ -1,6 +0,0 @@ -class XDR::StructValidator < ActiveModel::Validator - def validate(struct) - # validate each field - # TODO - end -end diff --git a/lib/xdr/union.rb b/lib/xdr/union.rb index 18b9f8a..d39222e 100644 --- a/lib/xdr/union.rb +++ b/lib/xdr/union.rb @@ -1,7 +1,4 @@ class XDR::Union - include ActiveModel::Model - include ActiveModel::AttributeMethods - extend XDR::Concerns::ConvertsToXDR extend XDR::DSL::Union @@ -17,8 +14,6 @@ class XDR::Union self.switch_type = nil self.switch_name = nil - attribute_method_suffix "!" - def self.arm_for_switch(switch) begin switch = normalize_switch switch @@ -100,16 +95,24 @@ def value alias_method :get, :value - def attribute(attr) - return nil unless @arm.to_s == attr + def method_missing(method) + is_bang = method.end_with?("!") + attr = method.to_s.delete_suffix("!") + + super unless self.class.arms.key?(attr.to_sym) + + if @arm.to_s != attr + return nil unless is_bang + raise XDR::ArmNotSetError, "#{attr} is not the set arm" + end get end - def attribute!(attr) - raise XDR::ArmNotSetError, "#{attr} is not the set arm" unless @arm.to_s == attr + def respond_to_missing?(method, *) + attr = method.to_s.delete_suffix("!") - get + self.class.arms.key?(attr.to_sym) || super end # diff --git a/lib/xdr/union_validator.rb b/lib/xdr/union_validator.rb deleted file mode 100644 index ae52768..0000000 --- a/lib/xdr/union_validator.rb +++ /dev/null @@ -1,7 +0,0 @@ -class XDR::UnionValidator < ActiveModel::Validator - def validate(union) - # validate a discriminant is set - # validate the arm is compatible with the set discriminant - # TODO - end -end diff --git a/spec/lib/xdr/dsl/union_spec.rb b/spec/lib/xdr/dsl/union_spec.rb index b4c3124..ef767f8 100644 --- a/spec/lib/xdr/dsl/union_spec.rb +++ b/spec/lib/xdr/dsl/union_spec.rb @@ -51,7 +51,7 @@ expect { klass.new(-1) }.to raise_error(XDR::InvalidSwitchError) end - it "allows bool types", :focus do + it "allows bool types" do klass = nil expect do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ad1bdb3..f69e4e6 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -5,4 +5,6 @@ Dir["#{__dir__}/support/**/*.rb"].sort.each { |f| require f } RSpec.configure do |config| + config.filter_run focus: true + config.run_all_when_everything_filtered = true end diff --git a/xdr.gemspec b/xdr.gemspec index f0a0fdc..33280f6 100644 --- a/xdr.gemspec +++ b/xdr.gemspec @@ -24,5 +24,4 @@ Gem::Specification.new do |spec| spec.required_ruby_version = ">= 2.4.0" spec.add_dependency "activesupport", ">= 4.2", "< 8.0" - spec.add_dependency "activemodel", ">= 4.2", "< 8.0" end