Class: SchemaToScaffold::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/schema_to_scaffold/attribute.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type) ⇒ Attribute

Returns a new instance of Attribute.



6
7
8
# File 'lib/schema_to_scaffold/attribute.rb', line 6

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/schema_to_scaffold/attribute.rb', line 4

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/schema_to_scaffold/attribute.rb', line 4

def type
  @type
end

Class Method Details

.parse(attribute) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/schema_to_scaffold/attribute.rb', line 14

def self.parse(attribute)
  match = attribute.match(/t\.(\w+)\s+"(\w+)"/)
  if match
    name = match.captures[1].sub(/_id$/, "")
    type = $&.nil? ? match.captures[0] : "references"
    Attribute.new(name, type)
  end
end

Instance Method Details

#to_scriptObject



10
11
12
# File 'lib/schema_to_scaffold/attribute.rb', line 10

def to_script
  "#{name}:#{type}" unless ["created_at","updated_at"].include?(name)
end