bsns 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/Gemfile +3 -0
  2. data/lib/bsns.rb +85 -0
  3. data/spec/bsns_spec.rb +66 -0
  4. metadata +51 -0
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "activesupport-inflector", "~> 0.1.0"
data/lib/bsns.rb ADDED
@@ -0,0 +1,85 @@
1
+ require 'active_support/inflector'
2
+
3
+ module BSNS
4
+
5
+ @@config = {}
6
+
7
+ def has_many model, opts = {}
8
+ field = opts[:as] || model.to_s.downcase.pluralize
9
+ with = opts[:with] || nil
10
+ model = model.to_s.singularize
11
+ class_eval do
12
+ define_method field do
13
+ fields = instance_variable_get "@#{field}"
14
+ collection_from_array fields, model, with
15
+ end
16
+ end
17
+ end
18
+
19
+ def self.configure opts
20
+ @@config = opts
21
+ end
22
+
23
+ def self.get_config key
24
+ @@config[key.to_sym]
25
+ end
26
+
27
+ class Base
28
+
29
+ extend BSNS
30
+
31
+ attr_accessor :load_path
32
+
33
+ def initialize data={}
34
+ set_defaults
35
+ data.each do |k,v|
36
+ accessor = "#{k}="
37
+ if respond_to? accessor
38
+ self.send accessor, v
39
+ else
40
+ instance_variable_set "@#{k}", v
41
+ end
42
+ end
43
+ end
44
+
45
+ def self.class
46
+ self
47
+ end
48
+
49
+ def self.root_path
50
+ BSNS.get_config(:content_path).gsub(/\/$/, '')+'/'
51
+ end
52
+
53
+ def self.relative_path
54
+ self.class.name.to_s.downcase.pluralize
55
+ end
56
+
57
+ def set_defaults
58
+ end
59
+
60
+ def self.load file
61
+ data = {}
62
+ data['load_path'] = file
63
+ data = YAML.load_file root_path +
64
+ relative_path.gsub(/\/$/, '')+'/'+
65
+ file.to_s+'.yml'
66
+ self.new data
67
+ end
68
+
69
+ def define_linking_key field, value
70
+
71
+ end
72
+
73
+ def collection_from_array arr, model, extra_key = nil
74
+ # Return an array of all the references for a thing.
75
+ arr.map do |d|
76
+ id = d.instance_of?(Array) ? d[0] : d
77
+ obj = model.to_s.capitalize.constantize.load id
78
+ obj.define_linking_field extra_key, d[1] if extra_key
79
+ obj
80
+ end
81
+ end
82
+
83
+ end
84
+
85
+ end
data/spec/bsns_spec.rb ADDED
@@ -0,0 +1,66 @@
1
+ require 'yammy'
2
+
3
+ BSNS.configure :content_path => File.dirname(__FILE__) + '/fixtures/sample_content'
4
+
5
+ class Buzzle < BSNS::Base
6
+
7
+ attr_accessor :name, :age, :years
8
+
9
+ has_many :buzzles, :as => :friendships, :with => :years
10
+ has_many :fizzles, :as => :enemies, :with => :reason
11
+ has_many :fizzles
12
+
13
+ end
14
+
15
+ class Fizzle < BSNS::Base
16
+
17
+ attr_accessor :name, :reason
18
+
19
+ has_many :fizzles
20
+
21
+ def self.relative_path
22
+ 'custom_dir_for_fizzles'
23
+ end
24
+
25
+ end
26
+
27
+ describe BSNS do
28
+
29
+ it "should load from content_directory" do
30
+ buzz = Buzzle.load 'buzz'
31
+ buzz.name.should == "Buzz Buzz Buzz"
32
+ buzz.age.should == 25
33
+ end
34
+
35
+ it "should load data from custom content_directory" do
36
+ fizz = Fizzle.load 'fizz'
37
+ fizz.name.should == 'Fizzy Fizz'
38
+ end
39
+
40
+ it "should load default associations" do
41
+ buzz = Buzzle.load 'buzz'
42
+ buzz.fizzles.length.should == 2
43
+ buzz.fizzles[0].name.should == "Fazzy Fazz Fazz"
44
+ 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
65
+
66
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bsns
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Michelle Steigerwalt
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-20 00:00:00.000000000 Z
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.
17
+ email: msteigerwalt@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - lib/bsns.rb
23
+ - spec/bsns_spec.rb
24
+ - Gemfile
25
+ homepage: http://github.com/Yuffster/bsns
26
+ licenses: []
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ none: false
33
+ requirements:
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project: bsns
45
+ rubygems_version: 1.8.24
46
+ signing_key:
47
+ specification_version: 3
48
+ summary: Business-oriented Semantic Node Structures. (A simple YAML-based way for
49
+ people to drop content and static data into your code's repository.)
50
+ test_files: []
51
+ has_rdoc: