operable 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -6,11 +6,7 @@ An easy way to perform simple operations (addition, subtraction, multiplication,
6
6
 
7
7
  ## A Simple Example
8
8
 
9
- Let's say we're collecting records of Baseball games. A `Team` model has various stats like `W`, `L`, `T`, etc.
10
-
11
- Operable, once included in your models, lets you use the `+`, `-`, `*` and `/` operators on them, just as you would an Integer. It returns in the value of the class you're already working with.
12
-
13
- We might imagine something like this:
9
+ Let's say we're collecting records of Baseball games. A `Team` model has various stats like `W`, `L`, `T`, etc. Including Operable in your model definition lets you use the `+`, `-`, `*` and `/` operators on objects just as you would on primatives, returning a new instance of the model class containing the result of your desired operation.
14
10
 
15
11
  ```ruby
16
12
  # Create team instances for the 2 NY baseball teams:
@@ -62,17 +58,17 @@ That's it! You can call operable_on multiple times to add more fields.
62
58
  Four operations are provided: `+`, `-`, `*`, `/`.
63
59
 
64
60
  ```ruby
65
- red = Color.new :r => 128, :g => 0, :b => 0
61
+ red = Color.new :r => 128, :g => 0, :b => 0
66
62
  green = Color.new :r => 0, :g => 128, :b => 0
67
- blue = Color.new :r => 0, :g => 0, :b => 128
63
+ blue = Color.new :r => 0, :g => 0, :b => 128
68
64
 
69
- yellow = green + red # => #<Color r: 128, g: 128, b: 0>
70
- grey = yellow + blue # => #<Color r: 128, g: 128, b: 128>
65
+ yellow = green + red # => #<Color r: 128, g: 128, b: 0>
66
+ grey = yellow + blue # => #<Color r: 128, g: 128, b: 128>
71
67
  bright_red = red * 2 # => #<Color r: 255, g: 0, b: 0>
72
- dark_grey = grey / 3 # => #<Color r: 43, g: 43, b: 43>
68
+ dark_grey = grey / 3 # => #<Color r: 43, g: 43, b: 43>
73
69
  ```
74
70
 
75
- ## Equality
71
+ ## Testing Equality
76
72
 
77
73
  Compare the equality of operable objects with the `matches?` method:
78
74
 
@@ -83,7 +79,8 @@ purple.matches?(red + blue) #=> true
83
79
  purple.matches?(red + green) #=> false
84
80
  ```
85
81
 
86
- ### Operate On All (Mongoid only)
82
+
83
+ ## Operate On All (Mongoid only)
87
84
 
88
85
  Mongoid models define their fields explicitly in the model declaration. We can use this to automatically determine what to operate on:
89
86
 
@@ -99,11 +96,16 @@ class Team
99
96
  end
100
97
  ```
101
98
 
102
- ### Operate On Associations (Mongoid only)
99
+ ## Operate On Associations
103
100
 
104
101
  We can include associations in our list of operable fields just as with normal attributes!
105
102
 
106
- First, include Operable on **both** documents. Then, on the **parent** document, manually specify the name of the association to *operable_on* (associations are not included by *operable_on_all* methods)
103
+ To operate on associations:
104
+
105
+ 1. Include Operable on **both** document classes.
106
+ 2. Manually specify the name of the association on the **parent** document using *operable_on*
107
+
108
+ NOTE: associations are not included by *operable_on_all* methods, you need to manually add them.
107
109
 
108
110
  ```ruby
109
111
  class First
@@ -131,6 +133,8 @@ f3 = f1 + f2 # => #<First a: 3, b: 5>
131
133
  f3.second # => #<Second c: 3, d: 5>
132
134
  ```
133
135
 
136
+ This feature now works with ActiveRecord as well.
137
+
134
138
  ## Enhancements and Pull Requests
135
139
 
136
140
  If you find the project useful but it doesn't meet all of your needs, feel free to fork it and send a pull request. I'd especially love contributions that enhance behavior with ActiveRecord. Please make sure to include specs!
@@ -13,8 +13,17 @@ module Operable
13
13
  attributes.select {|k, v| self.class.operable_fields.include? k }.each do |k, v|
14
14
  values[k.to_s] = v
15
15
  end
16
+
17
+ if respond_to?(:reflections)
18
+ reflections.select {|k, v| self.class.operable_fields.include? k.to_s }.each do |k, v|
19
+ if self.send(k).present?
20
+ values[k.to_s] = send(k)
21
+ end
22
+ end
23
+ end
24
+
16
25
  if respond_to?(:associations)
17
- associations.select {|k, v| self.class.operable_fields.include? k }.each do |k, v|
26
+ associations.select {|k, v| self.class.operable_fields.include? k.to_s }.each do |k, v|
18
27
  values[k.to_s] = send(k)
19
28
  end
20
29
  end
@@ -1,3 +1,3 @@
1
1
  module Operable
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: operable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,55 +9,55 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-08 00:00:00.000000000Z
12
+ date: 2011-12-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
16
- requirement: &70222187456640 !ruby/object:Gem::Requirement
16
+ requirement: &70324818204340 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: '3.0'
21
+ version: 3.1.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70222187456640
24
+ version_requirements: *70324818204340
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activerecord
27
- requirement: &70222187455280 !ruby/object:Gem::Requirement
27
+ requirement: &70324818203660 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
31
31
  - !ruby/object:Gem::Version
32
- version: 3.0.0
32
+ version: 3.1.0
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70222187455280
35
+ version_requirements: *70324818203660
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: mongoid
38
- requirement: &70222187453300 !ruby/object:Gem::Requirement
38
+ requirement: &70324818203180 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
42
42
  - !ruby/object:Gem::Version
43
- version: '2.0'
43
+ version: '2.3'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70222187453300
46
+ version_requirements: *70324818203180
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: bson_ext
49
- requirement: &70222187452100 !ruby/object:Gem::Requirement
49
+ requirement: &70324818202700 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
53
53
  - !ruby/object:Gem::Version
54
- version: '1.3'
54
+ version: '1.4'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70222187452100
57
+ version_requirements: *70324818202700
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: sqlite3
60
- requirement: &70222187450200 !ruby/object:Gem::Requirement
60
+ requirement: &70324818202020 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '1.3'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70222187450200
68
+ version_requirements: *70324818202020
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: growl
71
- requirement: &70222187448920 !ruby/object:Gem::Requirement
71
+ requirement: &70324818201460 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70222187448920
79
+ version_requirements: *70324818201460
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: rake
82
- requirement: &70222187448060 !ruby/object:Gem::Requirement
82
+ requirement: &70324818200720 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,29 +87,29 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *70222187448060
90
+ version_requirements: *70324818200720
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: rspec
93
- requirement: &70222187446480 !ruby/object:Gem::Requirement
93
+ requirement: &70324818199700 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ~>
97
97
  - !ruby/object:Gem::Version
98
- version: '2.6'
98
+ version: 2.8.0.rc1
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *70222187446480
101
+ version_requirements: *70324818199700
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: guard-rspec
104
- requirement: &70222187445540 !ruby/object:Gem::Requirement
104
+ requirement: &70324818199040 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
- - - ~>
107
+ - - ! '>='
108
108
  - !ruby/object:Gem::Version
109
- version: 0.4.3
109
+ version: '0'
110
110
  type: :development
111
111
  prerelease: false
112
- version_requirements: *70222187445540
112
+ version_requirements: *70324818199040
113
113
  description: Perform addition, subtraction, multiplication and division on your models.
114
114
  email:
115
115
  - jcoene@gmail.com
@@ -145,7 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
145
145
  version: '0'
146
146
  requirements: []
147
147
  rubyforge_project:
148
- rubygems_version: 1.8.10
148
+ rubygems_version: 1.8.11
149
149
  signing_key:
150
150
  specification_version: 3
151
151
  summary: Arithmetic operations on your models.