Class: Settings::Variable

Inherits:
Object
  • Object
show all
Defined in:
lib/character/settings.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(group, name, attrs) ⇒ Variable

Returns a new instance of Variable.



59
60
61
62
63
64
65
# File 'lib/character/settings.rb', line 59

def initialize(group, name, attrs)
  @group         = group
  @name          = name
  @type          = attrs['type']          || 'string'
  @description   = attrs['description']   || ''
  @default_value = attrs['default_value'] || ''
end

Instance Attribute Details

#default_valueObject

Returns the value of attribute default_value.



53
54
55
# File 'lib/character/settings.rb', line 53

def default_value
  @default_value
end

#descriptionObject

Returns the value of attribute description.



53
54
55
# File 'lib/character/settings.rb', line 53

def description
  @description
end

#groupObject

Returns the value of attribute group.



53
54
55
# File 'lib/character/settings.rb', line 53

def group
  @group
end

#nameObject

Returns the value of attribute name.



53
54
55
# File 'lib/character/settings.rb', line 53

def name
  @name
end

#typeObject

Returns the value of attribute type.



53
54
55
# File 'lib/character/settings.rb', line 53

def type
  @type
end

Instance Method Details

#stored_objectObject



67
68
69
# File 'lib/character/settings.rb', line 67

def stored_object
  Character::Settings::Variable.find_or_create_by(name: @name, group: @group)
end

#valueObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/character/settings.rb', line 71

def value
  object = stored_object
  value  = object.value || @default_value

  if @type == 'file'
    if object.has_file_uploaded?
      # return uploaded file
      return object.file.to_s
    elsif value.include? '//'
      # return direct link to file
      return value
    elsif value.empty?
      # return empty string
      return value
    else
      # return rails asset
      return ActionController::Base.helpers.asset_path(value)
    end

  elsif @type == 'integer'
    return value.to_i

  elsif @type == 'float'
    return value.to_f

  else # string
    return value
  end
end