amee_rails_layer 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +1 -2
- data/VERSION +1 -1
- data/amee_rails_layer.gemspec +5 -5
- data/lib/amee_rails_layer/amee_carbon_store.rb +20 -58
- metadata +11 -4
data/README.rdoc
CHANGED
@@ -19,8 +19,7 @@ these would be:
|
|
19
19
|
* units (string) - the units the amount field is in
|
20
20
|
* amount (float) - the amount of the thing being recorded, eg 6 (kg), 9 (litres)
|
21
21
|
with either:
|
22
|
-
* amee_profile (string) - the AMEE profile identifier under which all data is stored
|
23
|
-
* or profile_id (integer) - the record id of the model holding the amee_profile (and of course that model to have an amee_profile field)
|
22
|
+
* amee_profile (string) - the AMEE profile identifier under which all data is stored [optional]
|
24
23
|
Extra fields may also be required depending on the options used. See AmeeCarbonStore for full details
|
25
24
|
|
26
25
|
== Usage
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.6.0
|
data/amee_rails_layer.gemspec
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
# Generated by jeweler
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{amee_rails_layer}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.6.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["George Palmer", "James Smith"]
|
12
|
-
s.date = %q{
|
12
|
+
s.date = %q{2011-03-07}
|
13
13
|
s.description = %q{An abstraction layer for building applications around the AMEE API}
|
14
14
|
s.email = %q{help@amee.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -35,7 +35,7 @@ Gem::Specification.new do |s|
|
|
35
35
|
s.homepage = %q{http://github.com/AMEE/amee_rails_layer}
|
36
36
|
s.rdoc_options = ["--charset=UTF-8"]
|
37
37
|
s.require_paths = ["lib"]
|
38
|
-
s.rubygems_version = %q{1.3.
|
38
|
+
s.rubygems_version = %q{1.3.7}
|
39
39
|
s.summary = %q{An abstraction layer for building applications around the AMEE API}
|
40
40
|
s.test_files = [
|
41
41
|
"test/helper.rb",
|
@@ -46,7 +46,7 @@ Gem::Specification.new do |s|
|
|
46
46
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
47
47
|
s.specification_version = 3
|
48
48
|
|
49
|
-
if Gem::Version.new(Gem::
|
49
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
50
50
|
s.add_development_dependency(%q<amee-ruby>, [">= 0"])
|
51
51
|
else
|
52
52
|
s.add_dependency(%q<amee-ruby>, [">= 0"])
|
@@ -10,11 +10,8 @@
|
|
10
10
|
# * units (string) - the units the amount field is in
|
11
11
|
# * amount (float) - the amount of the thing being recorded, eg 6 (kg), 9 (litres)
|
12
12
|
# * amee_profile (string) - optional. The amee profile identifier under which all the data
|
13
|
-
# is stored. Although this field is optional,
|
14
|
-
#
|
15
|
-
# amee_profile identifier. Name is changed according to the :profile option passed into
|
16
|
-
# the has_carbon_data_stored_in_amee. Although this field is optional, either this or
|
17
|
-
# amee_profile is required.
|
13
|
+
# is stored. Although this field is optional, if it is not present then a Proc must be
|
14
|
+
# passed to the has_carbon_data_stored_in_amee decleration evaluating to an amee profile
|
18
15
|
# * repetitions (integer) - optional. Used when the model object is composed of several
|
19
16
|
# repetitions - for example 6 x 3 miles would make the repetitions 6
|
20
17
|
# * start_date (date) - optional. Used in combination with the has_date_range option on
|
@@ -28,29 +25,27 @@ module AmeeCarbonStore
|
|
28
25
|
|
29
26
|
module ClassMethods
|
30
27
|
# Class method that configures a class for storing carbon data in amee. Options are as follows:
|
31
|
-
# * profile - if set will use this
|
32
|
-
#
|
33
|
-
# - eg
|
34
|
-
#
|
35
|
-
#
|
28
|
+
# * profile - if set will use this Proc to access the amee profile used to store the data under
|
29
|
+
# rather than the model itself. Pass a Proc that evaluates to the model containing the amee
|
30
|
+
# profile - eg Proc.new{|model| model.method}, Proc.new{|model| model.parent.method}. The
|
31
|
+
# referenced model must store the amee profile key under the field amee_profile (as would be
|
32
|
+
# provided by the has_amee_profile decleration)
|
36
33
|
# * nameless - if set then automatically assign the name field so the user doesn't have to
|
37
34
|
# (still requires the name field in the database as a name must be set to store in amee)
|
38
35
|
# * has_date_range - will check for the presence of a start_date and end_date on the object
|
39
|
-
# and pass that through to AMEE to store with the data.
|
40
|
-
#
|
41
|
-
# therefore not recommended as it will be easy to create overlapping data). Requires the
|
42
|
-
# start_date and end_date database fields as described in header.
|
36
|
+
# and pass that through to AMEE to store with the data. Requires the start_date and end_date
|
37
|
+
# database fields as described in header.
|
43
38
|
# * repetitions - allows repetitions of the data at a database level where AMEE doesn't support
|
44
39
|
# it natively. For example multiple journeys can be setup with this option. The value stored
|
45
40
|
# in AMEE will be the total for all journeys. Requires the repetitions database field as
|
46
41
|
# described in header.
|
47
|
-
# * singular_types - if using a structure where multiple types are available for a model and
|
48
|
-
# the type is stored in the field "#{model}_type", this option enforces that only one instance
|
49
|
-
# of each type may exist in the database (for a given project if using a project structure)
|
50
42
|
def has_carbon_data_stored_in_amee(options = {})
|
51
|
-
|
52
43
|
if options[:profile]
|
53
|
-
|
44
|
+
unless options[:profile].is_a?(Proc)
|
45
|
+
warn '[DEPRECIATION] AmeeRailsLayer will no longer automatically provide the profile association. The relationship to the amee profile will also need to be defined as a Proc.'
|
46
|
+
belongs_to options[:profile]
|
47
|
+
options[:profile] = Proc.new{|model| model.send(options[:profile])}
|
48
|
+
end
|
54
49
|
else
|
55
50
|
has_amee_profile
|
56
51
|
end
|
@@ -59,18 +54,10 @@ module AmeeCarbonStore
|
|
59
54
|
validate_on_create :units_are_valid
|
60
55
|
validates_presence_of :start_date, :end_date if options[:has_date_range]
|
61
56
|
unless options[:nameless]
|
62
|
-
if options[:has_date_range]
|
63
|
-
validate :name_is_unique_given_date_range
|
64
|
-
else
|
65
|
-
uniqueness_options = options[:profile] ? {:scope => "#{options[:profile]}_id".to_sym} : {}
|
66
|
-
validates_uniqueness_of :name, uniqueness_options
|
67
|
-
end
|
68
|
-
validates_format_of :name, :with => /\A[\w -]+\Z/,
|
69
|
-
:message => "must be letters, numbers, spaces or underscores only"
|
70
57
|
validates_length_of :name, :maximum => 250
|
71
58
|
end
|
72
59
|
if options[:singular_types]
|
73
|
-
|
60
|
+
warn '[DEPRECIATION] singular_types option is no longer supported.'
|
74
61
|
end
|
75
62
|
if options[:repetitions]
|
76
63
|
validates_numericality_of :repetitions, :only_integer => true
|
@@ -80,7 +67,7 @@ module AmeeCarbonStore
|
|
80
67
|
before_update :update_amee
|
81
68
|
after_destroy :delete_from_amee
|
82
69
|
|
83
|
-
write_inheritable_attribute(:
|
70
|
+
write_inheritable_attribute(:amee_profile_proc, options[:profile]) if options[:profile]
|
84
71
|
write_inheritable_attribute(:repetitions, true) if options[:repetitions]
|
85
72
|
write_inheritable_attribute(:nameless_entries, true) if options[:nameless]
|
86
73
|
write_inheritable_attribute(:has_date_range, true) if options[:has_date_range]
|
@@ -145,26 +132,6 @@ module AmeeCarbonStore
|
|
145
132
|
def units_are_valid
|
146
133
|
errors.add("units", "are not valid") if amount_symbol.nil?
|
147
134
|
end
|
148
|
-
|
149
|
-
def name_is_unique_given_date_range
|
150
|
-
conditions = amee_profile_class_scoping.merge(:name => self.name)
|
151
|
-
self.class.find(:all, :conditions => conditions).each do |record|
|
152
|
-
next if record.id == self.id
|
153
|
-
unless (self.start_date < record.start_date && self.end_date <= record.start_date) ||
|
154
|
-
(self.start_date >= record.end_date && self.end_date > record.end_date)
|
155
|
-
errors.add_to_base("Entry already added covering dates within that range")
|
156
|
-
return false
|
157
|
-
end
|
158
|
-
end
|
159
|
-
end
|
160
|
-
|
161
|
-
def maximum_one_instance_for_each_type
|
162
|
-
model_type = "#{self.class.name.underscore}_type".to_sym
|
163
|
-
conditions = amee_profile_class_scoping.merge(model_type => send(model_type))
|
164
|
-
if self.class.send(:find, :first, :conditions => conditions)
|
165
|
-
errors.add_to_base "Only one #{amee_category.name} entry allowed"
|
166
|
-
end
|
167
|
-
end
|
168
135
|
|
169
136
|
def add_to_amee
|
170
137
|
profile = create_amee_profile
|
@@ -198,18 +165,13 @@ module AmeeCarbonStore
|
|
198
165
|
|
199
166
|
# TODO can be renamed to amee_connection once ruby-amee rails lib merged in
|
200
167
|
def connection_to_amee
|
201
|
-
|
202
|
-
|
168
|
+
profile_proc = self.class.read_inheritable_attribute(:amee_profile_proc)
|
169
|
+
profile_proc ? profile_proc.call(self).amee_connection : amee_connection
|
203
170
|
end
|
204
171
|
|
205
172
|
def amee_profile_path
|
206
|
-
|
207
|
-
|
208
|
-
end
|
209
|
-
|
210
|
-
def amee_profile_class_scoping
|
211
|
-
method = self.class.read_inheritable_attribute(:amee_profile_class)
|
212
|
-
method ? {"#{method}_id".to_sym => send(method).id} : {}
|
173
|
+
profile_proc = self.class.read_inheritable_attribute(:amee_profile_proc)
|
174
|
+
profile_proc ? "/profiles/#{profile_proc.call(self).amee_profile}" : "/profiles/#{amee_profile}"
|
213
175
|
end
|
214
176
|
|
215
177
|
def amee_profile_item
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: amee_rails_layer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 7
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
7
|
- 0
|
7
|
-
-
|
8
|
+
- 6
|
8
9
|
- 0
|
9
|
-
version: 0.
|
10
|
+
version: 0.6.0
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- George Palmer
|
@@ -15,16 +16,18 @@ autorequire:
|
|
15
16
|
bindir: bin
|
16
17
|
cert_chain: []
|
17
18
|
|
18
|
-
date:
|
19
|
+
date: 2011-03-07 00:00:00 +00:00
|
19
20
|
default_executable:
|
20
21
|
dependencies:
|
21
22
|
- !ruby/object:Gem::Dependency
|
22
23
|
name: amee-ruby
|
23
24
|
prerelease: false
|
24
25
|
requirement: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
25
27
|
requirements:
|
26
28
|
- - ">="
|
27
29
|
- !ruby/object:Gem::Version
|
30
|
+
hash: 3
|
28
31
|
segments:
|
29
32
|
- 0
|
30
33
|
version: "0"
|
@@ -64,23 +67,27 @@ rdoc_options:
|
|
64
67
|
require_paths:
|
65
68
|
- lib
|
66
69
|
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
67
71
|
requirements:
|
68
72
|
- - ">="
|
69
73
|
- !ruby/object:Gem::Version
|
74
|
+
hash: 3
|
70
75
|
segments:
|
71
76
|
- 0
|
72
77
|
version: "0"
|
73
78
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
74
80
|
requirements:
|
75
81
|
- - ">="
|
76
82
|
- !ruby/object:Gem::Version
|
83
|
+
hash: 3
|
77
84
|
segments:
|
78
85
|
- 0
|
79
86
|
version: "0"
|
80
87
|
requirements: []
|
81
88
|
|
82
89
|
rubyforge_project:
|
83
|
-
rubygems_version: 1.3.
|
90
|
+
rubygems_version: 1.3.7
|
84
91
|
signing_key:
|
85
92
|
specification_version: 3
|
86
93
|
summary: An abstraction layer for building applications around the AMEE API
|