Class: Cheer::Argument

Inherits:
Object
  • Object
show all
Defined in:
lib/cheer/argument.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Argument

Returns a new instance of Argument.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/cheer/argument.rb', line 6

def initialize(args = {})
  @model_klass  = args[:model_klass]
  @column_name  = parse_column_name(args[:column_name])
  @sort_order   = parse_sort_order(args[:sort_order])
  @around_limit = parse_around_limit(args[:around_limit])

  validate_column_name
  validate_sort_order
  validate_around_limit

  merge_sort_order
end

Instance Attribute Details

#around_limitObject (readonly)

Returns the value of attribute around_limit.



4
5
6
# File 'lib/cheer/argument.rb', line 4

def around_limit
  @around_limit
end

#column_nameObject (readonly)

Returns the value of attribute column_name.



4
5
6
# File 'lib/cheer/argument.rb', line 4

def column_name
  @column_name
end

#model_klassObject (readonly)

Returns the value of attribute model_klass.



4
5
6
# File 'lib/cheer/argument.rb', line 4

def model_klass
  @model_klass
end

#sort_orderObject (readonly)

Returns the value of attribute sort_order.



4
5
6
# File 'lib/cheer/argument.rb', line 4

def sort_order
  @sort_order
end

Instance Method Details

#column_exists_in_db?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/cheer/argument.rb', line 46

def column_exists_in_db?
  model_klass.column_names.include?(column_name)
end

#merge_sort_orderObject



50
51
52
53
54
# File 'lib/cheer/argument.rb', line 50

def merge_sort_order
  # Ensure 'id' is always present in Sort Order.
  @sort_order | ['id']
  @sort_order = @sort_order.join(',')
end

#parse_around_limit(around_limit) ⇒ Object



27
28
29
# File 'lib/cheer/argument.rb', line 27

def parse_around_limit(around_limit)
  around_limit ? around_limit : 2
end

#parse_column_name(column_name) ⇒ Object



19
20
21
# File 'lib/cheer/argument.rb', line 19

def parse_column_name(column_name)
  [String, Symbol].include?(column_name.class) ? column_name.to_s : ''
end

#parse_sort_order(sort_order) ⇒ Object



23
24
25
# File 'lib/cheer/argument.rb', line 23

def parse_sort_order(sort_order)
  sort_order.present? ? sort_order : ['id']
end

#validate_around_limitObject



36
37
38
39
# File 'lib/cheer/argument.rb', line 36

def validate_around_limit
  return if around_limit.to_i > 0
  raise Error::InvalidAroundLimit
end

#validate_column_nameObject



31
32
33
34
# File 'lib/cheer/argument.rb', line 31

def validate_column_name
  return if column_name.present? && column_exists_in_db?
  raise Error::InvalidColumnName
end

#validate_sort_orderObject



41
42
43
44
# File 'lib/cheer/argument.rb', line 41

def validate_sort_order
  return if sort_order.is_a?(Array)
  raise Error::InvalidSortOrder
end