Module: Foodsoft::ExpansionVariables
- Defined in:
- app/lib/foodsoft/expansion_variables.rb
Overview
Variable expansions for user-specified texts.
This is used in wiki-pages and the footer, for example, to allow foodcoops to show dynamic information in the text.
Plugins can modify the variables by means of the ‘#variables` accessor. Please be thoughful when choosing names as to avoid collisions. Do not put non-public info in variables.
Constant Summary collapse
- ACTIVE_MONTHS =
3
Class Method Summary collapse
-
.active_ordergroup_count ⇒ Number
Number of ordergroups that have been active in the past 3 months.
-
.active_supplier_count ⇒ Number
Number of suppliers that has been ordered from in the past 3 months.
-
.active_suppliers ⇒ String
Comma-separated list of suppliers that has been ordered from in the past 3 months.
-
.expand(str, options = {}) ⇒ String
Expand variables in a string.
-
.get(var) ⇒ String
Return expanded variable.
Instance Method Summary collapse
-
#variables ⇒ Hash
Variables and their values.
Class Method Details
.active_ordergroup_count ⇒ Number
Returns Number of ordergroups that have been active in the past 3 months.
63 64 65 66 67 |
# File 'app/lib/foodsoft/expansion_variables.rb', line 63 def self.active_ordergroup_count GroupOrder .where('updated_on > ?', ACTIVE_MONTHS.months.ago) .select(:ordergroup_id).distinct.count end |
.active_supplier_count ⇒ Number
Returns Number of suppliers that has been ordered from in the past 3 months.
70 71 72 73 74 |
# File 'app/lib/foodsoft/expansion_variables.rb', line 70 def self.active_supplier_count Order .where('starts > ?', ACTIVE_MONTHS.months.ago) .select(:supplier_id).distinct.count end |
.active_suppliers ⇒ String
Returns Comma-separated list of suppliers that has been ordered from in the past 3 months.
77 78 79 80 81 82 |
# File 'app/lib/foodsoft/expansion_variables.rb', line 77 def self.active_suppliers Supplier.joins(:orders) .where('orders.starts > ?', ACTIVE_MONTHS.months.ago) .order(:name).select(:name).distinct .map(&:name).join(', ') end |
.expand(str, options = {}) ⇒ String
Expand variables in a string
56 57 58 59 60 |
# File 'app/lib/foodsoft/expansion_variables.rb', line 56 def self.(str, = {}) str.gsub(/{{([._a-zA-Z0-9]+)}}/) do [::Regexp.last_match(1)] || get(::Regexp.last_match(1)) end end |
.get(var) ⇒ String
Return expanded variable
47 48 49 50 |
# File 'app/lib/foodsoft/expansion_variables.rb', line 47 def self.get(var) s = @variables[var.to_s] s.respond_to?(:call) ? s.call : s.to_s end |
Instance Method Details
#variables ⇒ Hash
Returns Variables and their values.
14 |
# File 'app/lib/foodsoft/expansion_variables.rb', line 14 cattr_accessor :variables |