Skip to content
This repository
Newer
Older
100644 205 lines (146 sloc) 5.716 kb
ffb3bb09 »
2011-09-30 Configure Travis builds, closes #11
1 # ActiveAttr #
2
f7305f9f »
2012-02-10 Add dependency status to README
3 [![Build History][2]][1] [![Dependency Status][5]][4]
ffb3bb09 »
2011-09-30 Configure Travis builds, closes #11
4
4826d2bb »
2011-12-13 Fill in some of the missing README sections, closes #47 [ci skip]
5 ActiveAttr is a set of modules that makes it easy to create plain old ruby
6 models with functionality found in ORMs, like ActiveRecord, without
7 reinventing the wheel. Think of ActiveAttr as the stuff ActiveModel left out.
ffb3bb09 »
2011-09-30 Configure Travis builds, closes #11
8
4826d2bb »
2011-12-13 Fill in some of the missing README sections, closes #47 [ci skip]
9 ActiveAttr is distributed as a rubygem [on rubygems.org][3].
10
11 * [API Documentation](http://rubydoc.info/gems/active_attr)
12 * [Contributors](https://github.com/cgriego/active_attr/contributors)
63bbd972 »
2011-09-30 Configure yardoc [ci skip] #17
13
ffb3bb09 »
2011-09-30 Configure Travis builds, closes #11
14 [1]: http://travis-ci.org/cgriego/active_attr
15 [2]: https://secure.travis-ci.org/cgriego/active_attr.png?branch=master
4826d2bb »
2011-12-13 Fill in some of the missing README sections, closes #47 [ci skip]
16 [3]: http://rubygems.org/gems/active_attr
f7305f9f »
2012-02-10 Add dependency status to README
17 [4]: https://gemnasium.com/cgriego/active_attr
18 [5]: https://gemnasium.com/cgriego/active_attr.png
a3e4cd3d »
2011-10-03 Words [ci skip]
19
69d9d8df »
2011-10-03 README Words [ci skip]
20 ## Modules ##
21
22 ### Attributes ###
a3e4cd3d »
2011-10-03 Words [ci skip]
23
24 Including the Attributes module into your class gives you a DSL for defining
25 the attributes of your model.
26
27 class Person
28 include ActiveAttr::Attributes
29
30 attribute :first_name
31 attribute :last_name
32 end
33
5e04485c »
2011-11-02 Added ActiveAttr::BlockInitializations, closes #33
34 person = Person.new
35 person.first_name = "Chris"
36 person.last_name = "Griego"
37 person.attributes #=> {"first_name"=>"Chris", "last_name"=>"Griego"}
a3e4cd3d »
2011-10-03 Words [ci skip]
38
a4af6f03 »
2011-12-30 Added AttributeDefaults, closes #5
39 #### AttributeDefaults ####
40
41 Including the AttributeDefaults module into your class builds on Attributes by
42 allowing defaults to be declared with attributes.
43
44 class Person
45 include ActiveAttr::AttributeDefaults
46
47 attribute :first_name, :default => "John"
48 attribute :last_name, :default => "Doe"
49 end
50
51 person = Person.new
52 person.first_name #=> "John"
53 person.last_name #=> "Doe"
54
1447df9e »
2011-12-13 Rearrange modules in README [ci skip]
55 #### QueryAttributes ####
56
57 Including the QueryAttributes module into your class builds on Attributes by
a4af6f03 »
2011-12-30 Added AttributeDefaults, closes #5
58 providing instance methods for querying your attributes.
1447df9e »
2011-12-13 Rearrange modules in README [ci skip]
59
60 class Person
61 include ActiveAttr::QueryAttributes
62
63 attribute :first_name
64 attribute :last_name
65 end
66
67 person = Person.new
68 person.first_name = "Chris"
69 person.first_name? #=> true
70 person.last_name? #=> false
71
72 #### TypecastedAttributes ####
73
8f6665ec »
2012-03-11 Finish documenting Typecasting, closes #77 [ci skip]
74 Including the TypecastedAttributes module into your class builds on Attributes
75 by providing type conversion for your attributes.
1447df9e »
2011-12-13 Rearrange modules in README [ci skip]
76
a4af6f03 »
2011-12-30 Added AttributeDefaults, closes #5
77 class Person
78 include ActiveAttr::TypecastedAttributes
79 attribute :age, :type => Integer
80 end
1447df9e »
2011-12-13 Rearrange modules in README [ci skip]
81
a4af6f03 »
2011-12-30 Added AttributeDefaults, closes #5
82 person = Person.new
83 person.age = "29"
84 person.age #=> 29
1447df9e »
2011-12-13 Rearrange modules in README [ci skip]
85
69d9d8df »
2011-10-03 README Words [ci skip]
86 ### BasicModel ###
a3e4cd3d »
2011-10-03 Words [ci skip]
87
88 Including the BasicModel module into your class gives you the bare minimum
89 required for your model to meet the ActiveModel API requirements.
90
91 class Person
92 include ActiveAttr::BasicModel
93 end
94
95 Person.model_name.plural #=> "people"
5e04485c »
2011-11-02 Added ActiveAttr::BlockInitializations, closes #33
96 person = Person.new
97 person.valid? #=> true
98 person.errors.full_messages #=> []
99
100 ### BlockInitialization ###
101
102 Including the BlockInitialization module into your class will yield the model
103 instance to a block passed to when creating a new instance.
104
105 class Person
106 include ActiveAttr::BlockInitialization
107 attr_accessor :first_name, :last_name
108 end
109
110 person = Person.new do |p|
111 p.first_name = "Chris"
112 p.last_name = "Griego"
113 end
114
115 person.first_name #=> "Chris"
116 person.last_name #=> "Griego"
a3e4cd3d »
2011-10-03 Words [ci skip]
117
20bca016 »
2011-11-24 Added Logger, closes #39
118 ### Logger ###
119
120 Including the Logger module into your class will give you access to a
121 configurable logger in model classes and instances. Your preferred logger can
122 be configured on an instance, subclass, class, parent class, and globally by
123 setting ActiveAttr::Logger.logger. When using Rails, the Rails framework
124 logger will be configured by default.
125
126 class Person
127 include ActiveAttr::Logger
128 end
129
130 Person.logger = Logger.new(STDOUT)
131 Person.logger? #=> true
132 Person.logger.info "Logging an informational message"
133
134 person = Person.new
135 person.logger? #=> true
136 person.logger = Logger.new(STDERR)
137 person.logger.warn "Logging a warning message"
138
69d9d8df »
2011-10-03 README Words [ci skip]
139 ### MassAssignment ###
a3e4cd3d »
2011-10-03 Words [ci skip]
140
141 Including the MassAssignment module into your class gives you methods for bulk
142 initializing and updating the attributes of your model. Any unknown attributes
ee392573 »
2011-12-14 Remove StrictMassAssignment in favor of ActiveModel 3.2, closes #40
143 are silently ignored.
a3e4cd3d »
2011-10-03 Words [ci skip]
144
145 class Person
146 include ActiveAttr::MassAssignment
69d9d8df »
2011-10-03 README Words [ci skip]
147 attr_accessor :first_name, :last_name
a3e4cd3d »
2011-10-03 Words [ci skip]
148 end
149
5e04485c »
2011-11-02 Added ActiveAttr::BlockInitializations, closes #33
150 person = Person.new(:first_name => "Chris")
151 person.attributes = { :last_name => "Griego" }
152 person.first_name #=> "Chris"
153 person.last_name #=> "Griego"
69d9d8df »
2011-10-03 README Words [ci skip]
154
1447df9e »
2011-12-13 Rearrange modules in README [ci skip]
155 #### MassAssignmentSecurity ####
55a67b1a »
2011-11-24 Added MassAssignmentSecurity, closes #22
156
157 Including the MassAssignmentSecurity module into your class extends the
158 MassAssignment methods to honor any declared mass assignment permission
159 blacklists or whitelists including support for mass assignment roles.
160
161 class Person
73682be2 »
2012-02-03 Fix small typo in MassAssignmentSecurity documentation
162 include ActiveAttr::MassAssignmentSecurity
55a67b1a »
2011-11-24 Added MassAssignmentSecurity, closes #22
163 attr_accessor :first_name, :last_name
164 attr_protected :last_name
165 end
166
167 person = Person.new(:first_name => "Chris", :last_name => "Griego")
168 person.first_name #=> "Chris"
169 person.last_name #=> nil
170
8f6665ec »
2012-03-11 Finish documenting Typecasting, closes #77 [ci skip]
171 ### Model ###
172
173 The Model module is a shortcut for incorporating the most common model
174 functionality into your model with one include. All of the above modules
175 are included when you include Model.
176
177 class Person
178 include ActiveAttr::Model
179 end
180
7a375b58 »
2011-11-26 Words [ci skip]
181 ## Integrations ##
182
183 ### Ruby on Rails ###
184
185 When using ActiveAttr inside a Rails application, ActiveAttr will configure
186 your models' default logger to use the Rails logger automatically. Just
187 include ActiveAttr in your Gemfile.
188
189 gem "active_attr"
190
191 ### RSpec ###
69d9d8df »
2011-10-03 README Words [ci skip]
192
193 ActiveAttr comes with matchers and RSpec integration to assist you in testing
7a375b58 »
2011-11-26 Words [ci skip]
194 your models. The matchers also work with compatible frameworks like Shoulda.
69d9d8df »
2011-10-03 README Words [ci skip]
195
196 require "active_attr/rspec"
197
198 describe Person do
23d2a46c »
2012-03-11 Added Matchers::HaveAttributeMatcher#of_type, closes #51
199 it do
200 should have_attribute(:first_name).
201 of_type(String).
202 with_default_value_of("John")
203 end
69d9d8df »
2011-10-03 README Words [ci skip]
204 end
Something went wrong with that request. Please try again.