mor 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c5cb357bae3b07adc47856b8ed0d8ab4047f34aa
4
- data.tar.gz: 2b014d70240842e4f18a337b559bf0b432375fd1
3
+ metadata.gz: b1afbeb537134b7328ed4769fda161403f81adf4
4
+ data.tar.gz: 622c07371fd2d4682019fa3deba77d48fddb844c
5
5
  SHA512:
6
- metadata.gz: 5058bd04d296254ce9ad18037ba22ce5249b44c4d6202bd38936418f8049a0e4d7d4f42209a6842ee3e31d2470d65b6de99e4da67332ba8ad544f8195a35771e
7
- data.tar.gz: ffa866b04161739abae23f887a144aec2daa6ff6838a954af91d2d0f47be151c4d504b77a7905808a49afaef7afe400f29dedf377adc30ef754a521a57189a8e
6
+ metadata.gz: fdef1f7bcb95126581c473fe6e98ffa04d9b1888498efecb6d7b3922d31ebed1e28a2b15586b39e4db26f2ff48c0ccb79d66cff53fdead486669f14fe9a5aada
7
+ data.tar.gz: 2d3e945cc5529cddcc99cc44d3c094c03b909e52c628050d7b26dac2791dec82cba7f0eaf892b161d18757ba76b46d3ed4ed771632b9888f0b14a33fb5239c87
@@ -1,6 +1,7 @@
1
1
  require 'active_model'
2
2
  require 'active_support/concern'
3
3
  require 'active_support/hash_with_indifferent_access'
4
+ require 'active_support/core_ext/hash'
4
5
 
5
6
  module Mor
6
7
  module Model
@@ -10,7 +11,10 @@ module Mor
10
11
  def attr_accessor *attrs
11
12
  super(*attrs)
12
13
  attrs.delete_if{ |a| a == :attributes }.each { |attr|
13
- define_method(:"#{attr}="){|val| self.attributes[attr]=val}
14
+ define_method(:"#{attr}="){ |val|
15
+ self.attributes[attr]=val
16
+ self.attributes[:id]=val if attr == self.class.primary_key && attr != :id
17
+ }
14
18
  define_method(attr){ self.attributes[attr] }
15
19
  }
16
20
  end
@@ -43,11 +47,16 @@ module Mor
43
47
  end
44
48
  end
45
49
 
46
- def find_by hash
50
+ def where hash
47
51
  return nil unless hash
48
52
  self.all.select{ |app|
49
53
  app.attributes.select{ |k,v| hash.keys.include?(k.to_sym) }.values.sort == hash.values.sort
50
- }.first
54
+ }
55
+ end
56
+
57
+ def find_by hash
58
+ return nil unless hash
59
+ where(hash).first
51
60
  end
52
61
 
53
62
  def all
@@ -75,8 +84,8 @@ module Mor
75
84
 
76
85
  define_model_callbacks :save, :create, :update, :destroy
77
86
 
78
- validates_presence_of :id
79
- validate :validate_uniqueness_of_id, unless: "persisted?"
87
+ validates_presence_of self.primary_key
88
+ validate :validate_uniqueness_of_primary_key, unless: "persisted?"
80
89
  after_create :add_to_index
81
90
  after_destroy :remove_from_index
82
91
 
@@ -127,8 +136,8 @@ module Mor
127
136
 
128
137
  private
129
138
 
130
- def validate_uniqueness_of_id
131
- errors.add(:id, :taken) if self.class.index.include?(self.id)
139
+ def validate_uniqueness_of_primary_key
140
+ errors.add(self.class.primary_key, :taken) if self.class.index.include?(self.id)
132
141
  end
133
142
 
134
143
  def save_or_update
@@ -1,3 +1,3 @@
1
1
  module Mor
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -12,6 +12,19 @@ class TestModel
12
12
  end
13
13
  end
14
14
 
15
+ class TestModelDiffPK
16
+ include Mor::Model
17
+ def self.primary_key
18
+ :slug
19
+ end
20
+ attr_accessor :slug, :title, :body, :slug
21
+ before_validation :set_slug
22
+ private
23
+ def set_slug
24
+ self.slug = title.downcase
25
+ end
26
+ end
27
+
15
28
  describe Mor::Model do
16
29
 
17
30
  it "acts as active_model/model" do
@@ -52,4 +65,22 @@ describe Mor::Model do
52
65
  TestModel.find("test-id").must_be_nil
53
66
  end
54
67
 
68
+ it "can has different primary key than id" do
69
+ instance = TestModelDiffPK.create(title: "Tett Name")
70
+ instance.persisted?.must_equal true
71
+ instance.id.must_equal instance.slug
72
+ instance.attributes[:id].must_equal instance.attributes[:slug]
73
+ end
74
+
75
+ it "finds single record by attributes" do
76
+ instance = TestModel.create(id: "test-id", title: "title", body: "test body")
77
+ TestModel.find_by(title: "title").attributes.must_equal instance.attributes
78
+ end
79
+
80
+ it "finds many records by attributes" do
81
+ instance = TestModel.create(id: "test-id", title: "title", body: "test body")
82
+ instance2 = TestModel.create(id: "test-id-2", title: "title", body: "test body")
83
+ TestModel.where(title: "title").size.must_equal 2
84
+ end
85
+
55
86
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Onur Uyar