riak-record 0.3.1 → 0.4.0

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: c8049ba441ebb2cba652263e32d6df1bd0a828a6
4
- data.tar.gz: 1108f270ce115612546ddef1b5311a68a91eb1bc
3
+ metadata.gz: c748667d6b7ac6f92c336fd09223cd25afc2ed44
4
+ data.tar.gz: 64f29aeab06a312e6950226069503015872cf7ae
5
5
  SHA512:
6
- metadata.gz: 2384d583d3f58389b0f85f0324b6303fd7513dced12cbb8d6000545639bd14d748b26d342de605ed2a5cfb2e1dba67201707088ce6ff8e393013e67e90cef871
7
- data.tar.gz: 7993a11ea520f4e214f6fa20a3bffe4ac552b6c0eb73d3e918daffb5bb0fecc563599fd6a59284789531407ecfca8971e514a6c0ed5888fcbb7432405a488008
6
+ metadata.gz: e3871dd3dce55ce6cdc00e7ed8d84cdb35794793be38bdb7b6668c51a6b71d1098ce174c604d85bf45dc30162e69e0b54b6f74b5426cdd45497ff2837740c036
7
+ data.tar.gz: 08ea68ccb42f6ae571104b9d052b35053914f7674d6c68aec8a7d56d2cdc5679592db0e76703eb6ba84e8be6dd1d25e70b985938e047857138dc88594e18344c
data/README.md CHANGED
@@ -35,6 +35,7 @@ post.data #> same as record.riak_object.data
35
35
 
36
36
  post.title = 'My First Post' #> record.riak_object.data['title']=
37
37
  post.title #> record.riak_object.data['title']
38
+ post.update_attributes(:title => 'My First Post (revised)')
38
39
 
39
40
  post.author_id = 99 #> record.riak_object.indexes["author_id_int"] = [99]
40
41
  post.category = 'ruby' #> record.riak_object.indexes["category_bin"] = ["ruby"]
@@ -47,9 +48,10 @@ post.reload #> reload the underlying riak_object from the db discarding changes
47
48
  Callbacks are called in this order:
48
49
  * before_save
49
50
  * before_create or before_update
51
+ * ...update_links...
50
52
  * ...save...
51
- * after_save
52
53
  * after_create or after_update
54
+ * after_save
53
55
 
54
56
 
55
57
  ### RiakRecord::Finder
@@ -80,7 +82,7 @@ end
80
82
  class Comment < RiakRecord::Base
81
83
  bucket_name :comments
82
84
  data_attribute :comment
83
- belongs_to :post, :class_name => 'Post', :foreign_key => "post_id"
85
+ belongs_to :post, :class_name => 'Post', :foreign_key => "post_id" # optional :link => true to create walkable link on save
84
86
 
85
87
  index_int_attributes :post_id
86
88
  end
data/TODO.md CHANGED
@@ -1,7 +1,6 @@
1
1
  # TODO
2
2
 
3
3
  * RiakRecord::Base
4
- * links_to should create a walkable link
5
4
  * id_generator
6
5
  * Document methods in classes
7
6
  * Validations support
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.4.0
@@ -1,10 +1,38 @@
1
1
  module RiakRecord
2
2
  module Associations
3
3
 
4
+ def update_links
5
+ link_definitions.each_pair do |tag, definition|
6
+ tag = tag.to_s
7
+ bucket_name = Object.const_get(definition[:class_name].to_s).bucket_name.to_s
8
+ key = self.send(definition[:foreign_key].to_sym).first
9
+
10
+ # remove links with tag name
11
+ self.links.delete_if{|l| l.tag.to_s == tag }
12
+
13
+ # add link if key is set
14
+ self.links << Riak::Link.new(bucket_name.to_s, key.to_s, tag.to_s) unless key.nil?
15
+ end
16
+ end
17
+
18
+ def link_definitions
19
+ self.class.link_definitions
20
+ end
21
+
4
22
  module ClassMethods
23
+ def link_definitions
24
+ @link_definitions ||= {}
25
+ end
26
+
5
27
  def belongs_to_riak(association_name, options = {})
6
28
  class_name = options[:class_name] ||= association_name.to_s.split("_").collect(&:capitalize).join
7
29
  foreign_key = options[:foreign_key] || "#{association_name}_id"
30
+
31
+ if options[:link]
32
+ raise ArgumentError, "link option only available for instances of RiakRecord" unless self < RiakRecord::Base
33
+ link_definitions[association_name.to_sym] = {:class_name => class_name, :foreign_key => foreign_key}
34
+ end
35
+
8
36
  method_def = <<-END_OF_RUBY
