Class: ParamsReady::Parameter::State

Inherits:
StructParameter show all
Extended by:
Forwardable, Query::Relation::PageAccessors
Defined in:
lib/params_ready/parameter/state.rb

Constant Summary

Constants inherited from AbstractStructParameter

AbstractStructParameter::EMPTY_HASH

Instance Attribute Summary

Attributes inherited from AbstractParameter

#definition

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Query::Relation::PageAccessors

extended, page_accessor

Methods included from Marshaller::ParameterModule

#marshal

Methods inherited from AbstractStructParameter

#[], #[]=, #find_in_hash, #for_frontend, #for_model, #for_output, #wrap_output

Methods included from ComplexParameter

#update_child

Methods inherited from Parameter

#allows_undefined?, #definite_default?, #eligible_for_output?, #find_in_hash, #format, #format_self_permitted, #freeze, #hash, #hash_key, #initialize, #inspect_content, #is_default?, #is_definite?, #is_nil?, #is_undefined?, #memo, #memo!, #nil_default?, #populate_other, #set_from_input, #set_value, #to_hash_if_eligible, #unwrap, #unwrap_or, #wrap_output

Methods inherited from AbstractParameter

#==, #dup, #initialize, #inspect, intent_for_children, #match?, #populate, #to_hash, #update_if_applicable, #update_in

Methods included from Extensions::Freezer

#freeze_variable, #freeze_variables, #variables_to_freeze

Methods included from FromHash

#set_from_hash

Methods included from Extensions::Freezer::InstanceMethods

#freeze

Constructor Details

This class inherits a constructor from ParamsReady::Parameter::Parameter

Class Method Details

.relation_delegator(name) ⇒ Object



17
18
19
20
21
22
# File 'lib/params_ready/parameter/state.rb', line 17

def self.relation_delegator(name)
  define_method name do |relation_name, *args|
    relation(relation_name).send(name, *args)
  end
  ruby2_keywords name
end

Instance Method Details

#after_page(relation_name, keyset) ⇒ Object



78
79
80
81
# File 'lib/params_ready/parameter/state.rb', line 78

def after_page(relation_name, keyset)
  value = relation(relation_name).pagination.after_page_value(keyset)
  update_in(value, [relation_name, :pagination])
end

#before_page(relation_name, keyset) ⇒ Object



73
74
75
76
# File 'lib/params_ready/parameter/state.rb', line 73

def before_page(relation_name, keyset)
  value = relation(relation_name).pagination.before_page_value(keyset)
  update_in(value, [relation_name, :pagination])
end

#current_pageObject



46
47
48
# File 'lib/params_ready/parameter/state.rb', line 46

def current_page
  page
end

#first_page(relation_name) ⇒ Object



50
51
52
53
# File 'lib/params_ready/parameter/state.rb', line 50

def first_page(relation_name)
  value = relation(relation_name).pagination.first_page_value
  update_in(value, [relation_name, :pagination])
end

#last_page(relation_name, *args, **opts) ⇒ Object



55
56
57
58
59
# File 'lib/params_ready/parameter/state.rb', line 55

def last_page(relation_name, *args, **opts)
  value = relation(relation_name).pagination.last_page_value(*args, **opts)
  return if value.nil?
  update_in(value, [relation_name, :pagination])
end

#limited_at(relation_name, limit) ⇒ Object



83
84
85
86
# File 'lib/params_ready/parameter/state.rb', line 83

def limited_at(relation_name, limit)
  limit_key = relation(relation_name).pagination.limit_key
  update_in(limit, [relation_name, :pagination, limit_key])
end

#next_page(relation_name, delta = 1, count: nil) ⇒ Object



67
68
69
70
71
# File 'lib/params_ready/parameter/state.rb', line 67

def next_page(relation_name, delta = 1, count: nil)
  value = relation(relation_name).pagination.next_page_value(delta, count: count)
  return if value.nil?
  update_in(value, [relation_name, :pagination])
end

#page(relation_name = nil, delta = 0, count: nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/params_ready/parameter/state.rb', line 34

def page(relation_name = nil, delta = 0, count: nil)
  if delta == 0
    clone
  else
    raise ParamsReadyError, "Relation must be specified when delta is not 0" if relation_name.nil?

    return nil unless relation(relation_name).pagination.can_yield_page? delta, count: count
    new_offset = relation(relation_name).new_offset(delta)
    update_in(new_offset, [relation_name, :pagination, 0])
  end
end

#previous_page(relation_name, delta = 1) ⇒ Object



61
62
63
64
65
# File 'lib/params_ready/parameter/state.rb', line 61

def previous_page(relation_name, delta = 1)
  value = relation(relation_name).pagination.previous_page_value(delta)
  return if value.nil?
  update_in(value, [relation_name, :pagination])
end

#relation(name) ⇒ Object

Raises:



12
13
14
15
# File 'lib/params_ready/parameter/state.rb', line 12

def relation(name)
  raise ParamsReadyError, "Relation not defined: '#{name}'" unless relations.include? name
  child(name)
end

#reordered(relation_name, column, direction) ⇒ Object



94
95
96
97
98
# File 'lib/params_ready/parameter/state.rb', line 94

def reordered(relation_name, column, direction)
  new_order = relation(relation_name).ordering.reordered_value(column, direction)
  reordered = update_in(new_order, [relation_name, :ordering])
  reordered.first_page(relation_name)
end

#toggled_order(relation_name, column) ⇒ Object



88
89
90
91
92
# File 'lib/params_ready/parameter/state.rb', line 88

def toggled_order(relation_name, column)
  new_order = relation(relation_name).ordering.toggled_order_value(column)
  toggled = update_in(new_order, [relation_name, :ordering])
  toggled.first_page(relation_name)
end