sunstone 1.6.8 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6b1787de5cb810783b227cb3f567734aa5ce0cc3
4
- data.tar.gz: 8a98b6a8e435cbfe10085e25afa88ae9a8354177
3
+ metadata.gz: f199678115b092025dfcacc613338734a4fcd233
4
+ data.tar.gz: 0cf2e737fff1dd02536ceacf622f702915efc292
5
5
  SHA512:
6
- metadata.gz: 6ce4c180fbede2b0bf98270636801def200595d654a42f2bc87f4ae6b41034ba58a199bd193973839e95a033335c8ca013eca340ea47417d71ae8ae9139d51d6
7
- data.tar.gz: 17dc0ac2d3c7413f783a3a3162bcdfb80e61dbd6edc0392a1820324b729fb8da346279cedbb23acb245f971f43daf4218df21cc1375bdea4bbfa7578d7d08ee8
6
+ metadata.gz: 9b950838b5a920260ada34a8e40bcf13cf99740f0348b765458ff380105d264202993b3b0d399ea671412722dc637e5d24cb2db7279db2f4dbe844950bf83f33
7
+ data.tar.gz: f5413976e8d8a1bbaded1394701cf81bc854953e4a029e792b2b87825e543f258222448e9141a79cf85af42f9cf812c8a93869af80defe33011e40b9f9d4fb64
@@ -19,16 +19,17 @@ module ActiveRecord
19
19
  end
20
20
 
21
21
  def exec_query(arel, name = 'SAR', binds = [])
22
- result = exec(to_sar(arel, binds), name)
22
+ sar = to_sar(arel, binds)
23
+ result = exec(sar, name)
23
24
 
24
- if result.is_a?(Array)
25
- ActiveRecord::Result.new(result[0] ? result[0].keys : [], result.map{|r| r.values})
25
+ if sar.instance_variable_get(:@sunstone_calculation)
26
+ # this is a count, min, max.... yea i know..
27
+ ActiveRecord::Result.new(['all'], [result], {:all => type_map.lookup('integer')})
26
28
  else
27
- # this is a count.. yea i know..
28
- ActiveRecord::Result.new(['all'], [[result]], {:all => type_map.lookup('integer')})
29
+ ActiveRecord::Result.new(result[0] ? result[0].keys : [], result.map{|r| r.values})
29
30
  end
30
31
  end
31
-
32
+
32
33
  end
33
34
  end
34
35
  end
@@ -62,9 +62,9 @@ module Arel
62
62
  case operation
63
63
  when :count
64
64
  path += "/#{operation}"
65
- when :average, :min, :max
66
- get_params[:column] = columns
67
- path += "/#{operation}"
65
+ when :calculate
66
+ path += "/calculate"
67
+ get_params[:select] = columns
68
68
  when :update, :delete
69
69
  path += "/#{get_params[:where]['id']}"
70
70
  get_params.delete(:where)
@@ -75,6 +75,7 @@ module Arel
75
75
  end
76
76
 
77
77
  request = request_type.new(path)
78
+ request.instance_variable_set(:@sunstone_calculation, true) if operation == :calculate
78
79
 
79
80
  if updates
80
81
  request.body = body
@@ -46,7 +46,7 @@ module Arel
46
46
  collector.request_type = Net::HTTP::Get
47
47
 
48
48
  unless o.projections.empty?
49
- visit(o.projections.first, collector)
49
+ o.projections.each { |proj| visit(proj, collector) }
50
50
  else
51
51
  collector.operation = :select
52
52
  end
@@ -391,8 +391,11 @@ module Arel
391
391
  # end
392
392
  #
393
393
  def visit_Arel_Nodes_Count o, collector
394
- collector.operation = :count
395
- collector.columns = visit o.expressions.first, collector
394
+ collector.operation = :calculate
395
+
396
+ collector.columns ||= []
397
+ collector.columns << {:count => (o.expressions.first.is_a?(Arel::Attributes::Attribute) ? o.expressions.first.name : o.expressions.first) }
398
+ # collector.columns = visit o.expressions.first, collector
396
399
  end
