relation_to_struct 0.0.1 → 0.0.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: da24825b1afe7727be14670b9c455cb3e37787e8
4
- data.tar.gz: 45d4ee2f132f955835ef6ccd06b313df2e063fd8
3
+ metadata.gz: f46cb4692b014b55ed3b9ef36eac25637fdd67e5
4
+ data.tar.gz: 233671dec235b9d0c0a1bd50625043f0a71b3c1f
5
5
  SHA512:
6
- metadata.gz: 37ec70d13d54eff799e4a0f270c2ed940323b014a029a9c3c28c1a9034190e773b9495cef5d6f097d5c00319daf6b255c55a2dc9a42a9e573930cdf7443763ba
7
- data.tar.gz: a8b3b83a0460f846dca55d202aae7735db3fd16f3fe5ba58e2288bcdba21a2adc03d6086741e9246df774ee24d807dfe1199c7a4e8ad861efb26c98f9456c834
6
+ metadata.gz: 1a7866549d1f975630261f903ffabbf38b5a6940c7811268275c46cca2b53a06502863c28ae7fbf37492cdfef2c9b505e47d4301a8fedf9ff1bc440d37c615af
7
+ data.tar.gz: e9b8d9a58cb1c904311b15284fd79fab9faec140e5068c4889f66b52b472b1c42bea48a4b191b58a170b1f4ff6a4dc3b11a3ea975a23f4c44d9bc6961dc371c6
@@ -0,0 +1,14 @@
1
+ module RelationToStruct::ActiveRecordBaseExtension
2
+ extend ::ActiveSupport::Concern
3
+
4
+ module ClassMethods
5
+ def structs_from_sql(struct_class, sql, binds=[])
6
+ result = connection.select_all(sanitize_sql(sql, nil), "Structs SQL Load", binds)
7
+ result.cast_values().map do |tuple|
8
+ struct_class.new(*tuple)
9
+ end
10
+ end
11
+ end
12
+ end
13
+
14
+ ::ActiveRecord::Base.send(:include, RelationToStruct::ActiveRecordBaseExtension)
@@ -1,3 +1,3 @@
1
1
  module RelationToStruct
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -1,4 +1,5 @@
1
1
  require "relation_to_struct/version"
2
+ require "relation_to_struct/active_record_base_extension"
2
3
  require "relation_to_struct/active_record_relation_extension"
3
4
 
4
5
  module RelationToStruct
@@ -62,12 +62,14 @@ describe RelationToStruct do
62
62
  end
63
63
 
64
64
  describe 'non-model specific querying' do
65
- it 'should allow querying from ActiveRecord' do
66
- pending 'next version'
65
+ it 'ActiveRecord::Base should respond to :structs_from_sql' do
66
+ expect(ActiveRecord::Base.respond_to?(:structs_from_sql)).to eq(true)
67
67
  end
68
68
 
69
- it 'should allow using find_by_sql directly' do
70
- pending 'next version'
69
+ it 'should allow querying with SQL directly' do
70
+ test_struct = Struct.new(:number)
71
+ sql = "SELECT 1 * 23"
72
+ expect(ActiveRecord::Base.structs_from_sql(test_struct, sql)).to eq([test_struct.new(23)])
71
73
  end
72
74
  end
73
75
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: relation_to_struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Coleman
@@ -136,6 +136,7 @@ files:
136
136
  - README.md
137
137
  - Rakefile
138
138
  - lib/relation_to_struct.rb
139
+ - lib/relation_to_struct/active_record_base_extension.rb
139
140
  - lib/relation_to_struct/active_record_relation_extension.rb
140
141
  - lib/relation_to_struct/version.rb
141
142
  - relation_to_struct.gemspec