active_record_content_blob 0.1.4 → 0.1.5

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,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NDdhNzkzYWRkMTk2YmM1MjFmZTBlNzE1NTYzNzgxZDMzZjhhNTM4ZQ==
4
+ ZjdmMjg5N2E0ZTA1ZDAzZWUxYTFhM2YxZTUzNjgzZjRlNjdlNWFiNA==
5
5
  data.tar.gz: !binary |-
6
- ZmIxY2U3MzI0MjMxNzdhNzhhNWMxODA0NTFmOTc0MDljMjA4M2FmZA==
6
+ NzA3YmRmMDQ1YWVlZjhlNDkwMGRlZTk1ZDE3MDlhZDE2OWUxMjk5Ng==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- MWQ5ODRmMWJkOTFkMTQ1YzEzNjRiYTk4ZGUwNTVmODM0YWQ0ZDI4ZmI0YjM3
10
- NWE2Yjc2NDFhNDI0ZWMwYzdiMmE4M2E0ODcyOTY1MTVjNDJmNjAzNTBhYzg0
11
- Y2NhZDljMWYzMWVhOGU2MjU2YjBiMjkyNWQ0NmNkNjc2MzhhZDg=
9
+ MjgwNTcwZDZhOGY4NzcxODY5NTAyZjk2MTRlN2ZiMjg2MWQ4MGY5MTRlZGUx
10
+ NjNkYjVjN2VmMGExMTc4OGQ5MDA3NGJjMDRjOTgyOGNiZTc4YzkyOWE5ZmU2
11
+ MTMzNzYwOTM5ZjE5YWIxMjc1MDM4ZGZlMjBhNmMxNGM5NThkOTQ=
12
12
  data.tar.gz: !binary |-
13
- ZjA5NzkxMmUyZGFiN2M3ODkwNDAwZTlmZDA1ZmJjMjkyYWM0MDYzZmRlYmU2
14
- OWYxMjUxNDRhZTU2YjEwMzE0Zjc4N2JkZjFmZjFkNTVkYzY0MmQxYWE3Zjkx
15
- MWM2MDU4MTY4ZTRmMGUwMjc3NzhhNjQ2ZDQ4YjlkYWM5Y2E1YWI=
13
+ ZGYyYjFlYTZhNjg0NTA3MjQwMjcyNzBjMjFkZDUwYWFkNGU2NzAyMWUwNzRm
14
+ MDVlYWM4MGM5ZWEwNjg2MmUxYzAxN2ZiMTQzOGJjMGYxMDNlOTRkNWY0ODcx
15
+ NGJmMzNiN2UxMzgwNTA3NmY0YmM0MmMyODgxYTg5ZjRjNTJjNTE=
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.4
1
+ 0.1.5
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "active_record_content_blob"
8
- s.version = "0.1.4"
8
+ s.version = "0.1.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Dan Nguyen"]
@@ -22,31 +22,44 @@ module ActiveRecordContentBlob
22
22
 
23
23
  module ClassMethods
24
24
 
25
+
26
+
25
27
  # returns a new record with an attached blob
26
- def build_with_blob(hsh, blob_content=nil)
28
+ # e.g. Record.build_with_a_blob(record_hsh, record_big_hsh)
29
+ def build_with_a_blob(hsh, blob_content=nil)
27
30
  hsh_syms = hsh.symbolize_keys
28
- # delete a possible :content_blob so that we can initialize
29
- # the record
30
-
31
+
32
+ # exclude :contents from instantiating a new Record
31
33
  record = self.new(hsh_syms.reject{|k,v| k == :contents})
32
34
 
33
- if blob_content.nil?
34
- # then build a blob only if hsh_syms has :content_blob key
35
- if hsh_syms.key?(:contents)
36
- blob_content = hsh_syms[:contents]
37
- record.build_a_blob(blob_content)
38
- end
35
+ stuff = if blob_content.present?
36
+ # build a blob using :blob_content
37
+ blob_content
38
+ elsif c = hsh_syms[:contents]
39
+ c
39
40
  else