397
400
  #
398
401
  # def visit_Arel_Nodes_Sum o, collector
@@ -400,35 +403,42 @@ module Arel
400
403
  # end
401
404
  #
402
405
  def visit_Arel_Nodes_Max o, collector
403
- collector.operation = :max
406
+ collector.operation = :calculate
407
+
408
+ collector.columns ||= []
404
409
  if o.expressions.first.is_a?(Arel::Attributes::Attribute)
405
410
  relation = o.expressions.first.relation
406
411
  join_name = relation.table_alias || relation.name
407
- collector.columns = join_name ? o.expressions.first.name : "#{join_name}.#{o.expressions.first.name}"
412
+ collector.columns << {:maximum => join_name ? o.expressions.first.name : "#{join_name}.#{o.expressions.first.name}"}
408
413
  else
409
- collector.columns = o.expressions.first
414
+ collector.columns << {:maximum => o.expressions.first}
410
415
  end
411
416
  end
412
417
 
413
418
  def visit_Arel_Nodes_Min o, collector
414
- collector.operation = :min
419
+ collector.operation = :calculate
420
+
421
+ collector.columns ||= []
415
422
  if o.expressions.first.is_a?(Arel::Attributes::Attribute)
416
423
  relation = o.expressions.first.relation
417
424
  join_name = relation.table_alias || relation.name
418
- collector.columns = join_name ? o.expressions.first.name : "#{join_name}.#{o.expressions.first.name}"
425
+ collector.columns << {:minimum => join_name ? o.expressions.first.name : "#{join_name}.#{o.expressions.first.name}"}
419
426
  else
420
- collector.columns = o.expressions.first
427
+ collector.columns << {:minimum => o.expressions.first}
421
428
  end
422
429
  end
423
430
 
424
431
  def visit_Arel_Nodes_Avg o, collector
425
- collector.operation = :average
432
+ collector.operation = :calculate
433
+
434
+ collector.columns ||= []
426
435
  if o.expressions.first.is_a?(Arel::Attributes::Attribute)
427
436
  relation = o.expressions.first.relation
428
437
  join_name = relation.table_alias || relation.name
429
- collector.columns = join_name ? o.expressions.first.name : "#{join_name}.#{o.expressions.first.name}"
438
+ collector.columns << {:average => join_name ? o.expressions.first.name : "#{join_name}.#{o.expressions.first.name}"}
430
439
  else
431
- collector.columns = o.expressions.first
440
+
441
+ collector.columns << {:average => o.expressions.first}
432
442
  end
433
443
  end
434
444
  #
@@ -0,0 +1,28 @@
1
+ module ActiveRecord
2
+ module Calculations
3
+ def pluck(*column_names)
4
+
5
+ # column_names.map! do |column_name|
6
+ # if column_name.is_a?(Symbol) && attribute_alias?(column_name)
7
+ # attribute_alias(column_name)
8
+ # else
9
+ # column_name.to_s
10
+ # end
11
+ # end
12
+
13
+ if has_include?(column_names.first)
14
+ construct_relation_for_association_calculations.pluck(*column_names)
15
+ else
16
+ relation = spawn
17
+ relation.select_values = column_names.map { |cn|
18
+ columns_hash.key?(cn) ? arel_table[cn] : cn
19
+ }
20
+
21
+ result = klass.connection.exec_query(relation.arel, nil, relation.arel.bind_values + bind_values)
22
+ result.cast_values(klass.column_types)
23
+ end
24
+ end
25
+
26
+ end
27
+ end
28
+
data/lib/sunstone.rb CHANGED
@@ -12,6 +12,7 @@ require 'sunstone/connection'
12
12
  require 'sunstone/exception'
13
13
  require 'ext/active_record/statement_cache'
14
14
  require 'ext/active_record/relation'
15
+ require 'ext/active_record/calculations'
15
16
  require 'ext/active_record/associations/builder/has_and_belongs_to_many'
16
17
 
17
18
  require 'ext/arel/select_manager'
