text_record 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1 +1,2 @@
1
1
  coverage/
2
+ pkg/
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
@@ -8,7 +8,7 @@ module TextRecord
8
8
  attr_reader :slug
9
9
 
10
10
  def_delegators :configuration, :content_path, :attribute_file
11
-
11
+
12
12
  # Since attributes are unique to each subclass, have to the object's metaclass since it will be
13
13
  # unique to each subclass
14
14
 
@@ -27,12 +27,25 @@ module TextRecord
27
27
 
28
28
  @attributes << Attribute.for_options(options).new(name, options)
29
29
  end
30
- end
30
+
31
+ def directory
32
+ File.join(TextRecord.configuration.content_path, self.name.pluralize.underscore)
33
+ end
34
+
35
+ def all
36
+ items = Collection.new
37
+ Dir["#{directory}/*"].map do |dir|
38
+ slug = File.basename dir
39
+ items.push(new(slug))
40
+ end
41
+ items
42
+ end
43
+
44
+ def where(options)
45
+ all.where(options)
46
+ end
47
+ end
31
48
 
32
- # class methods
33
- def self.directory
34
- File.join(TextRecord.configuration.content_path, self.name.pluralize.underscore)
35
- end
36
49
 
37
50
  # Instance Methods
38
51
  def initialize(path)
@@ -45,6 +58,14 @@ module TextRecord
45
58
  end
46
59
  end
47
60
 
61
+ def ==(other_model)
62
+ !other_model.nil? && (other_model.class == self.class) && self.slug.eql?(other_model.slug)
63
+ end
64
+
65
+ def eql?(other_model)
66
+ self == other_model
67
+ end
68
+
48
69
  def configuration
49
70
  TextRecord.configuration
50
71
  end
@@ -0,0 +1,23 @@
1
+ module TextRecord
2
+ class Collection < Array
3
+ def where(options = {})
4
+ select do |object|
5
+ options.keys.inject(true) do |flag, attribute|
6
+ object.send(attribute).eql?(options[attribute]) && flag
7
+ end
8
+ end
9
+ end
10
+
11
+
12
+ def order(options)
13
+ attribute = options.keys.first
14
+ order = options[attribute]
15
+
16
+ sort do |object1, object2|
17
+ comp = object1.send(attribute) <=> object2.send(attribute)
18
+ comp = (order == :asc) ? comp : comp * -1
19
+ comp
20
+ end
21
+ end
22
+ end
23
+ end
@@ -10,4 +10,8 @@ module TextRecord
10
10
  # Raised when slug cannot be found
11
11
  class SlugNotFoundError < TextRecordError
12
12
  end
13
+
14
+ # Raised when a model doesn't have a required attribute
15
+ class AttributeNotFoundError < TextRecordError
16
+ end
13
17
  end
data/lib/text_record.rb CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  begin
3
3
  require 'active_support'
4
4
  rescue
5
- require 'active_support'
5
+ require 'activesupport'
6
6
  end
7
7
 
8
8
  module TextRecord
@@ -11,4 +11,5 @@ end
11
11
  require 'text_record/configuration'
12
12
  require 'text_record/errors'
13
13
  require 'text_record/attribute'
14
+ require 'text_record/collection'
14
15
  require 'text_record/base'
@@ -0,0 +1,5 @@
1
+ Title: Active Record
2
+
3
+ IntroductionText: This is an introduction paragraph
4
+
5
+ Fixnum: 5
@@ -0,0 +1 @@
1
+ This is some markdown
@@ -0,0 +1,3 @@
1
+ Title: Story1
2
+ PostedOn: 2009-01-01
3
+ Id: 1
@@ -0,0 +1,3 @@
1
+ Title: Story2
2
+ PostedOn: 2009-01-02
3
+ Id: 2
@@ -0,0 +1,3 @@
1
+ Title: Story3
2
+ PostedOn: 2009-01-03
3
+ Id: 3
@@ -0,0 +1,5 @@
1
+ class Story < TextRecord::Base
2
+ attribute :title
3
+ attribute :posted_on
4
+ attribute :id
5
+ end
data/spec/base_spec.rb CHANGED
@@ -50,5 +50,38 @@ describe TextRecord::Base do
50
50
  AttributeTest.attributes.first.options.should eql({:from => 'post.md'})
51
51
  end
