dokument 1.0.0.alpha → 1.0.0.beta

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,11 +1,12 @@
1
1
  module Dokument
2
2
  class Association
3
3
 
4
- attr_accessor :name, :key, :bucket
4
+ attr_accessor :name, :owner, :key, :bucket
5
5
 
6
- def initialize(name, options = {})
6
+ def initialize(name, owner, options = {})
7
7
  self.name = name
8
- self.bucket = options[:bucket] || name.to_s
8
+ self.owner = owner
9
+ self.bucket = options[:bucket] || "#{owner.name}_#{name}"
9
10
  self.key = options[:key] || :id
10
11
  end
11
12
 
@@ -4,7 +4,7 @@ module Dokument
4
4
  class Attachment
5
5
  extend Forwardable
6
6
 
7
- delegate %w[data data= content_type content_type=] => :robject
7
+ delegate %w[raw_data raw_data= content_type content_type=] => :robject
8
8
 
9
9
  attr_reader :model, :association
10
10
 
@@ -17,9 +17,17 @@ module Dokument
17
17
  @robject ||= bucket.get_or_new(model.send(association.key).to_s)
18
18
  end
19
19
 
20
- def save
20
+ def data
21
+ raw_data
22
+ end
23
+
24
+ def data=(value)
25
+ self.raw_data = value
26
+ end
27
+
28
+ def save(options={})
21
29
  raise IncompleteError.new("Attachment is missing content_type or data (#{inspect})") unless complete?
22
- robject.store
30
+ robject.store(options)
23
31
  end
24
32
 
25
33
  def incomplete?
@@ -35,7 +43,7 @@ module Dokument
35
43
  def bucket
36
44
  Dokument.client[association.bucket]
37
45
  end
38
-
46
+
39
47
  class IncompleteError < StandardError ; end
40
48
 
41
49
  end
@@ -11,7 +11,7 @@ module Dokument
11
11
  end
12
12
 
13
13
  def dokument(name, options={})
14
- dokuments[name] = Association.new(name)
14
+ dokuments[name] = Association.new(name, self, options)
15
15
  add_association_reader(name)
16
16
  end
17
17
 
@@ -1,3 +1,3 @@
1
1
  module Dokument
2
- VERSION = "1.0.0.alpha"
2
+ VERSION = "1.0.0.beta"
3
3
  end
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe Dokument::Association do
4
4
 
5
5
  before :each do
6
- @association = Dokument::Association.new(:file)
6
+ @association = Dokument::Association.new(:file, ExampleModel)
7
7
  end
8
8
 
9
9
  it "take a name upon initialization" do
@@ -23,14 +23,14 @@ describe Dokument::Association do
23
23
  @association.key.should eq(:id)
24
24
  end
25
25
 
26
- it "defaults the bucket to the string version of the name" do
27
- @association.bucket.should eq('file')
26
+ it "defaults the bucket to the string version of the name with the model class" do
27
+ @association.bucket.should eq('ExampleModel_file')
28
28
  end
29
29
  end
30
30
 
31
31
  describe "initialization options" do
32
32
  before :each do
33
- @association = Dokument::Association.new(:file, :bucket => 'files_bucket', :key => 'some_id')
33
+ @association = Dokument::Association.new(:file, ExampleModel, :bucket => 'files_bucket', :key => 'some_id')
34
34
  end
35
35
 
36
36
  it "can set the bucket name" do
@@ -20,11 +20,26 @@ describe Dokument::Attachment do
20
20
  @attachment.data.should eq(@data)
21
21
  end
22
22
 
23
- it "delegates saving to it's robject" do
23
+ it "delegates data to raw_data" do
24
+ @attachment.should_receive(:raw_data)
25
+ @attachment.data
26
+ end
27
+
28
+ it "delegates data= to raw_data=" do
29
+ @attachment.should_receive(:raw_data=).with('awesome')
30
+ @attachment.data = 'awesome'
31
+ end
32
+
33
+ it "delegates saving to its robject" do
24
34
  @attachment.robject.should_receive(:store)
25
35
  @attachment.save
26
36
  end
27
37
 
38
+ it "delegates options passed to save to the robject when storing" do
39
+ @attachment.robject.should_receive(:store).with(:awesome => true)
40
+ @attachment.save(:awesome => true)
41
+ end
42
+
28
43
  describe "completeness" do
29
44
  it "is true if the data and content_type are set" do
30
45
  @attachment.should be_complete
@@ -47,11 +62,11 @@ describe Dokument::Attachment do
47
62
  end
48
63
 
49
64
  describe "robject" do
50
- it "uses the attachments data" do
65
+ it "uses the attachment's data" do
51
66
  @attachment.robject.data.should eq(@data)
52
67
  end
53
68
 
54
- it "uses the attachments content_type" do
69
+ it "uses the attachment's content_type" do
55
70
  @attachment.robject.content_type.should eq(@content_type)
56
71
  end
57
72
 
@@ -59,7 +74,7 @@ describe Dokument::Attachment do
59
74
  @attachment.robject.key.should eq(@model.id.to_s)
60
75
  end
61
76
 
62
- it "uses the associations bucket for the bucket name" do
77
+ it "uses the association's bucket for the bucket name" do
63
78
  @attachment.robject.bucket.name.should eq(@association.bucket)
64
79
  end
65
80
  end
@@ -10,6 +10,11 @@ describe Dokument::Model do
10
10
  ExampleModel.dokument :file
11
11
  ExampleModel.dokuments[:file].should be_a(Dokument::Association)
12
12
  end
13
+
14
+ it "accepts options with the `dokument` class method" do
15
+ ExampleModel.dokument :file, :bucket => 'files'
16
+ ExampleModel.dokuments[:file].bucket.should eq('files')
17
+ end
13
18
  end
14
19
 
15
20
  describe "provided instance methods" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dokument
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.alpha
4
+ version: 1.0.0.beta
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-12 00:00:00.000000000 Z
12
+ date: 2012-06-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: riak-client
16
- requirement: &70364752141440 !ruby/object:Gem::Requirement
16
+ requirement: &70128315028780 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.0.3
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70364752141440
24
+ version_requirements: *70128315028780
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70364752140940 !ruby/object:Gem::Requirement
27
+ requirement: &70128315028280 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: 2.10.0
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70364752140940
35
+ version_requirements: *70128315028280
36
36
  description: Provides a ripple document to give models 1 or n file attachments.
37
37
  email:
38
38
  - adamhunter@me.com