pluck_map 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pluck_map/attribute.rb +11 -4
- data/lib/pluck_map/presenter.rb +4 -2
- data/lib/pluck_map/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ce4f98bb259e8ba23f7c8d9613efab607f64a28e
|
4
|
+
data.tar.gz: f5b3ee8f6eb67fb100538ae6553f64c04ebe6550
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 418bfb90857663fd41284f42138656c5c6225fcd52a85253ffb9b11a0b87910e33badaf4126f5ad4f0ece309a6ad89d887100b5f7fdb7e58c294ff92576235a9
|
7
|
+
data.tar.gz: 7cc0ab90f39dc99fd391504e31af516be469f622c52e0f2c3ad03afc97361bda726fd12663de68d0d08463c75b19f2e8396b08bda92b0ded45985c63fd34eb12
|
data/lib/pluck_map/attribute.rb
CHANGED
@@ -6,11 +6,17 @@ module PluckMap
|
|
6
6
|
@id = id
|
7
7
|
@selects = Array(options.fetch(:select, id))
|
8
8
|
@name = options.fetch(:as, id)
|
9
|
-
@alias = name.to_s.
|
9
|
+
@alias = name.to_s.tr("_", " ")
|
10
10
|
@block = options[:map]
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
|
12
|
+
if options.key? :value
|
13
|
+
@value = options[:value]
|
14
|
+
@selects = []
|
15
|
+
else
|
16
|
+
raise ArgumentError, "You must select at least one column" if selects.empty?
|
17
|
+
raise ArgumentError, "You must define a block if you are going to select " <<
|
18
|
+
"more than one expression from the database" if selects.length > 1 && !block
|
19
|
+
end
|
14
20
|
end
|
15
21
|
|
16
22
|
def apply(object)
|
@@ -44,6 +50,7 @@ module PluckMap
|
|
44
50
|
# of keys. This method determines which values pertain
|
45
51
|
# to it by figuring out which order its keys were selected in
|
46
52
|
def to_ruby(keys)
|
53
|
+
return @value.inspect if defined?(@value)
|
47
54
|
indexes = self.keys.map { |key| keys.index(key) }
|
48
55
|
return "values[#{indexes[0]}]" if indexes.length == 1 && !block
|
49
56
|
ruby = "values.values_at(#{indexes.join(", ")})"
|
data/lib/pluck_map/presenter.rb
CHANGED
@@ -22,7 +22,9 @@ module PluckMap
|
|
22
22
|
|
23
23
|
def method_missing(attribute_name, *args, &block)
|
24
24
|
return super if initialized?
|
25
|
-
|
25
|
+
options = args.extract_options!
|
26
|
+
options[:value] = args.first if args.any?
|
27
|
+
attributes.push PluckMap::Attribute.new(attribute_name, options)
|
26
28
|
:attribute_added
|
27
29
|
end
|
28
30
|
|
@@ -77,7 +79,7 @@ module PluckMap
|
|
77
79
|
ruby = <<-RUBY
|
78
80
|
def to_h(query)
|
79
81
|
pluck(query) do |results|
|
80
|
-
results.map { |values| values = Array(values); { #{attributes.map { |attribute| "#{attribute.name.inspect} => #{
|
82
|
+
results.map { |values| values = Array(values); { #{attributes.map { |attribute| "#{attribute.name.inspect} => #{attribute.to_ruby(keys)}"}.join(", ")} } }
|
81
83
|
end
|
82
84
|
end
|
83
85
|
RUBY
|
data/lib/pluck_map/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pluck_map
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bob Lail
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-03-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -76,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
76
76
|
version: '0'
|
77
77
|
requirements: []
|
78
78
|
rubyforge_project:
|
79
|
-
rubygems_version: 2.
|
79
|
+
rubygems_version: 2.6.11
|
80
80
|
signing_key:
|
81
81
|
specification_version: 4
|
82
82
|
summary: A DSL for presenting ActiveRecord::Relations without instantiating ActiveRecord
|