diff --git a/Gemfile b/Gemfile index 0bdcb12..ec9cdf9 100644 --- a/Gemfile +++ b/Gemfile @@ -4,4 +4,4 @@ group :test do gem 'rspec_junit_formatter', '0.2.2' end # 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 e3d1de8..b0b0ea7 100644 --- a/lib/static_models.rb +++ b/lib/static_models.rb @@ -42,12 +42,24 @@ def valid? def self.where(*args) all end + + def ==(other) + other.try(:id) == id + end + + def hash + "static_model_#{self.class.object_id}_#{id}".freeze.hash + end + + def eql?(other) + other == self + end end class_methods do NumberType = if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.4.0') Integer - else + else Fixnum end @@ -103,11 +115,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 @@ -144,7 +156,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() @@ -180,7 +192,7 @@ def belongs_to(association, opts = {}) instance_variable_set("@invalid_#{association}_code", value.try(:to_sym)) end - end + end define_method("#{association}_code") do send(association).try(:code) || @@ -191,4 +203,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 21f5339..b8ff1ad 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,23 @@ 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 + 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 + expect { Breed.find(56) }.to raise_error(StaticModels::NotFoundError) + end + it "has a model name" do Breed.model_name.plural.should == "breeds" end @@ -115,13 +131,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] @@ -238,7 +254,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 be_nil dog.anything = Breed.doberman dog.anything = nil dog.anything_id.should be_nil