curly_mustache 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,31 @@
1
+ class Account < CurlyMustache::Base
2
+ attribute :name, :string, :allow_nil => false
3
+ attribute :created_at, :time
4
+ attribute :updated_at, :time
5
+
6
+ CALLBACKS = %w[before_create after_create
7
+ before_save after_save
8
+ before_update after_update
9
+ before_destroy after_destroy
10
+ before_validation after_validation
11
+ before_validation_on_create after_validation_on_create
12
+ before_validation_on_update after_validation_on_update
13
+ after_find]
14
+
15
+ attr_accessor :callbacks_made
16
+
17
+ # This just defines methods for each callback that simply records in #callbacks_made that
18
+ # the callback was called.
19
+ CALLBACKS.each do |callback_name|
20
+ class_eval <<-end_eval
21
+ #{callback_name} :#{callback_name}_cb
22
+ def #{callback_name}_cb
23
+ @callbacks_made ||= []
24
+ @callbacks_made << :#{callback_name}
25
+ end
26
+ end_eval
27
+ end
28
+
29
+ validates_presence_of :name
30
+
31
+ end
@@ -0,0 +1,13 @@
1
+ class Feed < CurlyMustache::Base
2
+ attribute :title, :string
3
+ attribute :url, :string
4
+
5
+ def send_attributes(attributes)
6
+ { "BLOB" => Marshal.dump(attributes) }
7
+ end
8
+
9
+ def recv_attributes(attributes)
10
+ Marshal.load(attributes["BLOB"])
11
+ end
12
+
13
+ end
@@ -0,0 +1,4 @@
1
+ class Page < CurlyMustache::Base
2
+ attribute :url, :string
3
+ attribute :html, :string
4
+ end
@@ -0,0 +1,10 @@
1
+ class User < CurlyMustache::Base
2
+ allow_settable_id!
3
+ attribute :id, :integer
4
+ attribute :name, :string
5
+ attribute :phone_number, :integer
6
+ attribute :balance, :float
7
+ attribute :is_admin, :boolean
8
+ attribute :created_at, :time
9
+ attribute :updated_at, :time
10
+ end
@@ -0,0 +1,22 @@
1
+ require "test_helper"
2
+
3
+ class SerializationTest < ActiveSupport::TestCase
4
+
5
+ def test_basic
6
+ feed = Feed.create(:title => "blah", :url => "http://www.blah.com")
7
+ feed = Feed.find(feed.id)
8
+ assert_equal "blah", feed.title
9
+ assert_equal "http://www.blah.com", feed.url
10
+ end
11
+
12
+ def test_proper_connection
13
+ feed = Feed.create(:title => "blah", :url => "http://www.blah.com")
14
+ assert_equal %w[BLOB],
15
+ Feed.connection.get(Feed.send(:id_to_key, feed.id)).keys
16
+
17
+ account = Account.create(:name => "blah", :created_at => Time.now, :updated_at => Time.now)
18
+ assert_equal %w[id name created_at updated_at].sort,
19
+ Account.connection.get(Account.send(:id_to_key, account.id)).keys.sort
20
+ end
21
+
22
+ end
@@ -0,0 +1,35 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'mocha'
4
+
5
+ require 'curly_mustache'
6
+
7
+ # This has to go before the model definitions or class_inheritable_accessor won't inherit.
8
+ adapter = ENV["ADAPTER"] || ENV["adapter"] || "memcached"
9
+ configs = YAML.load(File.open(File.dirname(__FILE__)+"/adapters.yml"){ |f| f.read })
10
+ CurlyMustache::Base.establish_connection(configs[adapter].merge(:adapter => adapter))
11
+
12
+ # This also has to go before the model definitions or class_inheritable_accessor won't inherit.
13
+ class CurlyMustache::Base
14
+ if connection.adapter_name == :cassandra
15
+ def send_attributes(attributes)
16
+ attributes.inject({}) do |memo, (k, v)|
17
+ memo[k] = String(v)
18
+ memo
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ # Load model definitions.
25
+ Dir.glob(File.dirname(__FILE__) + "/models/*").each{ |file_name| require file_name }
26
+
27
+ class ActiveSupport::TestCase
28
+
29
+ def disable_tests
30
+ methods.select{ |method| method =~ /^test_/ }.each do |method|
31
+ self.class.send(:define_method, method){ nil }
32
+ end
33
+ end
34
+
35
+ end
@@ -0,0 +1,13 @@
1
+ require "test_helper"
2
+
3
+ class TypesTest < ActiveSupport::TestCase
4
+
5
+ def test_clear
6
+ assert !CurlyMustache::Attributes::Types.definitions.blank?
7
+ CurlyMustache::Attributes::Types.clear
8
+ assert CurlyMustache::Attributes::Types.definitions.blank?
9
+ load File.dirname(__FILE__) + "/../lib/curly_mustache/default_types.rb"
10
+ assert !CurlyMustache::Attributes::Types.definitions.blank?
11
+ end
12
+
13
+ end
@@ -0,0 +1,65 @@
1
+ require "test_helper"
2
+
3
+ class ValidationsTest < ActiveSupport::TestCase
4
+
5
+ def test_basic
6
+ account = Account.new
7
+ assert !account.valid?
8
+ assert_equal 1, account.errors.length
9
+ assert_equal ["can't be blank"], account.errors[:name]
10
+ end
11
+
12
+ def test_create
13
+ account = Account.create
14
+ assert account.new_record?
15
+ assert_equal({}, account.attributes)
16
+ assert_equal [:before_validation_on_create, :before_validation], account.callbacks_made
17
+ end
18
+
19
+ def test_create!
20
+ begin
21
+ Account.create!
22
+ assert false
23
+ rescue CurlyMustache::RecordInvalid => e
24
+ assert e.message.index("Validation failed:") == 0
25
+ end
26
+ end
27
+
28
+ def test_save
29
+ account = Account.new
30
+ assert_equal false, account.save
31
+ assert_equal({}, account.attributes)
32
+ assert_equal [:before_validation_on_create, :before_validation], account.callbacks_made
33
+
34
+ # Now make sure the callback changed to on_update.
35
+ account = Account.create!(:name => "blah")
36
+ account.callbacks_made = []
37
+ account.name = nil
38
+ assert_equal false, account.save
39
+ assert_equal [:before_validation_on_update, :before_validation], account.callbacks_made
40
+ end
41
+
42
+ def test_save!
43
+ begin
44
+ account = Account.new
45
+ account.save!
46
+ assert false
47
+ rescue CurlyMustache::RecordInvalid => e
48
+ assert e.message.index("Validation failed:") == 0
49
+ assert_equal [:before_validation_on_create, :before_validation], account.callbacks_made
50
+ end
51
+
52
+ # Now make sure the callback changed to on_update.
53
+ account = Account.create!(:name => "blah")
54
+ account.callbacks_made = []
55
+ begin
56
+ account.name = nil
57
+ account.save!
58
+ assert false
59
+ rescue CurlyMustache::RecordInvalid => e
60
+ assert e.message.index("Validation failed:") == 0
61
+ assert_equal [:before_validation_on_update, :before_validation], account.callbacks_made
62
+ end
63
+ end
64
+
65
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: curly_mustache
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Christopher J Bottaro
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-02-24 00:00:00 -06:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description:
22
+ email: cjbottaro@alumni.cs.utexas.edu
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - LICENSE
29
+ - README.rdoc
30
+ - TODO
31
+ files:
32
+ - .document
33
+ - .gitignore
34
+ - LICENSE
35
+ - README.rdoc
36
+ - Rakefile
37
+ - TODO
38
+ - VERSION.yml
39
+ - curly_mustache.gemspec
40
+ - lib/curly_mustache.rb
41
+ - lib/curly_mustache/adapters.rb
42
+ - lib/curly_mustache/adapters/abstract.rb
43
+ - lib/curly_mustache/adapters/cassandra.rb
44
+ - lib/curly_mustache/adapters/memcached.rb
45
+ - lib/curly_mustache/attributes.rb
46
+ - lib/curly_mustache/attributes/definition.rb
47
+ - lib/curly_mustache/attributes/manager.rb
48
+ - lib/curly_mustache/attributes/types.rb
49
+ - lib/curly_mustache/base.rb
50
+ - lib/curly_mustache/connection.rb
51
+ - lib/curly_mustache/crud.rb
52
+ - lib/curly_mustache/default_types.rb
53
+ - lib/curly_mustache/errors.rb
54
+ - lib/curly_mustache/locking.rb
55
+ - test/abstract_adapter_test.rb
56
+ - test/adapters.yml
57
+ - test/attributes_test.rb
58
+ - test/callbacks_test.rb
59
+ - test/crud_test.rb
60
+ - test/locking_test.rb
61
+ - test/models/account.rb
62
+ - test/models/feed.rb
63
+ - test/models/page.rb
64
+ - test/models/user.rb
65
+ - test/serialization_test.rb
66
+ - test/test_helper.rb
67
+ - test/types_test.rb
68
+ - test/validations_test.rb
69
+ has_rdoc: true
70
+ homepage: http://github.com/cjbottaro/curly_mustache
71
+ licenses: []
72
+
73
+ post_install_message:
74
+ rdoc_options:
75
+ - --charset=UTF-8
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ segments:
90
+ - 0
91
+ version: "0"
92
+ requirements: []
93
+
94
+ rubyforge_project:
95
+ rubygems_version: 1.3.6
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: ActiveRecord-like interface to various data stores, using ActiveModel.
99
+ test_files:
100
+ - test/abstract_adapter_test.rb
101
+ - test/attributes_test.rb
102
+ - test/callbacks_test.rb
103
+ - test/crud_test.rb
104
+ - test/locking_test.rb
105
+ - test/models/account.rb
106
+ - test/models/feed.rb
107
+ - test/models/page.rb
108
+ - test/models/user.rb
109
+ - test/serialization_test.rb
110
+ - test/test_helper.rb
111
+ - test/types_test.rb
112
+ - test/validations_test.rb