hydra_attribute 0.3.0.beta1 → 0.3.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.
@@ -0,0 +1,102 @@
1
+ Feature: destroy model
2
+ When destroy model
3
+ Then all associated values should be deleted too
4
+
5
+ Background: create hydra attributes
6
+ Given create "HydraAttribute::HydraAttribute" models with attributes as "hashes":
7
+ | entity_type | name | backend_type | default_value |
8
+ | Product | code | string | [nil:] |
9
+ | Product | price | float | [string:0] |
10
+ | Product | active | boolean | [string:0] |
11
+ | Product | info | text | [string:] |
12
+ | Product | started | datetime | [string:2012-01-01] |
13
+
14
+ Scenario: destroy model
15
+ Given create "Product" model with attributes as "hashes":
16
+ | code | price | active | info | started |
17
+ | [string:1] | [float:1] | [boolean:true] | [string:a] | [string:2012-01-01] |
18
+ | [string:2] | [float:2] | [boolean:true] | [string:b] | [string:2012-01-02] |
19
+ | [string:3] | [float:3] | [boolean:true] | [string:c] | [string:2012-01-03] |
20
+ | [string:4] | [float:4] | [boolean:true] | [string:d] | [string:2012-01-04] |
21
+ | [string:5] | [float:5] | [boolean:true] | [string:e] | [string:2012-01-05] |
22
+
23
+ When select first "HydraAttribute::HydraStringProduct" record
24
+ Then record read attribute "value" and value should be "[string:1]"
25
+ When select first "HydraAttribute::HydraFloatProduct" record
26
+ Then record read attribute "value" and value should be "[float:1]"
27
+ When select first "HydraAttribute::HydraBooleanProduct" record
28
+ Then record read attribute "value" and value should be "[boolean:true]"
29
+ When select first "HydraAttribute::HydraTextProduct" record
30
+ Then record read attribute "value" and value should be "[string:a]"
31
+ When select first "HydraAttribute::HydraDatetimeProduct" record
32
+ Then record read attribute "value" and value should be "[string:2012-01-01]"
33
+
34
+ Given select first "Product" record
35
+ And destroy record
36
+
37
+ When select first "HydraAttribute::HydraStringProduct" record
38
+ Then record read attribute "value" and value should be "[string:2]"
39
+ When select first "HydraAttribute::HydraFloatProduct" record
40
+ Then record read attribute "value" and value should be "[float:2]"
41
+ When select first "HydraAttribute::HydraBooleanProduct" record
42
+ Then record read attribute "value" and value should be "[boolean:true]"
43
+ When select first "HydraAttribute::HydraTextProduct" record
44
+ Then record read attribute "value" and value should be "[string:b]"
45
+ When select first "HydraAttribute::HydraDatetimeProduct" record
46
+ Then record read attribute "value" and value should be "[string:2012-01-02]"
47
+
48
+ Given select first "Product" record
49
+ And destroy record
50
+
51
+ When select first "HydraAttribute::HydraStringProduct" record
52
+ Then record read attribute "value" and value should be "[string:3]"
53
+ When select first "HydraAttribute::HydraFloatProduct" record
54
+ Then record read attribute "value" and value should be "[float:3]"
55
+ When select first "HydraAttribute::HydraBooleanProduct" record
56
+ Then record read attribute "value" and value should be "[boolean:true]"
57
+ When select first "HydraAttribute::HydraTextProduct" record
58
+ Then record read attribute "value" and value should be "[string:c]"
59
+ When select first "HydraAttribute::HydraDatetimeProduct" record
60
+ Then record read attribute "value" and value should be "[string:2012-01-03]"
61
+
62
+ Given select first "Product" record
63
+ And destroy record
64
+
65
+ When select first "HydraAttribute::HydraStringProduct" record
66
+ Then record read attribute "value" and value should be "[string:4]"
67
+ When select first "HydraAttribute::HydraFloatProduct" record
68
+ Then record read attribute "value" and value should be "[float:4]"
69
+ When select first "HydraAttribute::HydraBooleanProduct" record
70
+ Then record read attribute "value" and value should be "[boolean:true]"
71
+ When select first "HydraAttribute::HydraTextProduct" record
72
+ Then record read attribute "value" and value should be "[string:d]"
73
+ When select first "HydraAttribute::HydraDatetimeProduct" record
74
+ Then record read attribute "value" and value should be "[string:2012-01-04]"
75
+
76
+ Given select first "Product" record
77
+ And destroy record
78
+
79
+ When select first "HydraAttribute::HydraStringProduct" record
80
+ Then record read attribute "value" and value should be "[string:5]"
81
+ When select first "HydraAttribute::HydraFloatProduct" record
82
+ Then record read attribute "value" and value should be "[float:5]"
83
+ When select first "HydraAttribute::HydraBooleanProduct" record
84
+ Then record read attribute "value" and value should be "[boolean:true]"
85
+ When select first "HydraAttribute::HydraTextProduct" record
86
+ Then record read attribute "value" and value should be "[string:e]"
87
+ When select first "HydraAttribute::HydraDatetimeProduct" record
88
+ Then record read attribute "value" and value should be "[string:2012-01-05]"
89
+
90
+ Given select first "Product" record
91
+ And destroy record
92
+
93
+ When select first "HydraAttribute::HydraStringProduct" record
94
+ Then record should be nil
95
+ When select first "HydraAttribute::HydraFloatProduct" record
96
+ Then record should be nil
97
+ When select first "HydraAttribute::HydraBooleanProduct" record
98
+ Then record should be nil
99
+ When select first "HydraAttribute::HydraTextProduct" record
100
+ Then record should be nil
101
+ When select first "HydraAttribute::HydraDatetimeProduct" record
102
+ Then record should be nil
@@ -2,6 +2,12 @@ Given /^create "([^"]+)" model$/ do |klass|
2
2
  klass.constantize.create!
