ascribe 0.0.7 → 0.1.0

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.md CHANGED
@@ -1,97 +1 @@
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
-
28
- class Foo
29
- include Ascribe::Attributes
30
-
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
92
- end
93
-
94
- foo = Foo.new(:bar => "asdf", :baz => "qwer", :qux => "zxcv")
95
- foo.attributes #=> {"bar" => "asdf"}
96
- foo.options #=> {"baz"=>"qwer", "qux"=>"zxcv"}
97
- ```
1
+ ## No longer supported - please use [active_attr](https://github.com/cgriego/active_attr)
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
9
9
  s.email = ["dan@appliedawesome.com"]
10
10
  s.homepage = "https://github.com/danryan/ascribe"
11
11
  s.summary = %q{Attributes for your Ruby objects}
12
- s.description = %q{}
12
+ s.description = %q{No longer supported - please use active_attr (https://github.com/cgriego/active_attr)}
13
13
 
14
14
  s.rubyforge_project = "ascribe"
15
15
 
@@ -70,104 +70,102 @@ module Ascribe
70
70
 
71
71
  end
72
72
 
73
- module InstanceMethods
74
- def initialize(attrs={})
75
- self.class.attributes.each_pair do |key, attribute|
76
- (class << self; self; end).class_eval do
77
- define_method(attribute.name) do |*value, &block|
78
- if !block.nil?
79
- write_attribute(attribute.name, block)
80
- elsif !value.blank?
81
- write_attribute(attribute.name, value.first)
82
- else
83
- read_attribute(attribute.name)
84
- end
73
+ def initialize(attrs={})
74
+ self.class.attributes.each_pair do |key, attribute|
75
+ (class << self; self; end).class_eval do
76
+ define_method(attribute.name) do |*value, &block|
77
+ if !block.nil?
78
+ write_attribute(attribute.name, block)
79
+ elsif !value.blank?
80
+ write_attribute(attribute.name, value.first)
81
+ else
82
+ read_attribute(attribute.name)
85
83
  end
86
- define_method("#{attribute.name}=") do |*value, &block|
87
- if !block.nil?
88
- write_attribute(attribute.name, block)
89
- elsif !value.blank?
90
- write_attribute(attribute.name, value.first)
91
- end
92
- end
93
84
  end
85
+ define_method("#{attribute.name}=") do |*value, &block|
86
+ if !block.nil?
87
+ write_attribute(attribute.name, block)
88
+ elsif !value.blank?
89
+ write_attribute(attribute.name, value.first)
90
+ end
91
+ end
94
92
  end
95
- assign_attributes(attrs)
96
93
  end
97
-
98
- def options
99
- @options ||= {}
94
+ assign_attributes(attrs)
95
+ end
96
+
97
+ def options
98
+ @options ||= {}
99
+ end
100
+
101
+ def assign_attributes(attrs)
102
+ attribute_keys.each do |attr_key|
103
+ value = read_attribute(attr_key)
104
+ write_attribute(attr_key, value)
100
105
  end
101
-
102
- def assign_attributes(attrs)
103
- attribute_keys.each do |attr_key|
104
- value = read_attribute(attr_key)
105
- write_attribute(attr_key, value)
106
- end
107
106
 
108
- # Set all attributes supplied in the attrs hash
109
- attrs.each_pair do |key, value|
110
- if respond_to?(:"#{key}")
111
- val =
112
- write_attribute(key, value)
113
- else
114
- options[key.to_s] = value
115
- end
116
- end
117
- end
118
-
119
- def update(attrs={})
120
- assign_attributes(attrs)
121
- end
122
-
123
- def read_attribute(name)
124
- if attribute = self.class.attributes[name.to_s]
125
- value = attribute.get(instance_variable_get(:"@#{name}"))
126
- instance_variable_set(:"@#{name}", value)
107
+ # Set all attributes supplied in the attrs hash
108
+ attrs.each_pair do |key, value|
109
+ if respond_to?(:"#{key}")
110
+ val =
111
+ write_attribute(key, value)
112
+ else
113
+ options[key.to_s] = value
127
114
  end
128
115
  end
129
-
130
- def write_attribute(name, value)
131
- attribute = self.class.attributes[name.to_s]
132
- instance_variable_set(:"@#{name}", attribute.set(value))
116
+ end
117
+
118
+ def update(attrs={})
119
+ assign_attributes(attrs)
120
+ end
121
+
122
+ def read_attribute(name)
123
+ if attribute = self.class.attributes[name.to_s]
124
+ value = attribute.get(instance_variable_get(:"@#{name}"))
125
+ instance_variable_set(:"@#{name}", value)
133
126
  end
127
+ end
128
+
129
+ def write_attribute(name, value)
130
+ attribute = self.class.attributes[name.to_s]
131
+ instance_variable_set(:"@#{name}", attribute.set(value))
132
+ end
133
+
134
+ def attributes
135
+ attributes = {}
134
136
 
135
- def attributes
136
- attributes = {}
137
-
138
- self.class.attributes.each do |key, attribute|
139
- name = attribute.name
140
- attributes[name] = read_attribute(name) if respond_to?(name)
141
- end
142
- return attributes
137
+ self.class.attributes.each do |key, attribute|
138
+ name = attribute.name
139
+ attributes[name] = read_attribute(name) if respond_to?(name)
143
140
  end
141
+ return attributes
142
+ end
143
+
144
+ def attributes=(attrs={})
145
+ return if attrs.blank?
144
146
 
145
- def attributes=(attrs={})
146
- return if attrs.blank?
147
-
148
- attrs.each_pair do |key, value|
149
- if respond_to?(:"#{key}")
150
- write_attribute(key, value)
151
- end
147
+ attrs.each_pair do |key, value|
148
+ if respond_to?(:"#{key}")
149
+ write_attribute(key, value)
152
150
  end
153
151
  end
154
-
155
- def attribute_keys
156
- self.class.attributes.keys
157
- end
158
-
159
- def to_hash
160
- attributes.merge("options" => options)
161
- end
162
-
163
- def inspect
164
- attrs = attributes.map do |attribute|
165
- "@#{attribute[0]}=#{attribute[1] ? attribute[1] : "nil"}"
166
- end
167
- result = attrs + ["@options=#{options}"]
168
- "#<#{self.class.name} #{result.join(" ")}>"
152
+ end
153
+
154
+ def attribute_keys
155
+ self.class.attributes.keys
156
+ end
157
+
158
+ def to_hash
159
+ attributes.merge("options" => options)
160
+ end
161
+
162
+ def inspect
163
+ attrs = attributes.map do |attribute|
164
+ "@#{attribute[0]}=#{attribute[1] ? attribute[1] : "nil"}"
169
165
  end
170
-
166
+ result = attrs + ["@options=#{options}"]
167
+ "#<#{self.class.name} #{result.join(" ")}>"
171
168
  end
169
+
172
170
  end
173
171
  end
@@ -0,0 +1,43 @@
1
+ module Ascribe
2
+ module Extensions
3
+ module Boolean
4
+ Mapping = {
5
+ true => true,
6
+ 'true' => true,
7
+ 'TRUE' => true,
8
+ 'True' => true,
9
+ 't' => true,
10
+ 'T' => true,
11
+ '1' => true,
12
+ 1 => true,
13
+ 1.0 => true,
14
+ false => false,
15
+ 'false' => false,
16
+ 'FALSE' => false,
17
+ 'False' => false,
18
+ 'f' => false,
19
+ 'F' => false,
20
+ '0' => false,
21
+ 0 => false,
22
+ 0.0 => false,
23
+ nil => nil
24
+ }
25
+
26
+ def to_store(value, *)
27
+ if value.is_a?(Boolean)
28
+ value
29
+ else
30
+ Mapping[value]
31
+ end
32
+ end
33
+
34
+ def from_store(value, *)
35
+ value.nil? ? nil : !!value
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ class Boolean
42
+ extend Ascribe::Extensions::Boolean
43
+ end
@@ -1,3 +1,3 @@
1
1
  module Ascribe
2
- VERSION = "0.0.7"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,80 +1,71 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: ascribe
3
- version: !ruby/object:Gem::Version
4
- hash: 17
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 0
9
- - 7
10
- version: 0.0.7
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Dan Ryan
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-10-09 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2012-05-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: activemodel
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 19
29
- segments:
30
- - 3
31
- - 0
32
- - 10
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
33
21
  version: 3.0.10
34
22
  type: :runtime
35
- version_requirements: *id001
36
- - !ruby/object:Gem::Dependency
37
- name: yajl-ruby
38
23
  prerelease: false
39
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.10
30
+ - !ruby/object:Gem::Dependency
31
+ name: yajl-ruby
32
+ requirement: !ruby/object:Gem::Requirement
40
33
  none: false
41
- requirements:
42
- - - ">="
43
- - !ruby/object:Gem::Version
44
- hash: 59
45
- segments:
46
- - 0
47
- - 8
48
- - 2
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
49
37
  version: 0.8.2
50
38
  type: :runtime
51
- version_requirements: *id002
52
- - !ruby/object:Gem::Dependency
53
- name: rake
54
39
  prerelease: false
55
- requirement: &id003 !ruby/object:Gem::Requirement
40
+ version_requirements: !ruby/object:Gem::Requirement
56
41
  none: false
57
- requirements:
58
- - - ">="
59
- - !ruby/object:Gem::Version
60
- hash: 63
61
- segments:
62
- - 0
63
- - 9
64
- - 2
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 0.8.2
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
65
53
  version: 0.9.2
66
54
  type: :runtime
67
- version_requirements: *id003
68
- description: ""
69
- email:
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 0.9.2
62
+ description: No longer supported - please use active_attr (https://github.com/cgriego/active_attr)
63
+ email:
70
64
  - dan@appliedawesome.com
71
65
  executables: []
72
-
73
66
  extensions: []
74
-
75
67
  extra_rdoc_files: []
76
-
77
- files:
68
+ files:
78
69
  - .gitignore
79
70
  - Gemfile
80
71
  - README.md
@@ -83,42 +74,34 @@ files:
83
74
  - lib/ascribe.rb
84
75
  - lib/ascribe/attribute.rb
85
76
  - lib/ascribe/attributes.rb
77
+ - lib/ascribe/extensions/boolean.rb
86
78
  - lib/ascribe/version.rb
87
79
  - spec/ascribe_spec.rb
88
80
  - spec/spec_helper.rb
89
81
  homepage: https://github.com/danryan/ascribe
90
82
  licenses: []
91
-
92
83
  post_install_message:
93
84
  rdoc_options: []
94
-
95
- require_paths:
85
+ require_paths:
96
86
  - lib
97
- required_ruby_version: !ruby/object:Gem::Requirement
87
+ required_ruby_version: !ruby/object:Gem::Requirement
98
88
  none: false
99
- requirements:
100
- - - ">="
101
- - !ruby/object:Gem::Version
102
- hash: 3
103
- segments:
104
- - 0
105
- version: "0"
106
- required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
94
  none: false
108
- requirements:
109
- - - ">="
110
- - !ruby/object:Gem::Version
111
- hash: 3
112
- segments:
113
- - 0
114
- version: "0"
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
115
99
  requirements: []
116
-
117
100
  rubyforge_project: ascribe
118
- rubygems_version: 1.8.10
101
+ rubygems_version: 1.8.21
119
102
  signing_key:
120
103
  specification_version: 3
121
104
  summary: Attributes for your Ruby objects
122
- test_files:
105
+ test_files:
123
106
  - spec/ascribe_spec.rb
124
107
  - spec/spec_helper.rb