rack-core-data 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rack-core-data (0.0.3)
4
+ rack-core-data (0.1.0)
5
5
  activesupport (~> 3.2.6)
6
6
  nokogiri (~> 1.4)
7
7
  rack (~> 1.4)
@@ -31,9 +31,11 @@ module Rack
31
31
 
32
32
  klass.class_eval do
33
33
  self.strict_param_setting = false
34
+ self.raise_on_save_failure = false
34
35
 
35
36
  plugin :json_serializer, :naked => true, :include => :url, :except => :id
36
37
  plugin :schema
38
+ plugin :validation_helpers
37
39
 
38
40
  def url
39
41
  "/#{self.class.table_name}/#{id}"
@@ -53,6 +55,8 @@ module Rack
53
55
  primary_key :id
54
56
 
55
57
  entity.attributes.each do |attribute|
58
+ next if attribute.transient?
59
+
56
60
  options = {
57
61
  :null => attribute.optional?,
58
62
  :index => attribute.indexed?,
@@ -90,10 +94,27 @@ module Rack
90
94
  create_table unless table_exists?
91
95
  end
92
96
 
97
+ klass.send :define_method, :validate do
98
+ entity.attributes.each do |attribute|
99
+ case attribute.type
100
+ when "Integer 16", "Integer 32", "Integer 64"
101
+ validates_integer attribute.name
102
+ when "Float", "Double", "Decimal"
103
+ validates_numeric attribute.name
104
+ when "String"
105
+ validates_min_length attribute.minimum_value, attribute.name if attribute.minimum_value
106
+ validates_max_length attribute.maximum_value, attribute.name if attribute.maximum_value
107
+ validates_min_length attribute.minimum_value, attribute.name if attribute.minimum_value
108
+ end
109
+ end
110
+ end
111
+
93
112
  app.class_eval do
94
113
  include Rack::CoreData::Models
95
114
  klass = Rack::CoreData::Models.const_get(entity.name.capitalize)
96
115
 
116
+ disable :raise_errors, :show_exceptions
117
+
97
118
  get "/#{entity.name.downcase.pluralize}/?" do
98
119
  klass.all.to_json
99
120
  end
@@ -105,7 +126,7 @@ module Rack
105
126
  record.to_json
106
127
  else
107
128
  status 406
108
- record.errors.to_json
129
+ {errors: record.errors}.to_json
109
130
  end
110
131
  end
111
132
 
@@ -120,7 +141,7 @@ module Rack
120
141
  record.to_json
121
142
  else
122
143
  status 406
123
- record.errors.to_json
144
+ {errors: record.errors}.to_json
124
145
  end
125
146
  end
126
147
 
@@ -130,7 +151,7 @@ module Rack
130
151
  status 200
131
152
  else
132
153
  status 406
133
- record.errors.to_json
154
+ {errors: record.errors}.to_json
134
155
  end
135
156
  end
136
157
 
@@ -1,6 +1,6 @@
1
1
  class Rack::CoreData::DataModel
2
2
  class Attribute
3
- attr_reader :name, :type, :identifier, :version_hash_modifier, :default_value
3
+ attr_reader :name, :type, :identifier, :version_hash_modifier, :default_value, :minimum_value, :maximum_value, :regular_expression
4
4
 
5
5
  def initialize(attribute)
6
6
  raise ArgumentError unless ::Nokogiri::XML::Element === attribute
@@ -9,12 +9,12 @@ class Rack::CoreData::DataModel
9
9
  @type = attribute['attributeType']
10
10
  @identifier = attribute['elementID']
11
11
  @version_hash_modifier = attribute['versionHashModifier']
12
- @default_value = case @type
13
- when "Integer 16", "Integer 32", "Integer 64"
14
- attribute['defaultValueString'].to_i
15
- when "Float", "Decimal"
16
- attribute['defaultValueString'].to_f
17
- end if attribute['defaultValueString']
12
+
13
+ @default_value = default_value_from_string(attribute['defaultValueString'])
14
+ @minimum_value = range_value_from_string(attribute['minValueString'])
15
+ @maximum_value = range_value_from_string(attribute['maxValueString'])
16
+
17
+ @regular_expression = Regexp.new(attributes['regularExpressionString']) rescue nil
18
18
 
19
19
  @optional = attribute['optional'] == "YES"
20
20
  @transient = attribute['transient'] == "YES"
@@ -23,11 +23,41 @@ class Rack::CoreData::DataModel
23
23
  end
24
24
 
25
25
  def to_s
26
- @name
26
+ [@name, @type].to_s
27
27
  end
28
28
 
29
29
  [:optional, :transient, :indexed, :syncable].each do |symbol|
30
30
  define_method("#{symbol}?") {!!instance_variable_get(("@#{symbol}").intern)}
31
31
  end
32
+
33
+ private
34
+
35
+ def default_value_from_string(string)
36
+ return nil unless string
37
+
38
+ return case @type
39
+ when "Integer 16", "Integer 32", "Integer 64"
40
+ string.to_i
41
+ when "Float", "Decimal"
42
+ string.to_f
43
+ when "Boolean"
44
+ string == "YES"
45
+ else
46
+ string
47
+ end
48
+ end
49
+
50
+ def range_value_from_string(string)
51
+ return nil unless string
52
+
53
+ return case @type
54
+ when "Float", "Decimal"
55
+ string.to_f
56
+ when "Date"
57
+ string
58
+ else
59
+ string.to_i
60
+ end
61
+ end
32
62
  end
33
63
  end
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  module CoreData
3
- VERSION = '0.0.3'
3
+ VERSION = '0.1.0'
4
4
  end
5
5
  end
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-core-data
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-06 00:00:00.000000000 Z
12
+ date: 2012-11-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -138,6 +138,7 @@ files:
138
138
  - ./lib/rack/core-data/version.rb
139
139
  - ./lib/rack/core-data.rb
140
140
  - ./LICENSE
141
+ - ./rack-core-data-0.0.3.gem
141
142
  - ./rack-core-data.gemspec
142
143
  - ./Rakefile
143
144
  - ./README.md
@@ -155,7 +156,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
155
156
  version: '0'
156
157
  segments:
157
158
  - 0
158
- hash: 2254694129767937347
159
+ hash: -107443950940570454
159
160
  required_rubygems_version: !ruby/object:Gem::Requirement
160
161
  none: false
161
162
  requirements:
@@ -164,7 +165,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
164
165
  version: '0'
165
166
  segments:
166
167
  - 0
167
- hash: 2254694129767937347
168
+ hash: -107443950940570454
168
169
  requirements: []
169
170
  rubyforge_project:
170
171
  rubygems_version: 1.8.24