module Subledger::Domain::Roles::Collectable::CollectableClass

Constants

ACTIONS
ACTION_MAP
COLLECTABLE_BY
MAX_LIMIT

Public Instance Methods

collect(args, &block) click to toggle source
# File lib/subledger/domain/roles/collectable.rb, line 11
def collect args, &block
  limit = args[:limit] ||= 25 unless block_given?

  validate args

  store           = args[:store]
  client          = args[:client]
  anchor          = args[:anchor]
  action          = args[:action]
  collection_name =  CollectionName.with_state( args.
                                                  merge :klass => self )

  args[:collection_name] = collection_name

  unless anchor
    anchor = args[:anchor] = client.send collection_name, args

    anchor.read if [:preceding, :following].include? action
  end

  validate_criterion args

  if block_given?
    collect_with_block args, &block
  else
    store.collect args
  end
end

Private Instance Methods

calculated_limit(limit,total) click to toggle source
# File lib/subledger/domain/roles/collectable.rb, line 94
def calculated_limit limit,total
  new_limit = limit - total

  new_limit < MAX_LIMIT ? new_limit : MAX_LIMIT
end
collect_with_block(args) { |item| ... } click to toggle source
# File lib/subledger/domain/roles/collectable.rb, line 51
def collect_with_block args
  store  = args[:store]
  anchor = args[:anchor]
  action = args[:action]
  limit  = args[:limit]

  next_action = ACTION_MAP[ action ]

  total = 0

  store_limit = first_limit limit

  while not ( finished_collecting? limit, total ) and
        not ( collected = store.collect(
                            args.merge!(
                              :anchor => anchor,
                              :action => action,
                              :limit  => store_limit ) ) ).empty?

    collected.each do |item|
      yield item

      total += 1
    end

    action      = next_action
    anchor      = collected.last
    store_limit = next_limit limit, total
  end
end
finished_collecting?(limit, total) click to toggle source
# File lib/subledger/domain/roles/collectable.rb, line 82
def finished_collecting? limit, total
  not limit.nil? and total >= limit
end
first_limit(limit) click to toggle source
# File lib/subledger/domain/roles/collectable.rb, line 86
def first_limit limit
  ( limit.nil? or limit > MAX_LIMIT ) ? MAX_LIMIT : limit
end
next_limit(limit, total) click to toggle source
# File lib/subledger/domain/roles/collectable.rb, line 90
def next_limit limit, total
  limit.nil? ? MAX_LIMIT : calculated_limit( limit, total )
end
validate(args) click to toggle source
# File lib/subledger/domain/roles/collectable.rb, line 100
def validate args
  validate_action    args
  validate_limit     args
  validate_state     args
end
validate_action(args) click to toggle source
# File lib/subledger/domain/roles/collectable.rb, line 113
def validate_action args
  unless ACTIONS.include? args[:action]
    raise CollectableError, ':action must be :before, :ending, :starting, :after, :preceding, or :following'
  end
end
validate_criterion(args) click to toggle source
# File lib/subledger/domain/roles/collectable.rb, line 162
def validate_criterion args
  anchor          = args[ :anchor ]
  collection_name = args[ :collection_name ]
  required_field  = COLLECTABLE_BY[ collection_name ]

  if anchor.send( required_field ).nil?
    raise CollectableError, "#{required_field.inspect} is required"
  end
end
validate_limit(args) click to toggle source
# File lib/subledger/domain/roles/collectable.rb, line 119
def validate_limit args
  limit = args[:limit]

  if not limit.nil? and limit < 1
    raise CollectableError, ':limit must be greater than zero or nil'
  end
end
validate_state(args) click to toggle source
# File lib/subledger/domain/roles/collectable.rb, line 127
def validate_state args
  state = args[:state]

  if state.nil?
    raise CollectableError, ':state is required'
  end
end