52
52
  end
53
- end
53
+ end
54
+
55
+ describe "equality" do
56
+ it "should say two instances are the same if their slugs are the same using ==" do
57
+ story1 = Story.new('Story1')
58
+ story2 = Story.new('Story1')
59
+
60
+ (story1 == story2).should be_true
61
+ end
62
+
63
+ it "should say two instances are the same if their slugs are the same using eql?" do
64
+ story1 = Story.new('Story1')
65
+ story2 = Story.new('Story1')
66
+
67
+ story2.eql?(story1).should be_true
68
+ end
69
+ end
70
+
71
+ describe "#all" do
72
+ it "should return all the stories" do
73
+ stories = Story.all
74
+ story1 = Story.new('Story1')
75
+ story2 = Story.new('Story2')
76
+ story3 = Story.new('Story3')
77
+
78
+ stories.include?(story1).should be_true
79
+ stories.include?(story2).should be_true
80
+ stories.include?(story3).should be_true
81
+ end
82
+
83
+ it "should return a TextRecord::Collection" do
84
+ Story.all.should be_a(TextRecord::Collection)
85
+ end
86
+ end
54
87
  end
@@ -81,4 +81,18 @@ describe "A Blog Post" do
81
81
  BlogPost.new('TextRecord').directory.should eql(File.join(TextRecord.configuration.content_path, "blog_posts","TextRecord"))
82
82
  end
83
83
  end
84
+
85
+ describe "#eql?" do
86
+ it "should return true if the slugs are the same" do
87
+ blog_post1 = BlogPost.new('TextRecord')
88
+ blog_post2 = BlogPost.new('TextRecord')
89
+ blog_post1.eql?(blog_post2).should be_true
90
+ end
91
+
92
+ it "should return false if the slugs are different" do
93
+ blog_post1 = BlogPost.new('TextRecord')
94
+ blog_post2 = BlogPost.new('ActiveRecord')
95
+ blog_post1.eql?(blog_post2).should be_false
96
+ end
97
+ end
84
98
  end
