Skip to content

Commit

Permalink
command_builders.rb refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesLavin committed Sep 4, 2013
1 parent 1e4e251 commit 1f048db
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/billing_logic/command_builders/command_builders.rb
Expand Up @@ -11,6 +11,8 @@ def self.money(money)
end

class ProductList
# ProductList.parse returns a dumb array
# You can't add behavior (I tried adding #price)
def self.parse(string, options = {})
if (products = (string =~ /\(([^\(\)]*)\)/) ? $1 : nil)
products.split(/ & /).map do |product_string|
Expand Down Expand Up @@ -78,9 +80,8 @@ def add_action_string
def add_bundle_action_string
product_ids = products.map { |product| product.identifier }.join(' & ')
price ||= products.inject(0){ |k, product| k += product.price; k }
price_string = BuilderHelpers.money(price)
initial_payment_string = total_initial_payment.zero? ? '' : " with initial payment set to $#{BuilderHelpers.money(total_initial_payment)}"
"add (#{product_ids}) @ $#{price_string}#{periodicity_abbrev(products.first.billing_cycle.period)} on #{starts_on.strftime(STRFTIME)}#{initial_payment_string}"
"add (#{product_ids}) @ $#{BuilderHelpers.money(price)}#{periodicity_abbrev(products.first.billing_cycle.period)} on #{starts_on.strftime(STRFTIME)}#{initial_payment_string}"
end

def self.from_string(string, options = {:product_class => ProductStub})
Expand All @@ -92,7 +93,7 @@ def self.from_string(string, options = {:product_class => ProductStub})
$1.to_sym
end
opts[:disable] = !!(string =~ /and disable/)
opts[:starts_on] = (string =~ /on #{BillingLogic::CommandBuilders::DATE_REGEX}/) ? Date.strptime($1, '%m/%d/%y') : (string =~ /now$/) ? Time.now : nil
opts[:starts_on] = (string =~ /on #{BillingLogic::CommandBuilders::DATE_REGEX}/) ? Date.strptime($1, STRFTIME) : (string =~ /now$/) ? Time.now : nil
opts[:products] = ProductList.parse(string, options)

opts[:profile_id] = case opts[:action]
Expand Down

0 comments on commit 1f048db

Please sign in to comment.