9
37
 
10
38
  def #{association_name}
@@ -35,12 +35,19 @@ module RiakRecord
35
35
  riak_object.indexes
36
36
  end
37
37
 
38
+ def links
39
+ riak_object.links
40
+ end
41
+
38
42
  def save
39
43
  creating = new_record?
40
44
 
41
45
  before_save!
42
46
  creating ? before_create! : before_update!
47
+
48
+ update_links
43
49
  riak_object.store(:returnbody => false)
50
+
44
51
  creating ? after_create! : after_update!
45
52
  after_save!
46
53
 
@@ -84,6 +91,14 @@ module RiakRecord
84
91
  self
85
92
  end
86
93
 
94
+ def update_attributes(attributes)
95
+ attributes.each_pair do |k,v|
96
+ setter = "#{k}=".to_sym
97
+ self.send(setter, v) if respond_to?(setter)
98
+ end
99
+ save
100
+ end
101
+
87
102
  def self.bucket_name(name = :not_a_name)
88
103
  @bucket_name = name.to_s unless name == :not_a_name
89
104
  namespace.present? ? namespace_prefixed+@bucket_name : @bucket_name
data/riak-record.gemspec CHANGED
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: riak-record 0.3.1 ruby lib
5
+ # stub: riak-record 0.4.0 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "riak-record"
9
- s.version = "0.3.1"
9
+ s.version = "0.4.0"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["Robert Graff"]
14
- s.date = "2014-09-27"
14
+ s.date = "2014-09-29"
15
15
  s.description = "RiakRecord is a thin and immature wrapper around riak-ruby-client. It creates a bucket for\n each class, provides a simple finder, and creates attribute reader."
16
16
  s.email = "robert_graff@yahoo.com"
17
17
  s.extra_rdoc_files = [
@@ -29,7 +29,9 @@ class Comment < RiakRecord::Base
29
29
  bucket_name "comments"
30
30
  data_attributes :comment
31
31
 
32
- index_int_attributes :post_id
32
+ belongs_to :linked_author, :class_name => 'Author', :foreign_key => :author_id, :link => true
33
+
34
+ index_int_attributes :post_id, :author_id
33
35
  end
34
36
 
35
37
  describe RiakRecord::Associations do
@@ -64,6 +66,40 @@ describe RiakRecord::Associations do
64
66
 
65
67
  end
66
68
 
69
+ describe "linked belongs_to" do
70
+ let(:comment){ Comment.new("1") }
71
+ let(:author){ Author.new("2") }
72
+
73
+ it "should add links" do
74
+ expect{
75
+ comment.update_attributes(:author_id => 'a2')
76
+ }.to change{ comment.links.count }.from(0).to(1)
77
+ end
78
+
79
+ it "should link to the related object" do
80
+ comment.update_attributes(:author_id => 2)
81
+
82
+ link = comment.links.first
83
+ expect(link.tag).to eq("linked_author")
84
+ expect(link.key).to eq("2")
85
+ expect(link.bucket).to eq(Author.bucket_name)
86
+ end
87
+
88
+ it "should update when changed" do
89
+ comment.update_attributes(:author_id => 2)
90
+ expect{
91
+ comment.update_attributes(:author_id => 3)
92
+ }.to change{comment.links.first.key}.from("2").to("3")
93
+ end
94
+
95
+ it "should delete with nil'd" do
96
+ comment.update_attributes(:author_id => 2)
97
+ expect{
98
+ comment.update_attributes(:author_id => nil)
99
+ }.to change{comment.links.count}.from(1).to(0)
100
+ end
101
+ end
102
+
67
103
  describe "has_many_riak" do
68
104
  before :each do
69
105
  @comment = Comment.new("1")
@@ -67,6 +67,19 @@ describe RiakRecord::Base do
67
67
  end
68
68
  end
69
69
 
70
+ describe "update_attributes" do
71
+ let(:record){ ExampleA.new("1234") }
72
+ it "should set the values" do
73
+ expect{
74
+ record.update_attributes(:attribute1 => 'here')
75
+ }.to change{ record.attribute1 }.to('here')
76
+ end
77
+ it "should call save" do
78
+ expect(record).to receive(:save)
79
+ record.update_attributes({})
80
+ end
81
+ end
82
+
70
83
  describe "new_record?" do
71
84
  let(:record){ ExampleA.new("1") }
72
85
  it "should be true for a new record" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: riak-record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Graff
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-27 00:00:00.000000000 Z
11
+ date: 2014-09-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: riak-client