data/sunstone.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "sunstone"
3
- s.version = '1.6.8'
3
+ s.version = '1.7.0'
4
4
  s.authors = ["Jon Bracy"]
5
5
  s.email = ["jonbracy@gmail.com"]
6
6
  s.homepage = "http://sunstonerb.com"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sunstone
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.8
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Bracy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-12 00:00:00.000000000 Z
11
+ date: 2015-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -270,6 +270,7 @@ files:
270
270
  - lib/arel/visitors/sunstone.rb
271
271
  - lib/ext/active_record/associations/builder/has_and_belongs_to_many.rb
272
272
  - lib/ext/active_record/batches.rb
273
+ - lib/ext/active_record/calculations.rb
273
274
  - lib/ext/active_record/finder_methods.rb
274
275
  - lib/ext/active_record/relation.rb
275
276
  - lib/ext/active_record/statement_cache.rb
@@ -279,7 +280,6 @@ files:
279
280
  - lib/sunstone.rb
280
281
  - lib/sunstone/connection.rb
281
282
  - lib/sunstone/exception.rb
282
- - lib/sunstone/model/associations.rb
283
283
  - lib/sunstone/parser.rb
284
284
  - sunstone.gemspec
285
285
  - test/sunstone/connection_test.rb
@@ -305,7 +305,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
305
305
  version: '0'
306
306
  requirements: []
307
307
  rubyforge_project:
308
- rubygems_version: 2.2.2
308
+ rubygems_version: 2.4.5
309
309
  signing_key:
310
310
  specification_version: 4
311
311
  summary: A library for interacting with REST APIs
@@ -1,89 +0,0 @@
1
- module Sunstone
2
- class Model
3
-
4
- module Associations
5
-
6
- extend ActiveSupport::Concern
7
-
8
- attr_accessor :assoications
9
-
10
- def initialize(*)
11
- super
12
- @associations = {}
13
- end
14
-
15
- def reflect_on_associations
16
- self.class.reflect_on_associations
17
- end
18
-
19
- module ClassMethods
20
-
21
- def inherited(subclass)
22
- super
23
- subclass.initialize_associations
24
- end
25
-
26
- def initialize_associations
27
- @associations = {}
28
- end
29
-
30
- def reflect_on_associations
31
- @associations
32
- end
33
-
34
- def belongs_to(name, options = {})
35
- @associations[name] = {
36
- :name => name,
37
- :macro => :belongs_to,
38
- :klass => (options[:class_name] || name).to_s.camelize.constantize,
39
- :foreign_key => (options[:foreign_key] || :"#{name}_id")
40
- }
41
-
42
- attribute(@associations[name][:foreign_key], :integer)
43
- define_association_reader(@associations[name])
44
- define_association_writer(@associations[name])
45
- end
46
-
47
- def has_many(name, options = {})
48
- @associations[name] = {
49
- :name => name,
50
- :macro => :has_many,
51
- :klass => (options[:class_name] || name.to_s.singularize).to_s.camelize.constantize
52
- }
53
-
54
- define_association_reader(@associations[name])
55
- define_association_writer(@associations[name])
56
- end
57
-
58
- def define_association_reader(association)
59
- # if association[:macro] == :belongs_to
60
- class_eval <<-EOV, __FILE__, __LINE__ + 1
61
- def #{association[:name]}
62
- @associations[:#{association[:name]}]
63
- end
64
- EOV
65
- # end
66
- end
67
-
68
- def define_association_writer(association)
69
- if association[:macro] == :belongs_to
70
- class_eval <<-EOV, __FILE__, __LINE__ + 1
71
- def #{association[:name]}=(value)
72
- self.#{association[:foreign_key]} = value.id if !value.nil?
73
- @associations[:#{association[:name]}] = value
74
- end
75
- EOV
76
- elsif association[:macro] == :has_many
77
- class_eval <<-EOV, __FILE__, __LINE__ + 1
78
- def #{association[:name]}=(value)
79
- @associations[:#{association[:name]}] = value
80
- end
81
- EOV
82
- end
83
- end
84
-
85
- end
86
-
87
- end
88
- end
89
- end