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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 77e7669a17721684395128655dda6e8da49d30b1
4
- data.tar.gz: 355d90c4ed3e0dfe1667c1666a9c32c7ea573d42
3
+ metadata.gz: ce4f98bb259e8ba23f7c8d9613efab607f64a28e
4
+ data.tar.gz: f5b3ee8f6eb67fb100538ae6553f64c04ebe6550
5
5
  SHA512:
6
- metadata.gz: 05ed5427a51748878512c6347f111890365264c011e192eea8e59fd2474dc175f935ee6434b98786603c16641a8bf4a7c6c5e9c5c3a77c203c0a1f902eb10b29
7
- data.tar.gz: 5d3f898e451f9aea4ab28d237e86a38e7edf3ae353e00672a89451f699bd222e4456e187889e180eb33640e6846d92a69410d9c52c37cf31b3750ab0ebdb6bc2
6
+ metadata.gz: 418bfb90857663fd41284f42138656c5c6225fcd52a85253ffb9b11a0b87910e33badaf4126f5ad4f0ece309a6ad89d887100b5f7fdb7e58c294ff92576235a9
7
+ data.tar.gz: 7cc0ab90f39dc99fd391504e31af516be469f622c52e0f2c3ad03afc97361bda726fd12663de68d0d08463c75b19f2e8396b08bda92b0ded45985c63fd34eb12
@@ -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.gsub('_', ' ')
9
+ @alias = name.to_s.tr("_", " ")
10
10
  @block = options[:map]
11
- raise ArgumentError, "You must select at least one column" if selects.empty?
12
- raise ArgumentError, "You must define a block if you are going to select " <<
13
- "more than one expression from the database" if selects.length > 1 && !block
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(", ")})"
@@ -22,7 +22,9 @@ module PluckMap
22
22
 
23
23
  def method_missing(attribute_name, *args, &block)
24
24
  return super if initialized?
25
- attributes.push PluckMap::Attribute.new(attribute_name, args.extract_options!)
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} => #{(attribute.to_ruby(keys))}"}.join(", ")} } }
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
@@ -1,3 +1,3 @@
1
1
  module PluckMapPresenter
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
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.1.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: 2015-11-07 00:00:00.000000000 Z
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.4.8
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