module Subledger::Store
Attributes
auth_key[R]
Public Class Methods
new(args)
click to toggle source
# File lib/subledger/store.rb, line 26 def initialize args @auth_key = args[:auth_key] setup end
Public Instance Methods
add_initial_controls_for(org)
click to toggle source
# File lib/subledger/store.rb, line 95 def add_initial_controls_for org client = org.client key = read client.keys :id => auth_key.id identity = key.identity # TODO OrgIdentities table is not being updated yet create client.active_controls :org => org, :identity => identity, :verbs => 'GET|PATCH', :path => "/#{API_VERSION}/orgs/#{org.id}", :mode => :allow create client.active_controls :org => org, :identity => identity, :verbs => 'POST|GET|PATCH', :path => "/#{API_VERSION}/orgs/#{org.id}/*", :mode => :allow end
add_initial_controls_to(identity)
click to toggle source
# File lib/subledger/store.rb, line 123 def add_initial_controls_to identity client = identity.client client.active_controls.create :identity => identity, :verbs => 'GET|PATCH', :path => "/#{API_VERSION}/identities/#{identity.id}", :mode => :allow client.active_controls.create :identity => identity, :verbs => 'GET|POST', :path => "/#{API_VERSION}/identities/#{identity.id}/*", :mode => :allow client.active_controls.create :identity => identity, :verbs => 'POST', :path => "/#{API_VERSION}/orgs", :mode => :allow end
add_key_to(identity)
click to toggle source
# File lib/subledger/store.rb, line 117 def add_key_to identity key = identity.client.active_keys :identity => identity create key end
attributes()
click to toggle source
# File lib/subledger/store.rb, line 32 def attributes { :auth_key => auth_key } end
backup_exists?(anchor)
click to toggle source
# File lib/subledger/store.rb, line 81 def backup_exists? anchor Celluloid::Actor[:backup_creators].exists? :anchor => anchor end
collect(args)
click to toggle source
# File lib/subledger/store.rb, line 36 def collect args collection_name = args[:collection_name] action = args[:action] limit = args[:limit] no_balances = args[:no_balances] begin if limit.nil? or limit < 1 or limit > 100 raise CollectError, ':limit must be 1-100' end collected = send action, args if collection_name == :account_lines and not collected.empty? if no_balances collected.each do |line| line.send :balance=, nil end else args.merge! :account_lines => collected set_account_line_balances args end end collected rescue Exception => e raise CollectError, "Unable to collect #{collection_name}: #{e}" end end
create_backup(anchor)
click to toggle source
# File lib/subledger/store.rb, line 75 def create_backup anchor Celluloid::Actor[:backup_creators].async.create :anchor => anchor anchor end
create_identity(identity)
click to toggle source
# File lib/subledger/store.rb, line 85 def create_identity identity create identity key = add_key_to identity add_initial_controls_to identity return identity, key end
first_and_last_line(args)
click to toggle source
# File lib/subledger/store.rb, line 142 def first_and_last_line args account = args[:account] # eventually consistent means we could get one, but not two... # zero is fine, it means the account has no lines! begin lines = [ ] lines += first_or_last_line( args.merge! :effective_at => first_second, :action => :starting ) lines += first_or_last_line( args.merge! :effective_at => last_second, :action => :ending ) end while lines.length == 1 and sleep 0.025 lines end
raise_unless_bucket_name_valid(args)
click to toggle source
# File lib/subledger/store.rb, line 67 def raise_unless_bucket_name_valid args bucket_name = args[:bucket_name] unless bucket_name.nil? Celluloid::Actor[:backup_bucket_validators].validate args end end
Private Instance Methods
balance_for(account_line)
click to toggle source
# File lib/subledger/store.rb, line 200 def balance_for account_line client = account_line.client account = account_line.account effective_at = account_line.effective_at previous_ms = previous_ms_for effective_at balance = account_balance :store => self, :client => client, :account => account, :at => previous_ms account.lines( :action => :after, :effective_at => previous_ms, :no_balances => true ) do |line| balance += line break if line == account_line end balance end
first_or_last_line(args)
click to toggle source
# File lib/subledger/store.rb, line 164 def first_or_last_line args account = args[:account] client = args[:client] ||= account.client anchor = account.line :effective_at => args[:effective_at] collect args.merge! :collection_name => :account_lines, :anchor => anchor, :limit => 1 end
previous_ms_for(effective_at)
click to toggle source
# File lib/subledger/store.rb, line 224 def previous_ms_for effective_at effective_at_sec = BigDecimal effective_at.tv_sec effective_at_usec = BigDecimal effective_at.tv_usec effective_at_frac = effective_at_usec / 1_000_000 effective_at_bd = effective_at_sec + effective_at_frac previous_ms_bd = effective_at_bd - BigDecimal( '.001' ) Time.at( previous_ms_bd ).utc end
set_account_line_balance(account_line)
click to toggle source
# File lib/subledger/store.rb, line 196 def set_account_line_balance account_line account_line.send :balance=, balance_for( account_line ) end
set_account_line_balances(args)
click to toggle source
# File lib/subledger/store.rb, line 175 def set_account_line_balances args if [ :before, :ending, :preceding ].include? args[:action] account_lines = args[:account_lines].reverse else account_lines = args[:account_lines] end first_line = account_lines.first balance = balance_for first_line first_line.send :balance=, balance # Beware insane Ruby array indexing on line below account_lines[1..-1].each do |account_line| balance += account_line account_line.send :balance=, balance end end