did_active_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/README ADDED
@@ -0,0 +1,4 @@
1
+ ActiveModel
2
+ ===========
3
+
4
+ Provides validation and callbacks outside of the ActiveRecord::Base hierarchy.
@@ -0,0 +1,137 @@
1
+ class ActiveModel
2
+
3
+ cattr_accessor :logger
4
+
5
+ class << self
6
+
7
+ def attribute_names
8
+ read_inheritable_attribute(:attribute_names) || []
9
+ end
10
+
11
+ def instantiate
12
+ invalid_method
13
+ end
14
+
15
+ def model_attr(*attribute_names)
16
+ attribute_names.collect!{|a| a.to_s}
17
+ write_inheritable_array(:attribute_names, attribute_names)
18
+ attribute_names.each do |a|
19
+ attr_accessor a
20
+ alias_method "#{a}_before_type_cast", a
21
+ end
22
+ end
23
+
24
+ def human_attribute_name(attribute_key_name)
25
+ attribute_key_name.humanize
26
+ end
27
+
28
+ def human_name
29
+ defaults = self_and_descendants_from_active_record.map do |klass|
30
+ :"#{klass.name.underscore}"
31
+ end
32
+ defaults << self.name.humanize
33
+ end
34
+
35
+ def base_class
36
+ self
37
+ end
38
+
39
+ def self_and_descendants_from_active_record
40
+ klass = self
41
+ classes = [klass]
42
+ while klass != klass.base_class
43
+ classes << klass = klass.superclass
44
+ end
45
+ classes
46
+ end
47
+
48
+ end
49
+
50
+ def initialize(attributes={})
51
+ self.attributes = attributes if attributes
52
+ end
53
+
54
+ def attribute_names
55
+ self.class.attribute_names
56
+ end
57
+
58
+ def attributes
59
+ self.attribute_names.inject({}) do |attrs, name|
60
+ attrs[name] = send(name)
61
+ attrs
62
+ end
63
+ end
64
+
65
+ def attributes=(attributes)
66
+ return unless attributes
67
+ attributes.stringify_keys!
68
+ attributes.each do |k, v|
69
+ send("#{k}=", v)
70
+ end
71
+ end
72
+
73
+ def method_missing(method_id, *args, &block)
74
+ method_name = method_id.to_s
75
+ if md = /(\?)$/.match(method_name)
76
+ query_attribute(md.pre_match)
77
+ else
78
+ super
79
+ end
80
+ end
81
+
82
+ def save
83
+ create_or_update
84
+ end
85
+ alias save! save
86
+
87
+ def create_or_update; end
88
+
89
+ def [](attribute)
90
+ instance_variable_get("@#{attribute}")
91
+ end
92
+
93
+ def []=(attribute, value)
94
+ instance_variable_set("@#{attribute}", value)
95
+ end
96
+
97
+ def query_attribute(attr_name)
98
+ attribute = self[attr_name]
99
+ if attribute.kind_of?(Fixnum) && attribute == 0
100
+ false
101
+ elsif attribute.kind_of?(String) && attribute == "0"
102
+ false
103
+ elsif attribute.kind_of?(String) && attribute.empty?
104
+ false
105
+ elsif attribute.nil?
106
+ false
107
+ elsif attribute == false
108
+ false
109
+ elsif attribute == "f"
110
+ false
111
+ elsif attribute == "false"
112
+ false
113
+ else
114
+ true
115
+ end
116
+ end
117
+
118
+ alias respond_to_without_attributes? respond_to?
119
+
120
+ def self.invalid_method
121
+ raise "Invalid method for ActiveModel"
122
+ end
123
+ def invalid_method; self.class.invalid_method; end
124
+
125
+ alias create invalid_method
126
+ alias update invalid_method
127
+ alias destroy invalid_method
128
+ alias update_attribute invalid_method
129
+
130
+ def new_record?
131
+ false
132
+ end
133
+
134
+ include ActiveRecord::Validations
135
+ include ActiveRecord::Callbacks
136
+
137
+ end
@@ -0,0 +1,33 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
+
3
+ class User < ActiveModel
4
+ model_attr :name
5
+ validates_presence_of :name
6
+ end
7
+
8
+ class ActiveModelTest < Test::Unit::TestCase
9
+ def test_validates_presence_of_name
10
+ user = User.new
11
+ assert !user.valid?
12
+ assert_equal "can't be blank", user.errors.on(:name)
13
+ end
14
+
15
+ def test_initialize_attributes
16
+ user = User.new(:name => "John Doe")
17
+ assert_equal "John Doe", user.name
18
+ end
19
+
20
+ def test_attribute_names
21
+ user = User.new
22
+ assert_equal ["name"], user.attribute_names
23
+ end
24
+
25
+ def test_attributes
26
+ user = User.new(:name => "John Doe")
27
+ assert_equal({"name" => "John Doe"}, user.attributes)
28
+ end
29
+
30
+ def test_new_record?
31
+ assert !User.new.new_record?
32
+ end
33
+ end
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_record'
4
+ require 'active_record/base'
5
+
6
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/active_model')
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: did_active_model
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Nathaniel Talbott
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-14 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Provides Rails model services (such as validations) to non-ActiveRecord descendants.
23
+ email: didier@nocoffee.fr
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - README
30
+ files:
31
+ - lib/active_model.rb
32
+ - README
33
+ - test/active_model_test.rb
34
+ - test/test_helper.rb
35
+ has_rdoc: true
36
+ homepage: https://github.com/ntalbott/active_model
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options:
41
+ - --charset=UTF-8
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ hash: 3
50
+ segments:
51
+ - 0
52
+ version: "0"
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.4.2
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Old active model interface
69
+ test_files:
70
+ - test/active_model_test.rb
71
+ - test/test_helper.rb