Class: RedGrape::PropertyDescription

Inherits:
Object
  • Object
show all
Defined in:
lib/red_grape/property_description.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, default = nil) ⇒ PropertyDescription

Returns a new instance of PropertyDescription.



5
6
7
8
9
# File 'lib/red_grape/property_description.rb', line 5

def initialize(name, type, default=nil)
  @name = name
  @type = type
  @default = default
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



3
4
5
# File 'lib/red_grape/property_description.rb', line 3

def default
  @default
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/red_grape/property_description.rb', line 3

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/red_grape/property_description.rb', line 3

def type
  @type
end

Instance Method Details

#accessible?(val) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
20
# File 'lib/red_grape/property_description.rb', line 11

def accessible?(val)
  case type
  when 'int', 'integer'
    val.is_a? Integer
  when 'float'
    val.is_a? Numeric
  else
    true
  end
end

#convert(val) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/red_grape/property_description.rb', line 33

def convert(val)
  case type
  when 'int', 'integer'
    val.to_i
  when 'float'
    val.to_f
  else
    val
  end
end

#convertable?(val) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
# File 'lib/red_grape/property_description.rb', line 22

def convertable?(val)
  case type
  when 'int', 'integer'
    val =~ /^\d+$/
  when 'float'
    val =~ /^\d+(\.\d+)?$/
  else
    true
  end
end

#has_default?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/red_grape/property_description.rb', line 44

def has_default?
  not @default.nil?
end

#to_sObject



48
49
50
# File 'lib/red_grape/property_description.rb', line 48

def to_s
  "{#{@name}:#{@type}:#{@default}}"
end