sandwich 0.0.7 → 0.0.8

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.7
1
+ 0.0.8
@@ -16,14 +16,9 @@ Transform /(an?|\d+) (.+) #{qc}$/ do |total, what, value|
16
16
  model = class_for(what)
17
17
  end
18
18
 
19
- raise \
20
- %Q{No attribute passed and no default attribute defined. You can do that with:
19
+ raise "No attribute passed and no default attribute defined for #{model}" if attr.nil?
21
20
 
22
- #{model}.default_attribute :your_default_attribute
23
-
24
- } if attr.nil?
25
-
26
- r = total.times.map { model.make(attr => value) }
21
+ r = total.times.map { model.make(attr.to_s.underscore => value) }
27
22
 
28
23
  r.length == 1 ? r.first : r
29
24
  end
@@ -56,7 +51,9 @@ end
56
51
 
57
52
  Given /^(an?|the|\d+) (.+) with the following attributes:$/ do |total, what, table|
58
53
  class_for(what).tap do |model|
59
- total.times { model.make(Hash[*table.raw.map { |k, v| [k.underscore, v] }.flatten]) }
54
+ total.times do
55
+ model.make(Hash[*table.raw.map { |k, v| [k.underscore, value_for(model, k, v)] }.flatten])
56
+ end
60
57
  end
61
58
  end
62
59
 
@@ -7,7 +7,7 @@ module Sandwich
7
7
  def value_for(model, attr, value)
8
8
  if assoc = model.reflect_on_association(attr.to_sym)
9
9
  assoc_class = assoc.class_name.constantize
10
- assoc_class.make(assoc_class.default_attribute => value)
10
+ assoc_class.find_or_make(assoc_class.default_attribute_query(value))
11
11
  else
12
12
  value
13
13
  end
@@ -1,4 +1,5 @@
1
1
  ActiveRecord::Base.class_eval do
2
2
  extend Sandwich::Base::ClassMethods
3
3
  include Sandwich::Amounts
4
+ include Sandwich::Helpers::Associations
4
5
  end
@@ -0,0 +1,13 @@
1
+ module Sandwich
2
+ module Ext
3
+ module FindOrMake
4
+ module ActiveRecord
5
+ def find_or_make(filters, args = {})
6
+ find(:first, :conditions => filters) || make(filters.merge(args))
7
+ end
8
+
9
+ ::ActiveRecord::Base.send(:extend, self)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,33 @@
1
+ module Sandwich
2
+ module Helpers
3
+ module Associations
4
+ def method_missing(name, *args)
5
+ plural_name = name.to_s.gsub('=', '').pluralize
6
+
7
+ if respond_to?(plural_name)
8
+ value = args.first
9
+ association = self.class.reflect_on_association(plural_name.to_sym)
10
+
11
+ if association
12
+ child_class = association.class_name.constantize
13
+
14
+ if value.kind_of?(child_class)
15
+ send(association_name) << value
16
+ else
17
+ send(plural_name) <<
18
+ child_class.find_or_make(child_class.default_attribute_query(value))
19
+ end
20
+ else
21
+ if send(plural_name)
22
+ send(plural_name) << value
23
+ else
24
+ send("#{plural_name}=", [value])
25
+ end
26
+ end
27
+ else
28
+ super
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -5,9 +5,13 @@ module Sandwich
5
5
  if name
6
6
  @default_attribute = name
7
7
  else
8
- @default_attribute
8
+ @default_attribute or raise "No default attribute defined for #{self}"
9
9
  end
10
10
  end
11
+
12
+ def default_attribute_query(value)
13
+ {default_attribute => value}
14
+ end
11
15
  end
12
16
  end
13
17
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 7
9
- version: 0.0.7
8
+ - 8
9
+ version: 0.0.8
10
10
  platform: ruby
11
11
  authors:
12
12
  - David Leal
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-26 00:00:00 +01:00
17
+ date: 2010-04-27 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -103,9 +103,11 @@ files:
103
103
  - lib/sandwich/cucumber/world.rb
104
104
  - lib/sandwich/ext/active_record.rb
105
105
  - lib/sandwich/ext/capybara.rb
106
+ - lib/sandwich/ext/machinist.rb
106
107
  - lib/sandwich/ext/object.rb
107
108
  - lib/sandwich/ext/string.rb
108
109
  - lib/sandwich/helpers/amounts.rb
110
+ - lib/sandwich/helpers/associations.rb
109
111
  - lib/sandwich/helpers/base.rb
110
112
  - spec/sandwich_spec.rb
111
113
  - spec/spec.opts