ascribe 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +92 -4
- data/ascribe.gemspec +1 -1
- data/lib/ascribe/attributes.rb +1 -1
- data/lib/ascribe/version.rb +1 -1
- metadata +8 -8
data/README.md
CHANGED
@@ -1,9 +1,97 @@
|
|
1
|
+
# Ascribe
|
2
|
+
|
3
|
+
Simple attributes for your Ruby objects.
|
4
|
+
|
5
|
+
## Why?
|
6
|
+
|
7
|
+
It seems there comes a point during all of my projects where I end up hacking up some sort of attribute and validation system into my models. I decided it was finally time I extracted my hacks into a library that provided a consistent way to add attributes to my classes that offered more than simple accessor methods. Models aren't always tied to an ORM that does this magic for you :)
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
```bash
|
12
|
+
gem install ascribe
|
13
|
+
```
|
14
|
+
|
15
|
+
If you're using Bundler, make sure you add it to your Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'ascribe', '>= 0.0.2'
|
19
|
+
```
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
To use Ascribe in a model, just make your class look like this:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require 'ascribe'
|
27
|
+
|
1
28
|
class Foo
|
2
29
|
include Ascribe::Attributes
|
3
30
|
|
4
|
-
|
5
|
-
|
31
|
+
end
|
32
|
+
```
|
33
|
+
### Declaring Attributes
|
34
|
+
|
35
|
+
Use the `attribute` class method to define attributes on your model. The sole requirements are the name and type of the attribute.
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
class User
|
39
|
+
include Ascribe::Attributes
|
40
|
+
|
41
|
+
attribute :name, String
|
42
|
+
attribute :email, String
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
### Setting types
|
47
|
+
|
48
|
+
The type is the second argument of the `attribute` class method. Type should always be a class name, as Ascribe checks that the assigned value is an instance of the class. Type can be a single class name, or an array of classes
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
class Foo
|
52
|
+
attribute :bar, String
|
53
|
+
attribute :baz, [String, Symbol]
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
### Specifying defaults
|
58
|
+
|
59
|
+
Defaults can be set via the :default key for an attribute. Defaults can either be a standard values (strings, arrays, hashes, etc), or they can be anything that responds to `#call`, like Procs.
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
class Pants
|
63
|
+
attribute :on, [TrueClass, FalseClass], :default => false
|
64
|
+
end
|
65
|
+
|
66
|
+
pants = Pants.new
|
67
|
+
pants.on #=> false
|
68
|
+
```
|
69
|
+
|
70
|
+
|
71
|
+
### Validation options
|
72
|
+
|
73
|
+
Ascribe can validate attributes in a number of ways
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
class Post
|
77
|
+
attribute :title, String, :required => true # presence
|
78
|
+
attribute :body, String, :length => { :min => 0, :max => 1000 } # length
|
79
|
+
attribute :hits, Integer, :numeric => true # numericality
|
80
|
+
attribute :email, String, :format => /\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/ # format
|
81
|
+
attribute :tags, Array, :in => ["foo", "bar"], :not_in => ["baz", "qux"] # inclusion/exclusion
|
82
|
+
end
|
83
|
+
```
|
84
|
+
|
85
|
+
### Bonus options
|
86
|
+
|
87
|
+
If any attributes not explicitly stated are included in the hash used to instantiate your object, they won't throw an error or be discarded; instead, they get assigned to the @options instance variable (handy if you need arbitrary attributes sometimes):
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
class Foo
|
91
|
+
attribute :bar, String
|
6
92
|
end
|
7
93
|
|
8
|
-
foo = Foo.new(:
|
9
|
-
foo.
|
94
|
+
foo = Foo.new(:bar => "asdf", :baz => "qwer", :qux => "zxcv")
|
95
|
+
foo.attributes #=> {"bar" => "asdf"}
|
96
|
+
foo.options #=> {"baz"=>"qwer", "qux"=>"zxcv"}
|
97
|
+
```
|
data/ascribe.gemspec
CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
19
|
s.require_paths = ["lib"]
|
20
20
|
|
21
|
-
s.add_dependency 'activemodel', '3.0.10'
|
21
|
+
s.add_dependency 'activemodel', '>= 3.0.10'
|
22
22
|
s.add_dependency 'yajl-ruby', '>= 0.8.2'
|
23
23
|
s.add_dependency 'rake', '>= 0.9.2'
|
24
24
|
# specify any dependencies here; for example:
|
data/lib/ascribe/attributes.rb
CHANGED
data/lib/ascribe/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ascribe
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,18 +13,18 @@ date: 2011-09-11 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activemodel
|
16
|
-
requirement: &
|
16
|
+
requirement: &70149126918840 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
|
-
- -
|
19
|
+
- - ! '>='
|
20
20
|
- !ruby/object:Gem::Version
|
21
21
|
version: 3.0.10
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70149126918840
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: yajl-ruby
|
27
|
-
requirement: &
|
27
|
+
requirement: &70149126918340 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 0.8.2
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70149126918340
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rake
|
38
|
-
requirement: &
|
38
|
+
requirement: &70149136261340 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: 0.9.2
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70149136261340
|
47
47
|
description: ''
|
48
48
|
email:
|
49
49
|
- dan@appliedawesome.com
|