@@ -0,0 +1,65 @@
1
+ require 'spec_helper'
2
+
3
+ describe TextRecord::Collection do
4
+ before(:each) do
5
+ @story1 = Story.new('Story1')
6
+ @story2 = Story.new('Story2')
7
+ @story3 = Story.new('Story3')
8
+ end
9
+
10
+ describe "#where" do
11
+ it "should find all the records matching :attribute => :value" do
12
+ found = Story.where :title => 'Story1'
13
+ found.should eql([@story1])
14
+ end
15
+
16
+ it "should be able to except multiple key/value pairs" do
17
+ found = Story.where :title => 'Story1', :id => 5
18
+ found.should eql([])
19
+ end
20
+
21
+ it "should return a new collection" do
22
+ all = Story.all
23
+ where = Story.all.where :id => 1
24
+
25
+ where.object_id.should_not eql(all.object_id)
26
+ end
27
+ end
28
+
29
+ describe "#order" do
30
+ it "should order the collection by the id asc using a fixnum" do
31
+ stories = Story.all.order :id => :asc
32
+
33
+ test = [@story1, @story2, @story3]
34
+ stories.should eql(test)
35
+ end
36
+
37
+ it "should order the collection by the id asc using a fixnum" do
38
+ stories = Story.all.order :id => :desc
39
+
40
+ test = [@story3, @story2, @story1]
41
+ stories.should eql(test)
42
+ end
43
+
44
+ it "should order the collection by posted_on asc using a date" do
45
+ stories = Story.all.order :posted_on => :asc
46
+
47
+ test = [@story1, @story2, @story3]
48
+ stories.should eql(test)
49
+ end
50
+
51
+ it "should order the collection by posted_on desc using a date" do
52
+ stories = Story.all.order :posted_on => :desc
53
+
54
+ test = [@story3, @story2, @story1]
55
+ stories.should eql(test)
56
+ end
57
+
58
+ it "should return a new collection" do
59
+ stories = Story.all
60
+ ordered = Story.all.order :posted_on => :desc
61
+
62
+ stories.object_id.should_not eql(ordered.object_id)
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,67 @@
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{text_record}
8
+ s.version = "0.0.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Adam Hawkins"]
12
+ s.date = %q{2010-02-05}
13
+ s.description = %q{Write your models in text files through YAML/Makdown/TextFile whatever you please. Take the forms out of your code and harness the power of your favorite editor.}
14
+ s.email = %q{Adman1965@gmaiil.com}
15
+ s.files = [
16
+ ".gitignore",
17
+ "Rakefile",
18
+ "VERSION",
19
+ "lib/text_record.rb",
20
+ "lib/text_record/attribute.rb",
21
+ "lib/text_record/base.rb",
22
+ "lib/text_record/collection.rb",
23
+ "lib/text_record/configuration.rb",
24
+ "lib/text_record/errors.rb",
25
+ "readme.markdown",
26
+ "spec/app/content/blog_posts/ActiveRecord/attributes.yml",
27
+ "spec/app/content/blog_posts/ActiveRecord/post.markdown",
28
+ "spec/app/content/blog_posts/TextRecord/attributes.yml",
29
+ "spec/app/content/blog_posts/TextRecord/post.markdown",
30
+ "spec/app/content/stories/Story1/attributes.yml",
31
+ "spec/app/content/stories/Story2/attributes.yml",
32
+ "spec/app/content/stories/Story3/attributes.yml",
33
+ "spec/app/models/blog_post.rb",
34
+ "spec/app/models/story.rb",
35
+ "spec/base_spec.rb",
36
+ "spec/blog_post_spec.rb",
37
+ "spec/collection_spec.rb",
38
+ "spec/configuration_spec.rb",
39
+ "spec/spec_helper.rb",
40
+ "text_record.gemspec"
41
+ ]
42
+ s.homepage = %q{http://github.com/Adman65/text_record}
43
+ s.rdoc_options = ["--charset=UTF-8"]
44
+ s.require_paths = ["lib"]
45
+ s.rubygems_version = %q{1.3.5}
46
+ s.summary = %q{Models through text files}
47
+ s.test_files = [
48
+ "spec/app/models/blog_post.rb",
49
+ "spec/app/models/story.rb",
50
+ "spec/base_spec.rb",
51
+ "spec/blog_post_spec.rb",
52
+ "spec/collection_spec.rb",
53
+ "spec/configuration_spec.rb",
54
+ "spec/spec_helper.rb"
55
+ ]
56
+
57
+ if s.respond_to? :specification_version then
58
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
59
+ s.specification_version = 3
60
+
61
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
62
+ else
63
+ end
64
+ else
65
+ end
66
+ end
67
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: text_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Hawkins
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-04 00:00:00 -08:00
12
+ date: 2010-02-05 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -28,16 +28,25 @@ files:
28
28
  - lib/text_record.rb
29
29
  - lib/text_record/attribute.rb
30
30
  - lib/text_record/base.rb
31
+ - lib/text_record/collection.rb
31
32
  - lib/text_record/configuration.rb
32
33
  - lib/text_record/errors.rb
33
34
  - readme.markdown
35
+ - spec/app/content/blog_posts/ActiveRecord/attributes.yml
36
+ - spec/app/content/blog_posts/ActiveRecord/post.markdown
34
37
  - spec/app/content/blog_posts/TextRecord/attributes.yml
35
38
  - spec/app/content/blog_posts/TextRecord/post.markdown
39
+ - spec/app/content/stories/Story1/attributes.yml
40
+ - spec/app/content/stories/Story2/attributes.yml
41
+ - spec/app/content/stories/Story3/attributes.yml
36
42
  - spec/app/models/blog_post.rb
43
+ - spec/app/models/story.rb
37
44
  - spec/base_spec.rb
38
45
  - spec/blog_post_spec.rb
46
+ - spec/collection_spec.rb
39
47
  - spec/configuration_spec.rb
40
48
  - spec/spec_helper.rb
49
+ - text_record.gemspec
41
50
  has_rdoc: true
42
51
  homepage: http://github.com/Adman65/text_record
43
52
  licenses: []
@@ -68,7 +77,9 @@ specification_version: 3
68
77
  summary: Models through text files
69
78
  test_files:
70
79
  - spec/app/models/blog_post.rb
80
+ - spec/app/models/story.rb
71
81
  - spec/base_spec.rb
72
82
  - spec/blog_post_spec.rb
83
+ - spec/collection_spec.rb
73
84
  - spec/configuration_spec.rb
74
85
  - spec/spec_helper.rb