bsns 0.1.0 → 0.2.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.
Files changed (4) hide show
  1. data/Gemfile +7 -1
  2. data/lib/bsns.rb +72 -10
  3. data/spec/bsns_spec.rb +76 -28
  4. metadata +6 -5
data/Gemfile CHANGED
@@ -1,3 +1,9 @@
1
1
  source "http://rubygems.org"
2
2
 
3
- gem "activesupport-inflector", "~> 0.1.0"
3
+ gem "activesupport-inflector", "~> 0.1.0"
4
+
5
+ group :test do
6
+
7
+ gem "rake"
8
+
9
+ end
data/lib/bsns.rb CHANGED
@@ -16,6 +16,10 @@ module BSNS
16
16
  end
17
17
  end
18
18
 
19
+ def acts_as_collection
20
+ set_v :single_file, true
21
+ end
22
+
19
23
  def self.configure opts
20
24
  @@config = opts
21
25
  end
@@ -28,10 +32,10 @@ module BSNS
28
32
 
29
33
  extend BSNS
30
34
 
31
- attr_accessor :load_path
32
-
33
35
  def initialize data={}
34
- set_defaults
36
+ get_defaults.each do |k,v|
37
+ data[k] ||= v
38
+ end
35
39
  data.each do |k,v|
36
40
  accessor = "#{k}="
37
41
  if respond_to? accessor
@@ -42,6 +46,35 @@ module BSNS
42
46
  end
43
47
  end
44
48
 
49
+ def get_defaults
50
+ data = get_v :defaults
51
+ unless data
52
+ data = self.class.load_or_nil "_defaults"
53
+ set_v :defaults, data
54
+ end
55
+ data || {}
56
+ end
57
+
58
+ def get_v k
59
+ self.class.get_v k
60
+ end
61
+
62
+ def set_v k, v
63
+ self.class.set_v k, v
64
+ end
65
+
66
+ def self.get_v k
67
+ begin
68
+ return class_variable_get "@@#{k}"
69
+ rescue
70
+ return nil
71
+ end
72
+ end
73
+
74
+ def self.set_v k, v
75
+ class_variable_set "@@#{k}", v
76
+ end
77
+
45
78
  def self.class
46
79
  self
47
80
  end
@@ -51,28 +84,52 @@ module BSNS
51
84
  end
52
85
 
53
86
  def self.relative_path
54
- self.class.name.to_s.downcase.pluralize
87
+ get_v(:relative_path) || self.class.name.to_s.downcase.pluralize
55
88
  end
56
89
 
57
- def set_defaults
90
+ def self.content_path p
91
+ set_v :relative_path, p
58
92
  end
59
93
 
60
- def self.load file
94
+ def self.load_data file
95
+ return load_from_collection file if get_v :single_file
61
96
  data = {}
62
97
  data['load_path'] = file
63
- data = YAML.load_file root_path +
64
- relative_path.gsub(/\/$/, '')+'/'+
65
- file.to_s+'.yml'
98
+ path = (root_path+relative_path.gsub(/\/$/, '')+'/')+file.to_s+'.yml'
99
+ throw "File not found: #{path}" unless File.exists? path
100
+ YAML.load_file path
101
+ end
102
+
103
+ def self.load file
104
+ data = load_data file
66
105
  self.new data
67
106
  end
68
107
 
69
- def define_linking_key field, value
108
+ def self.load_or_nil file
109
+ begin
110
+ load_data file
111
+ rescue
112
+ return nil
113
+ end
114
+ end
70
115
 
116
+ def self.load_from_collection f
117
+ unless get_v :data
118
+ data = YAML.load_file (root_path + relative_path + '.yml')
119
+ set_v :data, data
120
+ end
121
+ get_v(:data)[f.to_s]
71
122
  end
72
123
 
73
124
  def collection_from_array arr, model, extra_key = nil
74
125
  # Return an array of all the references for a thing.
75
126
  arr.map do |d|
127
+ if d.instance_of? Hash
128
+ # Normalize {:id => :linking_value} to [:id, :linking_value]
129
+ id = d.keys[0]
130
+ value = d[id]
131
+ d = [id, value]
132
+ end
76
133
  id = d.instance_of?(Array) ? d[0] : d
77
134
  obj = model.to_s.capitalize.constantize.load id
78
135
  obj.define_linking_field extra_key, d[1] if extra_key
@@ -80,6 +137,11 @@ module BSNS
80
137
  end
81
138
  end
82
139
 
140
+ def define_linking_field field, value
141
+ self.class.module_eval { attr_accessor field.to_sym }
142
+ self.send "#{field}=", value
143
+ end
144
+
83
145
  end
84
146
 
85
147
  end
data/spec/bsns_spec.rb CHANGED
@@ -1,26 +1,33 @@
1
- require 'yammy'
1
+ require 'bsns'
2
2
 
3
3
  BSNS.configure :content_path => File.dirname(__FILE__) + '/fixtures/sample_content'
4
4
 
5
5
  class Buzzle < BSNS::Base
6
6
 
7
- attr_accessor :name, :age, :years
7
+ attr_accessor :name, :age, :alignment
8
8
 
9
9
  has_many :buzzles, :as => :friendships, :with => :years
10
- has_many :fizzles, :as => :enemies, :with => :reason
11
10
  has_many :fizzles
