yaml_model 0.0.1

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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Michael Bleigh
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = YamlModel
2
+
3
+ Because sometimes you just don't need to write to your models.
4
+ YamlModel is a simple wrapper that provides some of the functionality
5
+ of an ORM for completely static, YAML-based data.
6
+
7
+ == Note on Patches/Pull Requests
8
+
9
+ * Fork the project.
10
+ * Make your feature addition or bug fix.
11
+ * Add tests for it. This is important so I don't break it in a future version unintentionally.
12
+ * Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Intridea, Inc. and Michael Bleigh. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "yaml_model"
8
+ gem.summary = %Q{A basic ORM-like setup for reading YAML files.}
9
+ gem.description = %Q{A basic ORM-like setup for reading YAML files.}
10
+ gem.email = "michael@intridea.com"
11
+ gem.homepage = "http://github.com/intridea/yaml_model"
12
+ gem.authors = ["Michael Bleigh"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "yaml_model #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
data/lib/yaml_model.rb ADDED
@@ -0,0 +1,80 @@
1
+ require 'yaml'
2
+
3
+ # YamlModel is a super-lightweight datastore for
4
+ # loading and accessing fixed, infrequently changing
5
+ # sets of data such as currencies or languages.
6
+ class YamlModel
7
+ attr_reader :key
8
+
9
+ class << self
10
+ def yaml_file(path_or_file = nil)
11
+ return @yaml_file unless path_or_file
12
+ @yaml_file = path_or_file
13
+ end
14
+
15
+ # The complete hash of existing themes.
16
+ def collection
17
+ @collection ||= YAML.load_file(yaml_file).freeze
18
+ end
19
+
20
+ # The complete instantiated list of models
21
+ def all
22
+ @all ||= collection.inject([]){|result, (key, value)| result << self.new(key.to_s, value); result}.freeze
23
+ end
24
+
25
+ # Reset the collection to be reloaded again from YAML on the next call.
26
+ def reset!
27
+ @collection, @all, @instances = nil
28
+ end
29
+
30
+ # Find the mode
31
+ def [](key)
32
+ return nil unless collection.key?(key.to_s)
33
+ (@instances ||= {})[key.to_s] ||= self.new(key, collection[key.to_s])
34
+ end
35
+ alias_method :find, :[]
36
+
37
+ def attribute(name, options = {})
38
+ (@attributes ||= []) << name.to_s
39
+ (@defaults ||= {})[name.to_s] = options.delete(:default)
40
+
41
+ class_eval do
42
+ attr_reader name
43
+ end
44
+ end
45
+
46
+ attr_reader :attributes
47
+ attr_reader :defaults
48
+ end
49
+
50
+ # Instantiate the model with the provided key and attributes.
51
+ def initialize(key, attrs = {})
52
+ attrs = attrs.inject({}){|h,(k,v)| h[k.to_s] = v; h}
53
+ @key = key.to_s
54
+ self.class.attributes.each do |attribute|
55
+ instance_variable_set("@#{attribute}", attrs[attribute.to_s] || self.class.defaults[attribute.to_s])
56
+ end
57
+ end
58
+
59
+ def ==(other)
60
+ other.is_a?(self.class) && self.key == other.key
61
+ end
62
+
63
+ def <=>(other)
64
+ -(other <=> self.key)
65
+ end
66
+
67
+ def < (other); (self <=> other) < 0 end
68
+ def <= (other); (self <=> other) <= 0 end
69
+ def >= (other); (self <=> other) >= 0 end
70
+ def > (other); (self <=> other) > 0 end
71
+
72
+ def to_s
73
+ key
74
+ end
75
+ alias_method :to_yaml, :to_s
76
+
77
+ def to_sym
78
+ key.to_sym
79
+ end
80
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'yaml_model'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
@@ -0,0 +1,171 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ class ExampleYamlModel < YamlModel
4
+ attribute :name
5
+ attribute :scheme, :default => 'skeeball'
6
+ end
7
+
8
+ describe YamlModel do
9
+ before do
10
+ @fixture = {
11
+ "ABC" => {
12
+ "name" => "Always Be Coding"
13
+ },
14
+
15
+ "DEF" => {
16
+ "name" => "Defense Electric Fort",
17
+ "scheme" => "snowball"
18
+ },
19
+
20
+ "GHI" => {
21
+ "name" => "Gordon's Heaving International",
22
+ "scheme" => "foosball"
23
+ }
24
+ }
25
+
26
+ YAML.stub!(:load_file).and_return(@fixture)
27
+ end
28
+
29
+ describe '.yaml_file' do
30
+ it 'should set and retrieve a class instance variable' do
31
+ ExampleYamlModel.yaml_file '/awesome.yml'
32
+ ExampleYamlModel.yaml_file.should == '/awesome.yml'
33
+ end
34
+ end
35
+
36
+ describe '.collection' do
37
+ it 'should be the YAML loading of config/tableized_model_name.yml with symbolized keys' do
38
+ YAML.should_receive(:load_file).and_return(@theme_fixture)
39
+ ExampleYamlModel.collection
40
+ end
41
+
42
+ it 'should be a hash' do
43
+ ExampleYamlModel.collection.should be_kind_of(Hash)
44
+ end
45
+
46
+ it 'should memoize' do
47
+ YAML.should_receive(:load_file).once.and_return(@theme_fixture)
48
+ 3.times{ExampleYamlModel.collection}
49
+ end
50
+ end
51
+
52
+ describe '.all' do
53
+ it 'should instantiate an array of the example objects' do
54
+ ExampleYamlModel.all.should be_include(ExampleYamlModel[:GHI])
55
+ end
56
+
57
+ it 'should be the same length as the collection' do
58
+ ExampleYamlModel.all.size.should == ExampleYamlModel.collection.size
59
+ end
60
+
61
+ it 'should initialize with proper attributes' do
62
+ t = ExampleYamlModel.all.select{|e| e.key == "ABC"}.first
63
+ t.name.should == "Always Be Coding"
64
+ end
65
+
66
+ it 'should memoize' do
67
+ collection = ExampleYamlModel.collection
68
+ ExampleYamlModel.should_receive(:collection).once.and_return(collection)
69
+ 3.times{ExampleYamlModel.all}
70
+ end
71
+ end
72
+
73
+ describe '.reset!' do
74
+ it 'should clear the collection' do
75
+ ExampleYamlModel.instance_variable_set(:@collection, "abc")
76
+ ExampleYamlModel.reset!
77
+ ExampleYamlModel.instance_variable_get(:@collection).should be_nil
78
+ end
79
+
80
+ it 'should clear the .all cache' do
81
+ ExampleYamlModel.instance_variable_set(:@all, "aoisdasd")
82
+ ExampleYamlModel.reset!
83
+ ExampleYamlModel.instance_variable_get(:@all).should be_nil
84
+ end
85
+ end
86
+
87
+ describe "[]" do
88
+ it 'should instantiate the theme specified by the key' do
89
+ ExampleYamlModel[:ABC].name.should == 'Always Be Coding'
90
+ end
91
+
92
+ it 'should be nil if a non-existent theme is passed' do
93
+ ExampleYamlModel[:nonexistent].should be_nil
94
+ end
95
+
96
+ it 'should be indifferent to string/symbol' do
97
+ ExampleYamlModel["ABC"].should == ExampleYamlModel[:ABC]
98
+ end
99
+
100
+ it 'should be memoized' do
101
+ ExampleYamlModel.should_receive(:new).once.with("ABC", "name" => 'Always Be Coding').and_return('wakka')
102
+ 3.times{ExampleYamlModel["ABC"]}
103
+ end
104
+ end
105
+
106
+ describe ' sorting' do
107
+ it ' <=> should compare using the key' do
108
+ (ExampleYamlModel.new('abc') <=> ExampleYamlModel.new('bed')).should == -1
109
+ (ExampleYamlModel.new('abc') <=> ExampleYamlModel.new('abc')).should == 0
110
+ (ExampleYamlModel.new('bed') <=> ExampleYamlModel.new('abc')).should == 1
111
+ end
112
+
113
+ it 'should use < properly' do
114
+ (ExampleYamlModel.new('abc') < ExampleYamlModel.new('bed')).should == true
115
+ (ExampleYamlModel.new('bed') < ExampleYamlModel.new('abc')).should == false
116
+ (ExampleYamlModel.new('bed') < ExampleYamlModel.new('bed')).should == false
117
+ end
118
+
119
+ it 'should use > properly' do
120
+ (ExampleYamlModel.new('abc') > ExampleYamlModel.new('bed')).should == false
121
+ (ExampleYamlModel.new('bed') > ExampleYamlModel.new('abc')).should == true
122
+ (ExampleYamlModel.new('bed') > ExampleYamlModel.new('bed')).should == false
123
+ end
124
+
125
+ it 'should use >= properly' do
126
+ (ExampleYamlModel.new('abc') >= ExampleYamlModel.new('bed')).should == false
127
+ (ExampleYamlModel.new('bed') >= ExampleYamlModel.new('abc')).should == true
128
+ (ExampleYamlModel.new('bed') <= ExampleYamlModel.new('bed')).should == true
129
+ end
130
+
131
+ it 'should use <= properly' do
132
+ (ExampleYamlModel.new('abc') <= ExampleYamlModel.new('bed')).should == true
133
+ (ExampleYamlModel.new('bed') <= ExampleYamlModel.new('abc')).should == false
134
+ (ExampleYamlModel.new('bed') <= ExampleYamlModel.new('bed')).should == true
135
+ end
136
+ end
137
+
138
+ describe '.attribute' do
139
+ it 'should add the specified attribute to the attributes list' do
140
+ ExampleYamlModel.attributes.should be_include('name')
141
+ end
142
+
143
+ it 'should be able to specify a default' do
144
+ ExampleYamlModel.new(:test).scheme.should == 'skeeball'
145
+ end
146
+ end
147
+
148
+ describe ".find" do
149
+ it 'should be the same as ExampleYamlModel[]' do
150
+ ExampleYamlModel.find(:ABC).should == ExampleYamlModel[:ABC]
151
+ end
152
+ end
153
+
154
+ describe '#new' do
155
+ it 'should allow for the attributes to be set' do
156
+ ExampleYamlModel.new(:test, :name => 'Awesome ExampleYamlModel').name.should == 'Awesome ExampleYamlModel'
157
+ ExampleYamlModel.new(:test, :scheme => 'bocciball').scheme.should == 'bocciball'
158
+ ExampleYamlModel.new(:test).key.should == "test"
159
+ end
160
+ end
161
+
162
+ describe '#to_s' do
163
+ it 'should be the string representation of the key' do
164
+ ExampleYamlModel[:ABC].to_s.should == ExampleYamlModel[:ABC].key.to_s
165
+ end
166
+ end
167
+
168
+ after do
169
+ ExampleYamlModel.reset!
170
+ end
171
+ end
@@ -0,0 +1,55 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{yaml_model}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Michael Bleigh"]
12
+ s.date = %q{2010-04-01}
13
+ s.description = %q{A basic ORM-like setup for reading YAML files.}
14
+ s.email = %q{michael@intridea.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/yaml_model.rb",
27
+ "spec/spec.opts",
28
+ "spec/spec_helper.rb",
29
+ "spec/yaml_model_spec.rb",
30
+ "yaml_model.gemspec"
31
+ ]
32
+ s.homepage = %q{http://github.com/intridea/yaml_model}
33
+ s.rdoc_options = ["--charset=UTF-8"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.6}
36
+ s.summary = %q{A basic ORM-like setup for reading YAML files.}
37
+ s.test_files = [
38
+ "spec/spec_helper.rb",
39
+ "spec/yaml_model_spec.rb"
40
+ ]
41
+
42
+ if s.respond_to? :specification_version then
43
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
47
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
48
+ else
49
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
50
+ end
51
+ else
52
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
53
+ end
54
+ end
55
+
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yaml_model
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Michael Bleigh
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-01 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 2
30
+ - 9
31
+ version: 1.2.9
32
+ type: :development
33
+ version_requirements: *id001
34
+ description: A basic ORM-like setup for reading YAML files.
35
+ email: michael@intridea.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files:
41
+ - LICENSE
42
+ - README.rdoc
43
+ files:
44
+ - .document
45
+ - .gitignore
46
+ - LICENSE
47
+ - README.rdoc
48
+ - Rakefile
49
+ - VERSION
50
+ - lib/yaml_model.rb
51
+ - spec/spec.opts
52
+ - spec/spec_helper.rb
53
+ - spec/yaml_model_spec.rb
54
+ - yaml_model.gemspec
55
+ has_rdoc: true
56
+ homepage: http://github.com/intridea/yaml_model
57
+ licenses: []
58
+
59
+ post_install_message:
60
+ rdoc_options:
61
+ - --charset=UTF-8
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ requirements: []
79
+
80
+ rubyforge_project:
81
+ rubygems_version: 1.3.6
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: A basic ORM-like setup for reading YAML files.
85
+ test_files:
86
+ - spec/spec_helper.rb
87
+ - spec/yaml_model_spec.rb