From db2eeb1cfa714e25ddea9d9dfc839dc071acaf05 Mon Sep 17 00:00:00 2001 From: Nicolas Orchow Date: Wed, 5 Dec 2018 16:42:00 -0300 Subject: [PATCH 1/9] Add code methods generation --- lib/static_models.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/static_models.rb b/lib/static_models.rb index e2b2d8c..78ccf11 100644 --- a/lib/static_models.rb +++ b/lib/static_models.rb @@ -156,6 +156,22 @@ def belongs_to(association, opts = {}) super(value) end end + + #Adding accesor for code_representation + define_method("#{association}_code=") do |value| + if thing = expected_class.find_by_code(value) + instance_variable_set("@invalid_#{association}_code", nil) + send("#{association}=", thing) + else + instance_variable_set("@invalid_#{association}_code", + value.try(:to_sym)) + end + end + + define_method("#{association}_code") do + send(association).try(:code) || + instance_variable_get("@invalid_#{association}_code") + end end end end From 5ec98746411be45a0284520e7586e695f148c9df Mon Sep 17 00:00:00 2001 From: Nicolas Orchow Date: Wed, 5 Dec 2018 16:50:02 -0300 Subject: [PATCH 2/9] Add missing find_by_code method --- lib/static_models.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/static_models.rb b/lib/static_models.rb index 78ccf11..caa320e 100644 --- a/lib/static_models.rb +++ b/lib/static_models.rb @@ -100,6 +100,10 @@ def find(id) values[id.to_i] end + def find_by_code(code) + all.select{|x| x.code == code.try(:to_sym)}.first + end + def all values.values end From ae0e505dfdb930239150fa1a372362dc37a2ae89 Mon Sep 17 00:00:00 2001 From: werner Date: Wed, 21 Aug 2019 16:08:34 -0300 Subject: [PATCH 3/9] Add NotFoundError that raise when a model is not found --- lib/static_models.rb | 5 +++-- spec/spec_helper.rb | 1 - spec/static_models_spec.rb | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/static_models.rb b/lib/static_models.rb index caa320e..3b1127d 100644 --- a/lib/static_models.rb +++ b/lib/static_models.rb @@ -97,11 +97,11 @@ def static_models_hashes(columns, hashes) end def find(id) - values[id.to_i] + values[id.to_i] || (raise NotFoundError.new("id #{id} not found")) end def find_by_code(code) - all.select{|x| x.code == code.try(:to_sym)}.first + all.select{|x| x.code == code.try(:to_sym)}.first end def all @@ -181,4 +181,5 @@ def belongs_to(association, opts = {}) end class ValueError < StandardError; end + class NotFoundError < StandardError; end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 76ef410..05bb44e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -5,7 +5,6 @@ require 'byebug' require "static_models" - # Require our macros and extensions Dir[File.expand_path('../../spec/support/macros/**/*.rb', __FILE__)].map(&method(:require)) diff --git a/spec/static_models_spec.rb b/spec/static_models_spec.rb index bc15ec5..bcfb4a6 100644 --- a/spec/static_models_spec.rb +++ b/spec/static_models_spec.rb @@ -200,7 +200,7 @@ class LocalDoggie dog.breed = Breed.corgi dog.breed = nil dog.breed_id.should be_nil - dog.breed.should be_nil + expect { dog.breed }.to raise_error(StaticModels::NotFoundError) dog.anything = Breed.doberman dog.anything = nil dog.anything_id.should be_nil From bfeeb01a427e68d2ecd3d04373f9bbbfd34032bc Mon Sep 17 00:00:00 2001 From: werner Date: Fri, 23 Aug 2019 11:58:34 -0300 Subject: [PATCH 4/9] Allows nil for belongs_to --- lib/static_models.rb | 2 +- spec/static_models_spec.rb | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/static_models.rb b/lib/static_models.rb index 3b1127d..30954a0 100644 --- a/lib/static_models.rb +++ b/lib/static_models.rb @@ -134,7 +134,7 @@ def belongs_to(association, opts = {}) define_method("#{association}") do klass = expected_class || send("#{association}_type").to_s.safe_constantize - if klass && klass.include?(Model) + if klass && klass.include?(Model) && send("#{association}_id").present? klass.find(send("#{association}_id")) elsif defined?(super) super() diff --git a/spec/static_models_spec.rb b/spec/static_models_spec.rb index bcfb4a6..1b4a54b 100644 --- a/spec/static_models_spec.rb +++ b/spec/static_models_spec.rb @@ -50,6 +50,10 @@ class Dog } end + it "throws an error when model not found" do + expect { Breed.find(56) }.to raise_error(StaticModels::NotFoundError) + end + it "has a model name" do Breed.model_name.plural.should == "breeds" end @@ -200,7 +204,7 @@ class LocalDoggie dog.breed = Breed.corgi dog.breed = nil dog.breed_id.should be_nil - expect { dog.breed }.to raise_error(StaticModels::NotFoundError) + expect(dog.breed).to be_nil dog.anything = Breed.doberman dog.anything = nil dog.anything_id.should be_nil From 99a51438ceb80cb97d16557552a4eabb613fb169 Mon Sep 17 00:00:00 2001 From: nubis Date: Tue, 24 Sep 2019 17:52:35 -0300 Subject: [PATCH 5/9] Make static models comparable by id --- lib/static_models.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/static_models.rb b/lib/static_models.rb index 30954a0..d6c50aa 100644 --- a/lib/static_models.rb +++ b/lib/static_models.rb @@ -42,6 +42,10 @@ def valid? def self.where(*args) all end + + def ==(other) + other.id == id + end end class_methods do From d601e01f5452af1d52735fc08b3821461a27d596 Mon Sep 17 00:00:00 2001 From: nubis Date: Tue, 24 Sep 2019 17:59:56 -0300 Subject: [PATCH 6/9] implement eql for static models --- lib/static_models.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/static_models.rb b/lib/static_models.rb index d6c50aa..f0fe437 100644 --- a/lib/static_models.rb +++ b/lib/static_models.rb @@ -46,6 +46,10 @@ def self.where(*args) def ==(other) other.id == id end + + def eql?(other) + other == self + end end class_methods do From f5ab8f507387197add9ae3bb841398b9ad5837e0 Mon Sep 17 00:00:00 2001 From: nubis Date: Tue, 24 Sep 2019 18:20:24 -0300 Subject: [PATCH 7/9] implements hash for static models --- lib/static_models.rb | 4 ++++ spec/static_models_spec.rb | 11 ++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/static_models.rb b/lib/static_models.rb index f0fe437..c369c49 100644 --- a/lib/static_models.rb +++ b/lib/static_models.rb @@ -47,6 +47,10 @@ def ==(other) other.id == id end + def hash + "static_model_#{self.class.object_id}_#{id}".freeze.hash + end + def eql?(other) other == self end diff --git a/spec/static_models_spec.rb b/spec/static_models_spec.rb index 1b4a54b..cc5c4e8 100644 --- a/spec/static_models_spec.rb +++ b/spec/static_models_spec.rb @@ -20,7 +20,6 @@ class Dog end describe StaticModels::Model do - it "defines and uses a static model" do Breed.corgi.tap do |b| b.should == Breed.find(6) @@ -50,6 +49,16 @@ class Dog } end + describe "when comparing equality" do + it "is equal when same code is used" do + expect(Breed.collie).to eq Breed.new(id: 1, code: :collie, height: nil) + end + it "can compare inside sets" do + expect( Set.new([Breed.collie])) + .to eq Set.new([Breed.new(id: 1, code: :collie, height: nil)]) + end + end + it "throws an error when model not found" do expect { Breed.find(56) }.to raise_error(StaticModels::NotFoundError) end From 40783fe05e5f19629b92ae04b08b0f47d9017b20 Mon Sep 17 00:00:00 2001 From: werner Date: Thu, 3 Oct 2019 12:22:52 -0300 Subject: [PATCH 8/9] Fix error undefined method `id' for false:FalseClass --- Gemfile | 2 +- lib/static_models.rb | 2 +- spec/static_models_spec.rb | 3 +++ static_models.gemspec | 2 +- 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index 6aece4e..3f2e0eb 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,4 @@ source 'https://rubygems.org' # Specify your gem's dependencies in static_models.gemspec -gemspec +gemspec \ No newline at end of file diff --git a/lib/static_models.rb b/lib/static_models.rb index c369c49..634a9f3 100644 --- a/lib/static_models.rb +++ b/lib/static_models.rb @@ -44,7 +44,7 @@ def self.where(*args) end def ==(other) - other.id == id + other.try(:id) == id end def hash diff --git a/spec/static_models_spec.rb b/spec/static_models_spec.rb index cc5c4e8..e1bd507 100644 --- a/spec/static_models_spec.rb +++ b/spec/static_models_spec.rb @@ -57,6 +57,9 @@ class Dog expect( Set.new([Breed.collie])) .to eq Set.new([Breed.new(id: 1, code: :collie, height: nil)]) end + it 'does not fail when compare with anything not a static model' do + expect(Breed.collie).not_to eq(true) + end end it "throws an error when model not found" do diff --git a/static_models.gemspec b/static_models.gemspec index 3ff6c92..c3eb334 100644 --- a/static_models.gemspec +++ b/static_models.gemspec @@ -30,6 +30,6 @@ Gem::Specification.new do |spec| spec.add_development_dependency "rake", "~> 10.0" spec.add_development_dependency "rspec", "~> 3.0" spec.add_development_dependency "activerecord", '~> 4.2', '>= 4.2.0' - spec.add_development_dependency "sqlite3", "~> 1.0", ">= 1.0.0" + spec.add_development_dependency "sqlite3", "~> 1.3", "< 1.4" spec.add_development_dependency "byebug", "~> 6.0", ">= 6.0.0" end From 7223d4d6993cca8a541578c74aef7c2a9d2f9619 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Rojas?= Date: Sun, 29 Mar 2020 06:28:13 -0300 Subject: [PATCH 9/9] Use Integer for forward and backward compatibility --- lib/static_models.rb | 4 ++-- spec/static_models_spec.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/static_models.rb b/lib/static_models.rb index 634a9f3..1834131 100644 --- a/lib/static_models.rb +++ b/lib/static_models.rb @@ -68,7 +68,7 @@ def static_models_dense(table) def static_models_sparse(table) table.each do |row| - expected = row.size == 2 ? [Fixnum, Symbol] : [Fixnum, Symbol, Hash] + expected = row.size == 2 ? [Integer, Symbol] : [Integer, Symbol, Hash] if row.collect(&:class) != expected raise ValueError.new("Invalid row #{row}, expected #{expected}") @@ -87,7 +87,7 @@ def static_models_hashes(columns, hashes) raise ValueError.new("Table column names must all be Symbols") end - unless hashes.all?{|h| h[:id].is_a?(Fixnum)} + unless hashes.all?{|h| h[:id].is_a?(Integer)} raise ValueError.new("Ids must be integers") end diff --git a/spec/static_models_spec.rb b/spec/static_models_spec.rb index e1bd507..aa2f29d 100644 --- a/spec/static_models_spec.rb +++ b/spec/static_models_spec.rb @@ -124,13 +124,13 @@ def self.checks_sparse(title, *table) check_def("checks sparse "+ title) do static_models_sparse(table) end end - checks_sparse "id is Fixnum", ["hello", :foo] + checks_sparse "id is Integer", ["hello", :foo] checks_sparse "code is Symbol", [1, 2323] checks_sparse "has only id, code, and Hash", [1, :bar, :ble, foo: :bar] checks_sparse "codes are unique", [1, :foo], [2, :foo] checks_sparse "columns are symbols", [1, :foo, [] => :baz] - checks_dense "id is Fixnum", [:id, :code], ["hello", :foo] + checks_dense "id is Integer", [:id, :code], ["hello", :foo] checks_dense "invalid code type", [:id, :code], [1, 2323] checks_dense "has id and code", [:id, :code, :other], [1,] checks_dense "codes are unique", [:id, :code], [1, :foo], [2, :foo]