3
3
  end
4
4
 
5
+ Given /^create "([^"]+)" model with attributes as "([^"]+):"$/ do |klass, format, table|
6
+ Array.wrap(table.send(format)).each do |hash|
7
+ klass.constantize.create!(type_cast_hash(hash))
8
+ end
9
+ end
10
+
5
11
  Given /^(load and )?(save|create|update(?: all| attributes)?|destroy(?: all)?|delete(?: all)?)(?: for)? "([^"]+)" models? with attributes as "([^"]+)":$/ do |load, action, klass, format, table|
6
12
  action = action.gsub(' ', '_')
7
13
  klass = klass.constantize
@@ -1,9 +1,9 @@
1
1
  When /^load all "([^"]+)" records$/ do |klass|
2
- @records = Object.const_get(klass).all
2
+ @records = klass.constantize.all
3
3
  end
4
4
 
5
5
  When /^select (first|last) "([^"]+)" record$/ do |method, klass|
6
- @record = Object.const_get(klass).send(method)
6
+ @record = klass.constantize.send(method)
7
7
  end
8
8
 
9
9
  When /^assign attributes as "([^"]+)":$/ do |format, table|
@@ -21,6 +21,10 @@ When /^keep "([^"]+)" attribute$/ do |attribute|
21
21
  @keep[attribute] = @record.send(attribute)
22
22
  end
23
23
 
24
+ Then /^record should be nil$/ do
25
+ @record.should be_nil
26
+ end
27
+
24
28
  Then /^attribute "([^"]+)" (should(?:\snot)?) be the same$/ do |attribute, behavior|
25
29
  method = behavior.sub(/\s/, '_')
26
30
  @keep[attribute].send(method) == @record.send(attribute)
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/kostyantyn/Sites/github/gems/hydra_attribute
3
3
  specs:
4
- hydra_attribute (0.3.0.beta2)
4
+ hydra_attribute (0.3.0.beta1)
5
5
  activerecord (>= 3.1.0)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/kostyantyn/Sites/github/gems/hydra_attribute
3
3
  specs:
4
- hydra_attribute (0.3.0.beta2)
4
+ hydra_attribute (0.3.0.beta1)
5
5
  activerecord (>= 3.1.0)
6
6
 
7
7
  GEM
@@ -111,6 +111,12 @@ module HydraAttribute
111
111
  reset_hydra_attribute_methods
112
112
  super
113
113
  end
114
+
115
+ def inspect
116
+ attr_list = columns.map { |c| "#{c.name}: #{c.type}" }
117
+ attr_list += hydra_attributes.map { |a| "#{a.name}: #{a.backend_type}" }
118
+ "#{name}(#{attr_list.join(', ')})"
119
+ end
114
120
  end
115
121
 
116
122
  def respond_to?(name, include_private = false)
@@ -118,17 +124,16 @@ module HydraAttribute
118
124
  super
119
125
  end
120
126
 
121
- %w(attributes attributes_before_type_cast).each do |method|
122
- class_eval <<-EOS, __FILE__, __LINE__ + 1
123
- def #{method}
124
- self.class.hydra_attribute_backend_types.each_with_object(super) do |type, attributes|
125
- hydra_value_association(type).all_models.each do |model|
126
- hydra_attribute = self.class.hydra_attribute(model.hydra_attribute_id)
127
- attributes[hydra_attribute.name] = model.#{method}['value']
128
- end
129
- end
130
- end
131
- EOS
127
+ def attributes
128
+ hydra_value_models.each_with_object(super) do |model, attributes|
129
+ attributes[model.attribute_name] = model.read_attribute('value')
130
+ end
131
+ end
132
+
133
+ def attributes_before_type_cast
134
+ hydra_value_models.each_with_object(super) do |model, attributes|
135
+ attributes[model.attribute_name] = model.read_attribute_before_type_cast('value')
136
+ end
132
137
  end
133
138
 
134
139
  %w(read_attribute read_attribute_before_type_cast).each do |method|
@@ -148,6 +153,22 @@ module HydraAttribute
148
153
  EOS
149
154
  end
150
155
 