40
- # second argument is for the blo
41
- record.build_a_blob(blob_content)
41
+ # then build a blob from the hsh
42
+ hsh
42
43
  end
43
44
 
45
+
46
+ prepared_stuff = prepare_content_for_blob(stuff)
47
+
48
+ record.build_a_blob(prepared_stuff)
44
49
  return record
45
50
  end
46
51
 
47
- def create_with_blob(hsh, blob_content=nil)
48
- build_with_blob(hsh, blob_content).save
52
+
53
+
54
+ def create_with_a_blob(hsh, blob_content=nil)
55
+ build_with_a_blob(hsh, blob_content).save
56
+ end
57
+
58
+ # allow this to be redefined
59
+ def prepare_content_for_blob(some_content)
60
+ some_content
49
61
  end
62
+
50
63
  end # classmethods
51
64
 
52
65
  ## instance methods
@@ -54,6 +67,7 @@ module ActiveRecordContentBlob
54
67
  self.build_content_blob(contents: some_content)
55
68
  end
56
69
 
70
+
57
71
 
58
72
  def has_blob?
59
73
  content_blob.exists?
data/spec/spec_helper.rb CHANGED
@@ -4,6 +4,7 @@ require 'rspec'
4
4
  require 'active_record'
5
5
  require 'database_cleaner'
6
6
  require 'sqlite3'
7
+ require 'pry'
7
8
 
8
9
  require 'active_record_content_blob'
9
10
 
@@ -46,30 +46,58 @@ describe "Blobable" do
46
46
  end
47
47
  end
48
48
 
49
- describe 'class convenience method ::build_with_blob' do
49
+ describe 'class convenience method ::build_with_a_blob' do
50
50
  context 'one argument' do
51
51
  it 'should build associated content_blob with Hash :contents' do
52
- @record = MusicRecord.build_with_blob({name: 'X', contents: [1]})
52
+ @record = MusicRecord.build_with_a_blob({name: 'X', contents: [1]})
53
53
  expect(@record.contents).to eq [1]
54
54
  end
55
55
 
56
- it 'should build no content_blob without :contents' do
57
- @record = MusicRecord.build_with_blob(name: 'X')
58
- expect(@record.contents).to be_nil
56
+ it 'should build a content_blob, eq to first Hash, even without :contents' do
57
+ @record = MusicRecord.build_with_a_blob(name: 'X')
58
+ expect(@record.contents).to eq( {name: 'X'})
59
59
  end
60
60
  end
61
61
 
62
62
  context 'second argument' do
63
63
  it 'should use second argument as :contents for built blob' do
64
- @record = MusicRecord.build_with_blob({name: 'A'}, [99])
64
+ @record = MusicRecord.build_with_a_blob({name: 'A'}, [99])
65
65
  expect(@record.contents).to eq [99]
66
66
  end
67
67
  end
68
68
  end
69
69
 
70
+
71
+ context 'allow .prepare_content_blob hook to be redefined' do
72
+ before(:each) do
73
+ class MusicRecord < ActiveRecord::Base
74
+ def self.prepare_content_for_blob(some_content)
75
+ some_content.is_a?(Hash) ? [some_content] : Array(some_content)
76
+ end
77
+ end
78
+
79
+ MusicRecord
80
+ end
81
+
82
+ after(:each) do
83
+ class MusicRecord < ActiveRecord::Base
84
+ def self.prepare_content_for_blob(some_content)
85
+ some_content
86
+ end
87
+ end
88
+ end
89
+
90
+ it 'should redefine .prepare_content_blob as specified' do
91
+ @record = MusicRecord.build_with_a_blob({name: 'A'})
92
+ expect(@record.contents).to eq [{name: 'A'}]
93
+ end
94
+
95
+
96
+ end
97
+
70
98
  context 'scopes' do
71
99
  before(:each) do
72
- @r1 = MusicRecord.create_with_blob(name: 'A', contents: ['Z'])
100
+ @r1 = MusicRecord.create_with_a_blob(name: 'A', contents: ['Z'])
73
101
  @r2 = MusicRecord.create(name: 'qqq')
74
102
  end
75
103
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_content_blob
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Nguyen