lazymodel 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Lazymodel
2
2
 
3
+ [![Build Status](https://secure.travis-ci.org/spaghetticode/lazymodel.png)](http://travis-ci.org/spaghetticode/lazymodel)
4
+
3
5
  This rails gem helps get ActiveModel goodies without the hassle to write a ton
4
6
  of code.
5
7
 
@@ -9,34 +11,51 @@ Add to your gemfile:
9
11
  ```ruby
10
12
  gem 'lazymodel'
11
13
  ```
14
+
12
15
  Create the model class that gets ActiveModel features. The key point is having
13
16
  the class inherit from ```Lazymodel::Base```
17
+
14
18
  ```ruby
15
19
  class Person < Lazymodel::Base
16
- activemodel_attributes :name, :surname, :suffix => '?'
20
+ activemodel_attributes :name, :surname, :suffix => '?', :callbacks => {
21
+ :before_create => :capitalize_attributes,
22
+ :after_update => [:notify_admin, :check_data]
23
+ }
17
24
 
18
25
  validates :name, :surname, :presence => true
19
26
 
27
+ def save
28
+ run_callbacks {# saving...}
29
+ end
30
+
31
+ def update
32
+ run_callbacks {# updating...}
33
+ end
34
+
20
35
  private
21
36
 
22
37
  def attribute?(attribute)
23
38
  send(attribute).present?
24
39
  end
40
+
41
+ def check_data; end
42
+ def notify_admin; end
43
+ def capitalize_attributes; end
25
44
  end
26
45
  ```
46
+
27
47
  This will grant you the power of the following modules:
48
+
28
49
  ```ruby
29
50
  ActiveModel::Naming
30
51
  ActiveModel::Concern
52
+ ActiveModel::Callbacks
31
53
  ActiveModel::Validations
32
54
  ActiveModel::AttributeMethods
33
55
  ```
56
+
34
57
  Via the class macro ```activemodel_attributes``` you can define in a single call the
35
58
  model attributes and its prefixed and suffixed methods. You also get reasonable
36
59
  ``` initialize, to_model, persisted?, attributes``` method defaults.
37
60
 
38
61
  The class complies with ```ActiveModel::Lint::Tests``` specifications.
39
-
40
- ## Todo
41
-
42
- Add activemodel callbacks features
@@ -4,6 +4,7 @@ module Lazymodel
4
4
  include ActiveModel::Conversion
5
5
  include ActiveModel::Validations
6
6
  include ActiveModel::AttributeMethods
7
+ extend ActiveModel::Callbacks
7
8
 
8
9
  class << self
9
10
  def activemodel_attributes(*args)
@@ -12,6 +13,7 @@ module Lazymodel
12
13
  attr_accessor *args
13
14
  define_prefix_or_affix_method :prefix, opts[:prefix]
14
15
  define_prefix_or_affix_method :suffix, opts[:suffix]
16
+ define_callbacks_macros(opts[:callbacks]) if opts[:callbacks]
15
17
  define_attribute_methods args
16
18
  # _attributes stores at class level the
17
19
  # the attribute names
@@ -21,6 +23,17 @@ module Lazymodel
21
23
 
22
24
  private
23
25
 
26
+ def define_callbacks_macros(callbacks)
27
+ callbacks.keys.map do |key|
28
+ key.to_s.sub(/^(before_|after_|around_)/, '')
29
+ end.uniq.each do |method|
30
+ define_model_callbacks method
31
+ end
32
+ callbacks.each do |type, methods|
33
+ send type, *Array.wrap(methods)
34
+ end
35
+ end
36
+
24
37
  def define_prefix_or_affix_method(type, values)
25
38
  Array.wrap(values).each do |value|
26
39
  send "attribute_method_#{type}", value
@@ -1,3 +1,3 @@
1
1
  module Lazymodel
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end