Class: Giter8::Conditional

Inherits:
Object
  • Object
show all
Defined in:
lib/giter8/conditional.rb

Overview

Represents a conditional structure in an AST

Constant Summary collapse

TRUTHY_VALUES =
%w[yes y true].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(property, helper, parent, source, line, column) ⇒ Conditional

Returns a new instance of Conditional.



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/giter8/conditional.rb', line 24

def initialize(property, helper, parent, source, line, column)
  @source = source
  @line = line
  @column = column
  @property = property
  @helper = helper
  @parent = parent

  @cond_then = AST.new
  @cond_else_if = AST.new
  @cond_else = AST.new
end

Instance Attribute Details

#columnObject

Returns the value of attribute column.



6
7
8
# File 'lib/giter8/conditional.rb', line 6

def column
  @column
end

#cond_elseObject

Returns the value of attribute cond_else.



6
7
8
# File 'lib/giter8/conditional.rb', line 6

def cond_else
  @cond_else
end

#cond_else_ifObject

Returns the value of attribute cond_else_if.



6
7
8
# File 'lib/giter8/conditional.rb', line 6

def cond_else_if
  @cond_else_if
end

#cond_thenObject

Returns the value of attribute cond_then.



6
7
8
# File 'lib/giter8/conditional.rb', line 6

def cond_then
  @cond_then
end

#helperObject

Returns the value of attribute helper.



6
7
8
# File 'lib/giter8/conditional.rb', line 6

def helper
  @helper
end

#lineObject

Returns the value of attribute line.



6
7
8
# File 'lib/giter8/conditional.rb', line 6

def line
  @line
end

#parentObject

Returns the value of attribute parent.



6
7
8
# File 'lib/giter8/conditional.rb', line 6

def parent
  @parent
end

#propertyObject

Returns the value of attribute property.



6
7
8
# File 'lib/giter8/conditional.rb', line 6

def property
  @property
end

#sourceObject

Returns the value of attribute source.



6
7
8
# File 'lib/giter8/conditional.rb', line 6

def source
  @source
end

Class Method Details

.truthy?(value) ⇒ Boolean

Returns whether a provided value is considered as “truthy”. Giter8 assumes the values “yes”, “y”, and “true” as true. Any other value is assumed to be false.

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
# File 'lib/giter8/conditional.rb', line 14

def self.truthy?(value)
  if value.nil?
    nil
  elsif value.is_a? Literal
    truthy?(value.value)
  elsif value.is_a? String
    TRUTHY_VALUES.any? { |e| e.casecmp(value).zero? }
  end
end

Instance Method Details

#cleanObject

Cleans this Conditional’s branches by calling AST#clean, and returns a copy of this instance.



39
40
41
42
43
44
45
46
47
# File 'lib/giter8/conditional.rb', line 39

def clean
  cond = Conditional.new(@property, @helper, @parent, @source, @line, @column)

  cond.cond_then = @cond_then.clean
  cond.cond_else = @cond_else.clean
  cond.cond_else_if = @cond_else_if.clean

  cond
end

#clean!Object

clean! executes the same operation as #clean, but updates this instance instead of returning a copy.



51
52
53
54
55
# File 'lib/giter8/conditional.rb', line 51

def clean!
  @cond_then = @cond_then.clean
  @cond_else = @cond_else.clean
  @cond_else_if = @cond_else_if.clean
end