156
+ def hydra_attribute_names
157
+ @hydra_attribute_names ||= hydra_value_models.map(&:attribute_name)
158
+ end
159
+
160
+ def hydra_attribute_backend_types
161
+ @hydra_attribute_backend_types ||= self.class.hydra_attribute_backend_types.select do |backend_type|
162
+ hydra_value_association(backend_type).all_models.any?
163
+ end
164
+ end
165
+
166
+ def inspect
167
+ attrs = self.class.column_names.map { |name| "#{name}: #{attribute_for_inspect(name)}" }
168
+ attrs += hydra_value_models.map { |model| "#{model.attribute_name}: #{model.attribute_for_inspect('value')}" }
169
+ "#<#{self.class} #{attrs.join(', ')}>"
170
+ end
171
+
151
172
  private
152
173
 
153
174
  def hydra_value_model(identifier)
@@ -159,6 +180,12 @@ module HydraAttribute
159
180
  end
160
181
  end
161
182
 
183
+ def hydra_value_models
184
+ @hydra_value_models ||= self.class.hydra_attribute_backend_types.inject([]) do |models, backend_type|
185
+ models + hydra_value_association(backend_type).all_models
186
+ end
187
+ end
188
+
162
189
  def hydra_value_association(backend_type)
163
190
  association(::HydraAttribute::AssociationBuilder.association_name(backend_type))
164
191
  end
@@ -39,6 +39,12 @@ module HydraAttribute
39
39
  value_model.belongs_to :entity, class_name: klass.model_name, autosave: false
40
40
  value_model.belongs_to :hydra_attribute, class_name: 'HydraAttribute::HydraAttribute'
41
41
  value_model.attr_accessible :hydra_attribute_id, :value
42
+
43
+ value_model.class_eval <<-EOS, __FILE__, __LINE__ + 1
44
+ def attribute_name
45
+ #{klass.model_name}.hydra_attribute(hydra_attribute_id).name
46
+ end
47
+ EOS
42
48
  end
43
49
 
44
50
  def add_association
@@ -1,3 +1,3 @@
1
1
  module HydraAttribute
2
- VERSION = '0.3.0.beta1'
2
+ VERSION = '0.3.0'
3
3
  end
metadata CHANGED
@@ -1,19 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hydra_attribute
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0.beta1
5
- prerelease: 6
4
+ version: 0.3.0
5
+ prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Kostyantyn Stepanyuk
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-25 00:00:00.000000000Z
12
+ date: 2012-07-26 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &2152234260 !ruby/object:Gem::Requirement
16
+ requirement: &2152204220 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.1.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2152234260
24
+ version_requirements: *2152204220
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &2152233160 !ruby/object:Gem::Requirement
27
+ requirement: &2152203520 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2152233160
35
+ version_requirements: *2152203520
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: cucumber
38
- requirement: &2152232280 !ruby/object:Gem::Requirement
38
+ requirement: &2152202360 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2152232280
46
+ version_requirements: *2152202360
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: sqlite3
49
- requirement: &2152231480 !ruby/object:Gem::Requirement
49
+ requirement: &2152201580 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2152231480
57
+ version_requirements: *2152201580
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: database_cleaner
60
- requirement: &2152230780 !ruby/object:Gem::Requirement
60
+ requirement: &2152200720 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *2152230780
68
+ version_requirements: *2152200720
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: appraisal
71
- requirement: &2152229960 !ruby/object:Gem::Requirement
71
+ requirement: &2152199780 !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: *2152229960
79
+ version_requirements: *2152199780
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: rake
82
- requirement: &2152229020 !ruby/object:Gem::Requirement
82
+ requirement: &2152198420 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,7 +87,7 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *2152229020
90
+ version_requirements: *2152198420
91
91
  description: hydra_attribute is an implementation of EAV pattern for ActiveRecord
92
92
  models.
93
93
  email: kostya.stepanyuk@gmail.com
@@ -110,6 +110,7 @@ files:
110
110
  - features/attributes/update.feature
111
111
  - features/create.feature
112
112
  - features/define.feature
113
+ - features/destroy.feature
113
114
  - features/query_methods/group.feature
114
115
  - features/query_methods/order.feature
115
116
  - features/query_methods/select.feature
@@ -163,9 +164,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
163
164
  required_rubygems_version: !ruby/object:Gem::Requirement
164
165
  none: false
165
166
  requirements:
166
- - - ! '>'
167
+ - - ! '>='
167
168
  - !ruby/object:Gem::Version
168
- version: 1.3.1
169
+ version: '0'
169
170
  requirements: []
170
171
  rubyforge_project:
171
172
  rubygems_version: 1.8.10
@@ -179,6 +180,7 @@ test_files:
179
180
  - features/attributes/update.feature
180
181
  - features/create.feature
181
182
  - features/define.feature
183
+ - features/destroy.feature
182
184
  - features/query_methods/group.feature
183
185
  - features/query_methods/order.feature
184
186
  - features/query_methods/select.feature