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 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
- attribute :name, String, :required => true, :default => "NAME"
5
- attribute :stuff, String, :required => true
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(:name => "bar", :type => "baz")
9
- foo.name
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:
@@ -111,7 +111,7 @@ module Ascribe
111
111
  val =
112
112
  write_attribute(key, value)
113
113
  else
114
- options[key] = value
114
+ options[key.to_s] = value
115
115
  end
116
116
  end
117
117
 
@@ -1,3 +1,3 @@
1
1
  module Ascribe
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
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.1
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: &70195220082180 !ruby/object:Gem::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: *70195220082180
24
+ version_requirements: *70149126918840
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: yajl-ruby
27
- requirement: &70195220081680 !ruby/object:Gem::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: *70195220081680
35
+ version_requirements: *70149126918340
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &70195220081220 !ruby/object:Gem::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: *70195220081220
46
+ version_requirements: *70149136261340
47
47
  description: ''
48
48
  email:
49
49
  - dan@appliedawesome.com