Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Configuration For Plain Value Behavior #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.2.2

* Add options of configuration of `plain_values` in a matter of consistency

## 0.1.1

* groupdate compatibility
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,18 @@ Order.group_by_year(:created_at, last: 5, default_value: {}).calculate_all(:pric
}
```

## Configuration

You can change the behavior of plain values and force to get the function returned

```
CalculateAll.configure do |config|
config.plain_values = false # default true
end
```

Return `[{ cash: { count: 3 }}] instead of [{ cash: 3 }]`

## Installation

Add this line to your application's Gemfile:
Expand Down
19 changes: 18 additions & 1 deletion lib/calculate-all.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
require 'calculate-all/version'
require 'calculate-all/helpers'
require 'calculate-all/querying'
require 'calculate-all/configuration'

module CalculateAll
# Method to aggregate function results in one request
def calculate_all(*function_aliases, **functions, &block)

# If only one function_alias is given, the result can be just a single value
# So return [{ cash: 3 }] instead of [{ cash: { count: 3 }}]
if function_aliases.size == 1 && functions == {}
if CalculateAll.configuration.plain_values && (function_aliases.size == 1 && functions == {})
return_plain_values = true
end

Expand Down Expand Up @@ -74,6 +75,22 @@ def calculate_all(*function_aliases, **functions, &block)
# Return the output array
results
end

class << self
attr_writer :configuration
end

def self.configuration
@configuration ||= Configuration.new
end

def self.reset
@configuration = Configuration.new
end

def self.configure
yield(configuration)
end
end

# Make the calculate_all method available for all ActiveRecord::Relations instances
Expand Down
9 changes: 9 additions & 0 deletions lib/calculate-all/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module CalculateAll
class Configuration
attr_accessor :plain_values

def initialize
@plain_values = true
end
end
end
2 changes: 1 addition & 1 deletion lib/calculate-all/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module CalculateAll
VERSION = '0.2.1'
VERSION = '0.2.2'
end