Class: Hemp::Orm::Property

Inherits:
Object show all
Defined in:
lib/hemp/orm/property.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Property



5
6
7
8
9
# File 'lib/hemp/orm/property.rb', line 5

def initialize(name, options = {})
  @name = name
  @options = options
  parse_options
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/hemp/orm/property.rb', line 4

def name
  @name
end

#nullableObject (readonly)

Returns the value of attribute nullable.



4
5
6
# File 'lib/hemp/orm/property.rb', line 4

def nullable
  @nullable
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/hemp/orm/property.rb', line 4

def options
  @options
end

#primary_keyObject (readonly)

Returns the value of attribute primary_key.



4
5
6
# File 'lib/hemp/orm/property.rb', line 4

def primary_key
  @primary_key
end

#typeObject (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/hemp/orm/property.rb', line 4

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object



44
45
46
# File 'lib/hemp/orm/property.rb', line 44

def ==(other)
  name == other.name
end

#parse_optionsObject



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/hemp/orm/property.rb', line 11

def parse_options
  set_default_options
  @options.each_pair do |key, value|
    case key
    when :type
      instance_variable_set("@type", value) if supported_type? value
    when :primary_key, :nullable
      instance_variable_set "@#{key}", value
    end
  end
end

#set_default_optionsObject



23
24
25
26
27
# File 'lib/hemp/orm/property.rb', line 23

def set_default_options
  @type = "text"
  @primary_key = false
  @nullable = true
end

#supported_type?(arg) ⇒ Boolean



36
37
38
39
40
41
42
# File 'lib/hemp/orm/property.rb', line 36

def supported_type?(arg)
  if %w(boolean integer text).include? arg.to_s
    true
  else
    raise DatabaseError.new "Type parameter passed in not supported."
  end
end

#to_sObject



29
30
31
32
33
34
# File 'lib/hemp/orm/property.rb', line 29

def to_s
  "#{name} #{type}" <<
    (nullable ? "" : " not null") <<
    (primary_key ? " primary key" : "") <<
    (primary_key && type == :integer ? " autoincrement" : "")
end