Module: Jekyll::Filters::Assertion

Defined in:
lib/jekyll/filters/assertion.rb

Instance Method Summary collapse

Instance Method Details

#blank(input) ⇒ Object

Checks if input value is empty.

post.title | blank } => false



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/jekyll/filters/assertion.rb', line 113

def blank(input)
  case input
  when TrueClass then false
  when FalseClass then true
  when NilClass then true
  when Integer then false
  when Float then false
  when Time then false
  when String
    require 'fast_blank'
    input.blank?
  when Array then input.compact.empty?
  when Hash then input.compact.empty?
  else input.respond_to?(:empty?) ? input.empty? : !!!input
  end
end

#equals(input, comparison) ⇒ TrueClass|FalseClass

Compares two values.

Example usage:

item.url | equals: page.url }

Parameters:

  • (Any)
  • (Any)

Returns:

  • (TrueClass|FalseClass)


15
16
17
# File 'lib/jekyll/filters/assertion.rb', line 15

def equals(input, comparison)
  input == comparison
end

#gt(input, comparison) ⇒ TrueClass|FalseClass

Greater than

Parameters:

  • (Integer|Float)
  • (Integer|Float)

Returns:

  • (TrueClass|FalseClass)


29
30
31
# File 'lib/jekyll/filters/assertion.rb', line 29

def gt(input, comparison)
  input > comparison
end

#gte(input, comparison) ⇒ TrueClass|FalseClass

Greater than or equal

Parameters:

  • (Integer|Float)
  • (Integer|Float)

Returns:

  • (TrueClass|FalseClass)


47
48
49
# File 'lib/jekyll/filters/assertion.rb', line 47

def gte(input, comparison)
  input >= comparison
end

#input_if(input, value) ⇒ Object

Returns the input when value is not empty and truthy

post.url | input_if: true } => ‘url/to/post/’



101
102
103
# File 'lib/jekyll/filters/assertion.rb', line 101

def input_if(input, value)
  input if present(value) && value
end

#input_unless(input, value) ⇒ Object

post.url | input_unless: false } => nil



106
107
108
# File 'lib/jekyll/filters/assertion.rb', line 106

def input_unless(input, value)
  input unless present(value) && value
end

#lt(input, comparison) ⇒ TrueClass|FalseClass

Less than

Parameters:

  • (Integer|Float)
  • (Integer|Float)

Returns:

  • (TrueClass|FalseClass)


38
39
40
# File 'lib/jekyll/filters/assertion.rb', line 38

def lt(input, comparison)
  input < comparison
end

#lte(input, comparison) ⇒ TrueClass|FalseClass

Less than or equal

Parameters:

  • (Integer|Float)
  • (Integer|Float)

Returns:

  • (TrueClass|FalseClass)


56
57
58
# File 'lib/jekyll/filters/assertion.rb', line 56

def lte(input, comparison)
  input <= comparison
end

#not(input, comparison) ⇒ Object

The two values must be different



20
21
22
# File 'lib/jekyll/filters/assertion.rb', line 20

def not(input, comparison)
  input != comparison
end

#presence(input) ⇒ Object

Return the input if it’s not blank

post.title | presence } => ‘the post title’



140
141
142
# File 'lib/jekyll/filters/assertion.rb', line 140

def presence(input)
  input if present input
end

#present(input) ⇒ Object

The opposite of blank

post.title | present } => true



133
134
135
# File 'lib/jekyll/filters/assertion.rb', line 133

def present(input)
  !blank(input)
end

#ternary(input, value, alternative) ⇒ Any

Ternary operator. If the input value is truthy, return first argument, otherwise returns the last.

Example usage:

assign active = item.url | equals: page.url % active | ternary: ‘active’, ” }

Parameters:

  • (Any)
  • (Any)
  • (Any)

Returns:

  • (Any)


72
73
74
75
76
77
78
# File 'lib/jekyll/filters/assertion.rb', line 72

def ternary(input, value, alternative)
  if present(input) && input
    value
  else
    alternative
  end
end

#value_if(input, value) ⇒ Object

Returns the value when the input is not empty or falsey

If tags have something:

post.tags | value_if: “some tags” } => “some tags”



85
86
87
# File 'lib/jekyll/filters/assertion.rb', line 85

def value_if(input, value)
  value if present(input) && input
end

#value_unless(input, value) ⇒ Object

Returns the value when the input is empty or falsey

If tags are empty:

post.tags | value_unless: “no tags” } => nil



94
95
96
# File 'lib/jekyll/filters/assertion.rb', line 94

def value_unless(input, value)
  value unless present(input) && input
end