Class: Jaspion::Kilza::Property

Inherits:
Object
  • Object
show all
Defined in:
lib/jaspion/kilza/property.rb

Overview

Represents a single Class property

Direct Known Subclasses

Java::Property, Objc::Property, Swift::Property

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, array, key = '') ⇒ Property

Returns a new instance of Property.



29
30
31
32
33
34
35
36
# File 'lib/jaspion/kilza/property.rb', line 29

def initialize(name, type, array, key = '')
  @name = Jaspion::Kilza::Property.normalize(name)
  @original_name = name
  @type = type
  @array = array
  @key = key
  @original_type = type
end

Instance Attribute Details

#arrayObject Also known as: array?

Indicates if the property represents an array of objects



21
22
23
# File 'lib/jaspion/kilza/property.rb', line 21

def array
  @array
end

#keyObject Also known as: key?

Indicates if the property should be used for comparing purposes Used to compare if one object is equal to another one



26
27
28
# File 'lib/jaspion/kilza/property.rb', line 26

def key
  @key
end

#nameObject

Normalized property name Starts with _ or alphanumeric character and doesn’t contain any special character



8
9
10
# File 'lib/jaspion/kilza/property.rb', line 8

def name
  @name
end

#original_nameObject

Original JSON property name



11
12
13
# File 'lib/jaspion/kilza/property.rb', line 11

def original_name
  @original_name
end

#original_typeObject

Ruby string type Can be object, fixnum, float, falseclass, trueclass and nilclass



15
16
17
# File 'lib/jaspion/kilza/property.rb', line 15

def original_type
  @original_type
end

#typeObject

Property type name



18
19
20
# File 'lib/jaspion/kilza/property.rb', line 18

def type
  @type
end

Class Method Details

.clean(str) ⇒ String

Removes everything except numbers and letters.

Parameters:

  • str (String)

    string to be cleaned

Returns:



82
83
84
85
# File 'lib/jaspion/kilza/property.rb', line 82

def self.clean(str)
  return if str.nil?
  str.gsub(/[^a-zA-Z0-9]/, '')
end

.normalize(str) ⇒ String

Cleans the string and make it lowercase.

Parameters:

  • str (String)

    string to be cleaned

Returns:



92
93
94
95
96
97
98
# File 'lib/jaspion/kilza/property.rb', line 92

def self.normalize(str)
  return if str.nil?
  str = str.gsub(/[^a-zA-Z0-9]/, '_')
  str = '_' if str.length == 0
  str = '_' + str if str[0].number?
  str.downcase
end

Instance Method Details

#==(pr) ⇒ Object



58
59
60
# File 'lib/jaspion/kilza/property.rb', line 58

def ==(pr)
  @name == pr.name
end

#boolean?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/jaspion/kilza/property.rb', line 46

def boolean?
  @original_type == 'trueclass' || @original_type == 'falseclass'
end

#class_nameObject

If this Property represents a new Class, it returns the formatted class name



64
65
66
# File 'lib/jaspion/kilza/property.rb', line 64

def class_name
  Jaspion::Kilza::Class.normalize(@original_name)
end

#fixnum?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/jaspion/kilza/property.rb', line 42

def fixnum?
  @original_type == 'fixnum'
end

#float?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/jaspion/kilza/property.rb', line 50

def float?
  @original_type == 'float'
end

#null?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/jaspion/kilza/property.rb', line 54

def null?
  @original_type == 'nilclass'
end

#object?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/jaspion/kilza/property.rb', line 38

def object?
  @original_type == 'hash'
end

#to_sObject



68
69
70
71
72
73
74
75
# File 'lib/jaspion/kilza/property.rb', line 68

def to_s
  {
    name: @name,
    original_name: @original_name,
    type: @type,
    array?: @array
  }.to_s
end