mod_factor

How to Use

Add factor model:

rails g model financial_factor key:string amount:decimal owner:references owner_type:string
rails g model property key:string value:text owner:references owner_type:string

Change factor model:

belongs_to :owner, :polymorphic => true

Include ModFactor::Owner to owner:

class SomeModel < ActiveRecord::Base
    include ModFactor::Owner
    factors :financial_factors do
        factor :prop_one
        factor :prop_formula, :generate => true, 
            :formula => proc {1 + 1}
        factor :prop_one_times_ten, :generate=>true, 
            :formula => proc {(owner.prop_one||0) * 10}
        factor :prop_condition, :generate => true,
            :condition => proc {|extra| prop_one},
            :formula => proc {|extra| prop_one || raise("prop one is nil")}
        factor :prop_default, :default=>10
    end

    properties :preferences do
        property :pref_boolean, :boolean, :default=>false
        property :pref_name, :string, :default=>'name'
    end

end

Sample usages:

some_model = SomeModel.new
some_model.prop_one             # nil
some_model.prop_one = 10    # 10
some_model.prop_default     # 10
some_model.calculate
some_model.prop_one_times_ten # 100

some_model.pref_boolean  # true
some_model.pref_boolean = false

some_model.save

Include Math helper:

class SomeModel < ActiveRecord::Base
    include ModFactor::Math
end

Provides following:

some_model.sigma_geometric # http://en.wikipedia.org/wiki/Geometric_progression

Use validates as normal:

validates :prop_one, :presence=>true

Contributing to mod_factor

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
  • Fork the project.
  • Start a feature/bugfix branch.
  • Commit and push until you are happy with your contribution.
  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright

Copyright (c) 2012 Ryan Wong. See LICENSE.txt for further details.