11
+ has_many :fizzles, :as => :enemies, :with => :reason
12
+ has_many :fizzles, :as => :opinions, :with => :opinion
13
+
14
+ end
15
+
16
+ class Wizzle < BSNS::Base
17
+
18
+ attr_accessor :name, :powers, :species, :weaknesses
19
+
20
+ acts_as_collection
12
21
 
13
22
  end
14
23
 
15
24
  class Fizzle < BSNS::Base
16
25
 
17
- attr_accessor :name, :reason
26
+ attr_accessor :name
18
27
 
19
- has_many :fizzles
28
+ content_path 'custom_dir_for_fizzles'
20
29
 
21
- def self.relative_path
22
- 'custom_dir_for_fizzles'
23
- end
30
+ has_many :fizzles
24
31
 
25
32
  end
26
33
 
@@ -42,25 +49,66 @@ describe BSNS do
42
49
  buzz.fizzles.length.should == 2
43
50
  buzz.fizzles[0].name.should == "Fazzy Fazz Fazz"
44
51
  buzz.fizzles[1].name.should == "Fuzzy Wuzzy"
45
- end
46
-
47
- it "should load self-referential associations" do
48
- fizz = Fizzle.load 'fizz'
49
- fizz.fizzles.length.should == 1
50
- fizz.fizzles[0].name.should == "Fazzy Fazz Fazz"
51
- end
52
-
53
- it "should load self-referential associations with extra keys" do
54
- buzz = Buzzle.load 'buzz'
55
- buzz.friendships.length.should == 2
56
- buzz.friendships[1].name.should == 'Bizzy Bizz Bizz'
57
- buzz.friendships[0].name.should == 'Bazzy Bazz Bazz'
58
- end
59
-
60
- it "should load foreign associations with custom name" do
61
- buzz = Buzzle.load 'buzz'
62
- buzz.enemies.length.should == 2
63
- buzz.enemies[0].name.should == 'Fizzy Fizz'
64
- end
52
+ end
53
+
54
+ it "should load self-referential associations" do
55
+ fizz = Fizzle.load 'fizz'
56
+ fizz.fizzles.length.should == 1
57
+ fizz.fizzles[0].name.should == "Fazzy Fazz Fazz"
58
+ end
59
+
60
+ it "should load self-referential associations with extra keys" do
61
+ buzz = Buzzle.load 'buzz'
62
+ buzz.friendships.length.should == 2
63
+ buzz.friendships[1].name.should == 'Bizzy Bizz Bizz'
64
+ buzz.friendships[0].name.should == 'Bazzy Bazz Bazz'
65
+ buzz.friendships[1].years.should == 4
66
+ buzz.friendships[0].years.should == 5
67
+ end
68
+
69
+ it "should load foreign associations with custom name" do
70
+ buzz = Buzzle.load 'buzz'
71
+ buzz.enemies.length.should == 2
72
+ buzz.enemies[0].name.should == 'Fizzy Fizz'
73
+ end
74
+
75
+ it "should load foreign associations with extra linking field" do
76
+ buzz = Buzzle.load 'buzz'
77
+ buzz.enemies[0].reason.should == "Wasn't fizzy enough; ruined soda."
78
+ buzz.enemies[1].reason.should == "Got all over the rest of the clothes in the dryer."
79
+ end
80
+
81
+ it "should load associations in key/value hash form" do
82
+ buzz = Buzzle.load 'buzz'
83
+ buzz.opinions.length.should == 1
84
+ buzz.opinions[0].opinion.should == "Hasn't done anything particularly annoying, yet."
85
+ end
86
+
87
+ it "should load defaults from the _defaults.yml file" do
88
+ buzz = Buzzle.load 'buzz'
89
+ buzz.alignment.should == "lawful neutral"
90
+ end
91
+
92
+ it "should allow for overrides of default values" do
93
+ bizz = Buzzle.load 'bizz'
94
+ bizz.alignment.should == "chaotic neutral"
95
+ end
96
+
97
+ it "should load an item from a single-file collection" do
98
+ wizz = Wizzle.load 'wizzy'
99
+ wizz.name.should == "Whizz"
100
+ wizz.powers.length.should == 1
101
+ wizz.weaknesses.length.should == 1
102
+ end
103
+
104
+ it "should preload defaults from a single-file collection" do
105
+ wizz = Wizzle.load 'wizzy'
106
+ wizz.species.should == "human"
107
+ end
108
+
109
+ it "should allow override of preloaded defaults" do
110
+ wuzz = Wizzle.load 'wuzzy'
111
+ wuzz.species.should == "horse"
112
+ end
65
113
 
66
114
  end
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.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-20 00:00:00.000000000 Z
12
+ date: 2013-08-22 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: For the streamlined modification of informational, promotional and business
15
- assets by non-technical personnel, providing project stake-holders with an unparalleled
16
- ability to efficiently allocate resources and expedite engineering man hours.
14
+ description: Static YAML assets which live in your application repositories. For
15
+ the streamlined modification of informational, promotional and business assets by
16
+ non-technical personnel, providing project stake-holders with an unparalleled ability
17
+ to efficiently allocate resources and expedite engineering man hours.
17
18
  email: msteigerwalt@gmail.com
18
19
  executables: []
19
20
  extensions: []