ninja-model 0.5.7 → 0.6.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/.gitignore CHANGED
@@ -17,3 +17,4 @@ Gemfile.lock
17
17
  .bundle
18
18
  *.gem
19
19
  .rspec
20
+ .rvmrc
@@ -90,10 +90,14 @@ module NinjaModel
90
90
 
91
91
  def initialize(attributes = nil)
92
92
  @attributes = attributes_from_model_attributes
93
- self.attributes = attributes unless attributes.nil?
94
93
  @persisted = false
95
94
  @readonly = true
96
95
  @destroyed = false
96
+
97
+ populate_with_current_scope_attributes
98
+
99
+ self.attributes = attributes unless attributes.nil?
100
+
97
101
  result = yield self if block_given?
98
102
  _run_initialize_callbacks
99
103
  result
@@ -103,9 +107,21 @@ module NinjaModel
103
107
  @attributes = record.stringify_keys
104
108
  @readonly = @destroyed = false
105
109
  @persisted = true
110
+
111
+ populate_with_current_scope_attributes
112
+
106
113
  _run_find_callbacks
107
114
  _run_initialize_callbacks
108
115
  self
109
116
  end
117
+
118
+ private
119
+
120
+ def populate_with_current_scope_attributes
121
+ if scope = self.class.send(:current_scoped_methods)
122
+ create_with = scope.scope_for_create
123
+ create_with.each { |att, value| self.respond_to?(:"#{att}=") && self.send("#{att}=", value) } if create_with
124
+ end
125
+ end
110
126
  end
111
127
  end
@@ -30,6 +30,12 @@ module NinjaModel
30
30
  end
31
31
  end
32
32
 
33
+ def new(*args, &block)
34
+ scoping { @klass.new(*args, &block) }
35
+ end
36
+
37
+ alias build new
38
+
33
39
  def to_a
34
40
  @records ||= begin
35
41
  records = @klass.adapter.read(self)
@@ -65,6 +71,17 @@ module NinjaModel
65
71
  to_a.inspect
66
72
  end
67
73
 
74
+ def scope_for_create
75
+ Hash[@predicates.find_all { |w|
76
+ w.respond_to?(:meth) && w.meth == :eq
77
+ }.map { |where|
78
+ [
79
+ where.attribute,
80
+ where.value
81
+ ]
82
+ }]
83
+ end
84
+
68
85
  protected
69
86
 
70
87
  def method_missing(method, *args, &block)
@@ -1,3 +1,3 @@
1
1
  module NinjaModel
2
- VERSION = "0.5.7"
2
+ VERSION = "0.6.0"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ninja-model
3
3
  version: !ruby/object:Gem::Version
4
- hash: 5
5
- prerelease: false
4
+ hash: 7
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
- - 5
9
- - 7
10
- version: 0.5.7
8
+ - 6
9
+ - 0
10
+ version: 0.6.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Josh Williams
@@ -15,8 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-09-21 00:00:00 -05:00
19
- default_executable:
18
+ date: 2011-10-27 00:00:00 Z
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
21
  name: activerecord
@@ -181,7 +180,6 @@ files:
181
180
  - spec/spec_helper.rb
182
181
  - spec/support/active_model_lint.rb
183
182
  - spec/support/matchers/convert.rb
184
- has_rdoc: true
185
183
  homepage: http://github.com/t3hpr1m3/ninja-model.git
186
184
  licenses: []
187
185
 
@@ -211,7 +209,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
211
209
  requirements: []
212
210
 
213
211
  rubyforge_project: ninja-model
214
- rubygems_version: 1.3.7
212
+ rubygems_version: 1.7.2
215
213
  signing_key:
216
214
  specification_version: 3
217
215
  summary: Pseudo-ORM for Ruby
@@ -235,3 +233,4 @@ test_files:
235
233
  - spec/spec_helper.rb
236
234
  - spec/support/active_model_lint.rb
237
235
  - spec/support/matchers/convert.rb
236
+ has_rdoc: