Class: MudratProjector::Account
- Inherits:
-
Object
- Object
- MudratProjector::Account
- Defined in:
- lib/mudrat_projector/account.rb
Constant Summary collapse
- TYPES =
i(asset expense liability revenue equity)
Instance Attribute Summary collapse
-
#open_date ⇒ Object
readonly
Returns the value of attribute open_date.
-
#parent_id ⇒ Object
readonly
Returns the value of attribute parent_id.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #add_entry(entry) ⇒ Object
- #balance ⇒ Object
- #close! ⇒ Object
- #closed? ⇒ Boolean
- #create_child(params = {}) ⇒ Object
-
#initialize(params = {}) ⇒ Account
constructor
A new instance of Account.
- #parent? ⇒ Boolean
- #serialize ⇒ Object
- #tag?(tag_name) ⇒ Boolean
Constructor Details
#initialize(params = {}) ⇒ Account
Returns a new instance of Account.
7 8 9 10 11 12 13 14 15 |
# File 'lib/mudrat_projector/account.rb', line 7 def initialize params = {} @entries = [] @open_date = params[:open_date] || ABSOLUTE_START @offset = 0 @opening_balance = params[:opening_balance] || 0 @parent_id = params[:parent_id] || nil = params[:tags] || [] @type = params.fetch :type end |
Instance Attribute Details
#open_date ⇒ Object (readonly)
Returns the value of attribute open_date.
5 6 7 |
# File 'lib/mudrat_projector/account.rb', line 5 def open_date @open_date end |
#parent_id ⇒ Object (readonly)
Returns the value of attribute parent_id.
5 6 7 |
# File 'lib/mudrat_projector/account.rb', line 5 def parent_id @parent_id end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
5 6 7 |
# File 'lib/mudrat_projector/account.rb', line 5 def end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
5 6 7 |
# File 'lib/mudrat_projector/account.rb', line 5 def type @type end |
Instance Method Details
#add_entry(entry) ⇒ Object
17 18 19 20 |
# File 'lib/mudrat_projector/account.rb', line 17 def add_entry entry @entries.push entry @offset += entry.delta end |
#balance ⇒ Object
22 23 24 |
# File 'lib/mudrat_projector/account.rb', line 22 def balance @opening_balance + @offset end |
#close! ⇒ Object
26 27 28 29 30 |
# File 'lib/mudrat_projector/account.rb', line 26 def close! freeze return self if closed? self.class.new serialize end |
#closed? ⇒ Boolean
32 33 34 |
# File 'lib/mudrat_projector/account.rb', line 32 def closed? @entries.empty? end |
#create_child(params = {}) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/mudrat_projector/account.rb', line 36 def create_child params = {} new_params = serialize new_params.merge!( opening_balance: params[:opening_balance], parent_id: params.fetch(:parent_id), tags: ( | Array(params[:tags])), ) self.class.new new_params end |
#parent? ⇒ Boolean
46 47 48 |
# File 'lib/mudrat_projector/account.rb', line 46 def parent? parent_id.nil? ? false : true end |
#serialize ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/mudrat_projector/account.rb', line 54 def serialize hash = { opening_balance: balance } i(open_date parent_id type).each do |attr| value = public_send attr unless default_value? attr, value hash[attr] = value unless Array(value).empty? end end hash end |
#tag?(tag_name) ⇒ Boolean
50 51 52 |
# File 'lib/mudrat_projector/account.rb', line 50 def tag? tag_name .include? tag_name end |