Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 18 additions & 5 deletions lib/static_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,24 @@ def valid?
def self.where(*args)
all
end

def ==(other)
other.try(:id) == id
Copy link
Contributor

@tmsrjs tmsrjs Jun 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we check for class equality too?

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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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) ||
Expand All @@ -191,4 +203,5 @@ def belongs_to(association, opts = {})
end

class ValueError < StandardError; end
class NotFoundError < StandardError; end
end
1 change: 0 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
24 changes: 20 additions & 4 deletions spec/static_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down