static_model 0.1.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.
data/History.txt ADDED
@@ -0,0 +1,6 @@
1
+ == 0.1.0 2008-07-09
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
5
+ - Basic functionality
6
+ - Full test suite using shoulda
data/License.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 FIXME full name
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/Manifest.txt ADDED
@@ -0,0 +1,16 @@
1
+ History.txt
2
+ License.txt
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ config/hoe.rb
7
+ config/requirements.rb
8
+ lib/static_model.rb
9
+ lib/static_model/base.rb
10
+ lib/static_model/errors.rb
11
+ lib/static_model/rails.rb
12
+ lib/static_model/version.rb
13
+ setup.rb
14
+ test/books.yml
15
+ test/test_helper.rb
16
+ test/test_static_model.rb
data/README.txt ADDED
@@ -0,0 +1,44 @@
1
+ = static_model
2
+
3
+ http://github.com/quirkey/static_model/
4
+
5
+ == DESCRIPTION:
6
+
7
+ ActiveRecord like functionalities for reading from YAML with a simple class implementation
8
+
9
+ == SYNOPSIS:
10
+
11
+ Use like ActiveRecord::Base, except no database, just a YAML file. The YAML should contain an array of records.
12
+
13
+ == REQUIREMENTS:
14
+
15
+ YAML
16
+
17
+ == INSTALL:
18
+
19
+ sudo gem install static_model
20
+
21
+ == LICENSE:
22
+
23
+ (The MIT License)
24
+
25
+ Copyright (c) 2008 Aaron Quint, Quirkey NYC, LLC
26
+
27
+ Permission is hereby granted, free of charge, to any person obtaining
28
+ a copy of this software and associated documentation files (the
29
+ 'Software'), to deal in the Software without restriction, including
30
+ without limitation the rights to use, copy, modify, merge, publish,
31
+ distribute, sublicense, and/or sell copies of the Software, and to
32
+ permit persons to whom the Software is furnished to do so, subject to
33
+ the following conditions:
34
+
35
+ The above copyright notice and this permission notice shall be
36
+ included in all copies or substantial portions of the Software.
37
+
38
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
39
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
40
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
41
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
42
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
43
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
44
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require 'config/requirements'
2
+ require 'config/hoe' # setup Hoe + all gem configuration
3
+
4
+ Dir['tasks/**/*.rake'].each { |rake| load rake }
data/config/hoe.rb ADDED
@@ -0,0 +1,73 @@
1
+ require 'static_model/version'
2
+
3
+ AUTHOR = 'Aaron Quint' # can also be an array of Authors
4
+ EMAIL = "aaron@quirkey.com"
5
+ DESCRIPTION = "ActiveRecord like functionalities for reading from YAML with a simple class implementation"
6
+ GEM_NAME = 'static_model' # what ppl will type to install your gem
7
+ RUBYFORGE_PROJECT = 'quirkey' # The unix name for your project
8
+ HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
9
+ DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
10
+ EXTRA_DEPENDENCIES = [
11
+ # ['activesupport', '>= 1.3.1']
12
+ ] # An array of rubygem dependencies [name, version]
13
+
14
+ @config_file = "~/.rubyforge/user-config.yml"
15
+ @config = nil
16
+ RUBYFORGE_USERNAME = "unknown"
17
+ def rubyforge_username
18
+ unless @config
19
+ begin
20
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
21
+ rescue
22
+ puts <<-EOS
23
+ ERROR: No rubyforge config file found: #{@config_file}
24
+ Run 'rubyforge setup' to prepare your env for access to Rubyforge
25
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
26
+ EOS
27
+ exit
28
+ end
29
+ end
30
+ RUBYFORGE_USERNAME.replace @config["username"]
31
+ end
32
+
33
+
34
+ REV = nil
35
+ # UNCOMMENT IF REQUIRED:
36
+ # REV = YAML.load(`svn info`)['Revision']
37
+ VERS = StaticModel::VERSION::STRING + (REV ? ".#{REV}" : "")
38
+ RDOC_OPTS = ['--quiet', '--title', 'static_model documentation',
39
+ "--opname", "index.html",
40
+ "--line-numbers",
41
+ "--main", "README",
42
+ "--inline-source"]
43
+
44
+ class Hoe
45
+ def extra_deps
46
+ @extra_deps.reject! { |x| Array(x).first == 'hoe' }
47
+ @extra_deps
48
+ end
49
+ end
50
+
51
+ # Generate all the Rake tasks
52
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
53
+ $hoe = Hoe.new(GEM_NAME, VERS) do |p|
54
+ p.developer(AUTHOR, EMAIL)
55
+ p.description = DESCRIPTION
56
+ p.summary = DESCRIPTION
57
+ p.url = HOMEPATH
58
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
59
+ p.test_globs = ["test/**/test_*.rb"]
60
+ p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
61
+
62
+ # == Optional
63
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
64
+ #p.extra_deps = EXTRA_DEPENDENCIES
65
+
66
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
67
+ end
68
+
69
+ CHANGES = $hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
70
+ PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
71
+ $hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
72
+ $hoe.rsync_args = '-av --delete --ignore-errors'
73
+ $hoe.spec.post_install_message = File.open(File.dirname(__FILE__) + "/../PostInstall.txt").read rescue ""
@@ -0,0 +1,15 @@
1
+ require 'fileutils'
2
+ include FileUtils
3
+
4
+ require 'rubygems'
5
+ %w[rake hoe newgem rubigen].each do |req_gem|
6
+ begin
7
+ require req_gem
8
+ rescue LoadError
9
+ puts "This Rakefile requires the '#{req_gem}' RubyGem."
10
+ puts "Installation: gem install #{req_gem} -y"
11
+ exit
12
+ end
13
+ end
14
+
15
+ $:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
@@ -0,0 +1,137 @@
1
+ module StaticModel
2
+ class Base
3
+
4
+ @@load_path = File.join('config', 'data')
5
+
6
+ attr_reader :id
7
+
8
+ def initialize(attribute_hash = {})
9
+ raise(StaticModel::BadOptions, "Initializing a model is done with a Hash {}") unless attribute_hash.is_a?(Hash)
10
+ @id = attribute_hash.delete('id') || attribute_hash.delete(:id) || (self.class.count + 1)
11
+ self.attributes = attribute_hash
12
+ end
13
+
14
+ def to_s
15
+ self.inspect
16
+ end
17
+
18
+ def attributes
19
+ @attributes ||= {}
20
+ end
21
+
22
+ def attributes=(attribute_hash)
23
+ attribute_hash.each {|k,v| set_attribute(k,v) }
24
+ end
25
+
26
+ def attribute_names
27
+ attributes.keys
28
+ end
29
+
30
+ class << self
31
+
32
+ def find(what, *args, &block)
33
+ case what
34
+ when Symbol
35
+ send("find_#{what}")
36
+ when Integer
37
+ find_by_id(what)
38
+ end
39
+ end
40
+
41
+ def find_by_id(id)
42
+ load
43
+ record = @@records.detect {|r| r.id == id }
44
+ raise(StaticModel::RecordNotFound, "Could not find record with id = #{id}") unless record
45
+ record
46
+ end
47
+
48
+ def [](id)
49
+ find_by_id(id)
50
+ end
51
+
52
+ def find_all
53
+ load
54
+ @@records
55
+ end
56
+ alias_method :all, :find_all
57
+
58
+ def find_first
59
+ load
60
+ @@records[0]
61
+ end
62
+ alias_method :first, :find_first
63
+
64
+ def find_all_by(attribute, value)
65
+ load
66
+ records = @@records.find_all {|r| r.send(attribute) == value }
67
+ end
68
+
69
+ def find_first_by(attribute, value)
70
+ load
71
+ records = @@records.find {|r| r.send(attribute) == value }
72
+ end
73
+
74
+ def load(reload = false)
75
+ return if loaded? && !reload
76
+ raise(StaticModel::DataFileNotFound, "You must set a data file to load from") unless File.readable?(data_file)
77
+ records = YAML::load_file(data_file)
78
+ @@records = records.dup.collect {|r| new(r) }
79
+ @@loaded = true
80
+ end
81
+
82
+ def loaded?
83
+ @@loaded ||= false
84
+ end
85
+
86
+ def data_file
87
+ @@data_file ||= default_data_file_path
88
+ end
89
+
90
+ def set_data_file(file_path)
91
+ raise(StaticModel::DataFileNotFound, "Could not find data file #{file_path}") unless File.readable?(file_path)
92
+ @@data_file = file_path
93
+ end
94
+
95
+ def count
96
+ @@records.length
97
+ end
98
+
99
+ protected
100
+ def default_data_file_path
101
+ File.join(@@load_path, "#{self.to_s.tableize}.yml")
102
+ end
103
+
104
+ private
105
+ def method_missing(meth, *args)
106
+ meth_name = meth.to_s
107
+ if meth_name =~ /^find_(all|first)_by_(.+)/
108
+ attribute_name = meth_name.gsub(/^find_(all|first)_by_/, '')
109
+ finder = meth_name.gsub(/_#{attribute_name}/, '')
110
+ return self.send(finder, attribute_name, *args)
111
+ end
112
+ super
113
+ end
114
+ end
115
+
116
+ protected
117
+ def set_attribute(name, value)
118
+ self.attributes[name.to_sym] = value
119
+ end
120
+
121
+ private
122
+ def method_missing(meth, *args)
123
+ attribute_name = meth.to_s.gsub(/=$/,'').to_sym
124
+ if attribute_names.include?(attribute_name)
125
+ if meth.to_s =~ /=$/
126
+ # set
127
+ return set_attribute(attribute_name, args[0])
128
+ else
129
+ # get
130
+ return attributes[attribute_name]
131
+ end
132
+ end
133
+ super
134
+ end
135
+
136
+ end
137
+ end
@@ -0,0 +1,6 @@
1
+ module StaticModel
2
+ class Error < RuntimeError; end;
3
+ class RecordNotFound < Error; end;
4
+ class DataFileNotFound < Error; end;
5
+ class BadOptions < Error; end;
6
+ end
@@ -0,0 +1,7 @@
1
+ if defined?(Rails)
2
+ module StaticModel
3
+ class Base
4
+ @@load_path = File.join(Rails.root,'config', 'data')
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ module StaticModel #:nodoc:
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ TINY = 0
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
@@ -0,0 +1,7 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require 'yaml'
5
+ require 'static_model/errors'
6
+ require 'static_model/base'
7
+ require 'static_model/rails'