bsns 0.3.2 → 0.3.3

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.
Files changed (3) hide show
  1. data/lib/bsns.rb +37 -18
  2. data/spec/bsns_spec.rb +5 -0
  3. metadata +3 -2
data/lib/bsns.rb CHANGED
@@ -11,8 +11,25 @@ module BSNS
11
11
  model = model.to_s.singularize
12
12
  class_eval do
13
13
  define_method field do
14
- fields = instance_variable_get "@#{field}"
15
- collection_from_array fields, model, opts
14
+ obj_from_dataset instance_variable_get("@#{field}"), model, opts
15
+ end
16
+ end
17
+ end
18
+
19
+ def has_one model, opts = {}
20
+ model = model.to_s
21
+ field = opts[:as] || model.downcase
22
+ class_eval do
23
+ define_method field do
24
+ klass = model.capitalize.constantize.load
25
+ data = instance_variable_get "@#{field}"
26
+ if opts[:embedded]
27
+ obj = klass.new data
28
+ else
29
+ if data.type_of? Hash
30
+ obj = klass.load data[0]
31
+ end
32
+ end
16
33
  end
17
34
  end
18
35
  end
@@ -125,24 +142,26 @@ module BSNS
125
142
 
126
143
  # Return an array of all the references for a thing.
127
144
  def collection_from_array arr, model, opts
145
+ arr.map { |d| obj_from_dataset d, model, opts }
146
+ end
147
+
148
+ def obj_from_dataset d, model, opts={}
128
149
  extra_key = opts[:with]
129
- arr.map do |d|
130
- if d.instance_of?(Hash) && !opts[:embedded]
131
- # Normalize {:id => :linking_value} to [:id, :linking_value]
132
- id = d.keys[0]
133
- value = d[id]
134
- d = [id, value]
135
- end
136
- klass = obj = model.to_s.capitalize.constantize
137
- if opts[:embedded]
138
- obj = klass.new d
139
- else
140
- id = d.instance_of?(Array) ? d[0] : d
141
- obj = klass.load id
142
- end
143
- obj.define_linking_field extra_key, d[1] if extra_key
144
- obj
150
+ if d.instance_of?(Hash) && !opts[:embedded]
151
+ # Normalize {:id => :linking_value} to [:id, :linking_value]
152
+ id = d.keys[0]
153
+ value = d[id]
154
+ d = [id, value]
155
+ end
156
+ klass = obj = model.to_s.capitalize.constantize
157
+ if opts[:embedded]
158
+ obj = klass.new d
159
+ else
160
+ id = d.instance_of?(Array) ? d[0] : d
161
+ obj = klass.load id
145
162
  end
163
+ obj.define_linking_field extra_key, d[1] if extra_key
164
+ obj
146
165
  end
147
166
 
148
167
  def define_linking_field field, value
data/spec/bsns_spec.rb CHANGED
@@ -65,6 +65,11 @@ describe BSNS do
65
65
  fizz.fizzles[0].name.should == "Fazzy Fazz Fazz"
66
66
  end
67
67
 
68
+ it "should load embedded has_one associations" do
69
+ buzz = Buzzle.load 'buzz'
70
+ buzz.cat.thing.should == 'tiedye shirts'
71
+ end
72
+
68
73
  it "should load self-referential associations with extra keys" do
69
74
  buzz = Buzzle.load 'buzz'
70
75
  buzz.friendships.length.should == 2
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bsns
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -24,7 +24,8 @@ files:
24
24
  - spec/bsns_spec.rb
25
25
  - Gemfile
26
26
  homepage: http://github.com/Yuffster/bsns
27
- licenses: []
27
+ licenses:
28
+ - MIT
28
29
  post_install_message:
29
30
  rdoc_options: []
30
31
  require_paths: