Class: LookupKey

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Authorization
Defined in:
app/models/lookup_key.rb

Constant Summary collapse

KEY_TYPES =
%w( string boolean integer real array hash yaml json )
VALIDATOR_TYPES =
%w( regexp list )
TRUE_VALUES =
[true, 1, '1', 't', 'T', 'true', 'TRUE', 'on', 'ON', 'yes', 'YES', 'y', 'Y'].to_set
FALSE_VALUES =
[false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF', 'no', 'NO', 'n', 'N'].to_set
KEY_DELM =
","
EQ_DELM =
"="

Instance Method Summary collapse

Methods included from Authorization

#enforce_create_permissions, #enforce_destroy_permissions, #enforce_edit_permissions, #enforce_permissions, included, #permission_failed?

Instance Method Details

#as_json(options = {}) ⇒ Object


98
99
100
101
# File 'app/models/lookup_key.rb', line 98

def as_json(options={})
  options ||= {}
  super({:only => [:key, :is_param, :required, :override, :description, :default_value, :id]}.merge(options))
end

#cast_validate_value(value) ⇒ Object

Returns the casted value, or raises a TypeError


92
93
94
95
96
# File 'app/models/lookup_key.rb', line 92

def cast_validate_value value
  method = "cast_value_#{key_type}".to_sym
  return value unless self.respond_to? method, true
  self.send(method, value) rescue raise TypeError
end

#default_value_before_type_castObject


74
75
76
# File 'app/models/lookup_key.rb', line 74

def default_value_before_type_cast
  value_before_type_cast default_value
end

#param_classObject


19
20
21
# File 'app/models/lookup_key.rb', line 19

def param_class
  param_classes.first
end

#pathObject


63
64
65
66
# File 'app/models/lookup_key.rb', line 63

def path
  path = read_attribute(:path)
  path.blank? ? array2path(Setting["Default_variables_Lookup_Path"]) : path
end

#path=(v) ⇒ Object


68
69
70
71
72
# File 'app/models/lookup_key.rb', line 68

def path=(v)
  return unless v
  using_default = v.tr("\r","") == array2path(Setting["Default_variables_Lookup_Path"])
  write_attribute(:path, using_default ? nil : v)
end

#path_elementsObject


103
104
105
106
107
108
109
# File 'app/models/lookup_key.rb', line 103

def path_elements
  path.split.map do |paths|
    paths.split(KEY_DELM).map do |element|
      element
    end
  end
end

#to_paramObject


55
56
57
# File 'app/models/lookup_key.rb', line 55

def to_param
  "#{id}-#{key}"
end

#to_sObject


59
60
61
# File 'app/models/lookup_key.rb', line 59

def to_s
  key
end

#value_before_type_cast(val) ⇒ Object


78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/models/lookup_key.rb', line 78

def value_before_type_cast val
  case key_type.to_sym
    when :json
      val = JSON.dump val
    when :yaml, :hash
      val = YAML.dump val
      val.sub!(/\A---\s*$\n/, '')
    when  :array
      val = val.inspect
  end unless key_type.blank?
  val
end