Class: Tefoji::DeclaredValue

Inherits:
Object
  • Object
show all
Defined in:
lib/tefoji/declared_value.rb

Instance Method Summary collapse

Constructor Details

#initialize(user_value = nil) ⇒ DeclaredValue

Returns a new instance of DeclaredValue.



3
4
5
6
7
8
9
10
# File 'lib/tefoji/declared_value.rb', line 3

def initialize(user_value = nil)
  @value = if user_value.is_a? DeclaredValue
             # prevent recursive DeclaredValues
             user_value.value
           else
             user_value
           end
end

Instance Method Details

#%(other) ⇒ Object



46
47
48
# File 'lib/tefoji/declared_value.rb', line 46

def %(other)
  value % other
end

#falsey?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/tefoji/declared_value.rb', line 56

def falsey?
  simple? && (@value.nil? || @value.empty? || @value.downcase == 'false' || @value == false)
end

#prepend(*prepended_things) ⇒ Object



25
26
27
# File 'lib/tefoji/declared_value.rb', line 25

def prepend(*prepended_things)
  value.prepend(*prepended_things)
end

#simple?Boolean

For most DeclaredValues that aren’t Structs that have their one ‘value` method

Returns:

  • (Boolean)


13
14
15
# File 'lib/tefoji/declared_value.rb', line 13

def simple?
  !@value.respond_to?(:computed_value)
end

#to_aObject



37
38
39
40
41
42
43
44
# File 'lib/tefoji/declared_value.rb', line 37

def to_a
  current = value
  if current.is_a? Array
    current
  else
    [current]
  end
end

#to_iObject



29
30
31
# File 'lib/tefoji/declared_value.rb', line 29

def to_i
  value.to_i
end

#to_sObject



33
34
35
# File 'lib/tefoji/declared_value.rb', line 33

def to_s
  value.to_s
end

#truthy?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/tefoji/declared_value.rb', line 60

def truthy?
  !falsey?
end

#unset?Boolean

When querying @value, we don’t want to invoke @value.computed_value because it may have unwanted side-effects.

Returns:

  • (Boolean)


52
53
54
# File 'lib/tefoji/declared_value.rb', line 52

def unset?
  @value.nil?
end

#valueObject



17
18
19
20
21
22
23
# File 'lib/tefoji/declared_value.rb', line 17

def value
  if simple?
    @value
  else
    @value.computed_value
  end
end