Class: Rails::Generators::GeneratedAttribute

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/generators/sequel/generated_attribute.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type) ⇒ GeneratedAttribute

Returns a new instance of GeneratedAttribute.



6
7
8
9
# File 'lib/rails/generators/sequel/generated_attribute.rb', line 6

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

Instance Attribute Details

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/rails/generators/sequel/generated_attribute.rb', line 4

def name
  @name
end

#typeObject

Returns the value of attribute type.



4
5
6
# File 'lib/rails/generators/sequel/generated_attribute.rb', line 4

def type
  @type
end

Instance Method Details

#alter_definitionObject



19
20
21
22
23
24
25
# File 'lib/rails/generators/sequel/generated_attribute.rb', line 19

def alter_definition
  case type
    when :Text then "String, :text => true"
    when :Boolean then "TrueClass"
    else type
  end
end

#create_definitionObject



11
12
13
14
15
16
17
# File 'lib/rails/generators/sequel/generated_attribute.rb', line 11

def create_definition
  case type
    when :Text then "String :#{name}, :text => true"
    when :Boolean then "TrueClass :#{name}"
    else "#{type} :#{name}"
  end
end

#defaultObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rails/generators/sequel/generated_attribute.rb', line 40

def default
  @default ||= case type
    when :integer                     then 1
    when :float                       then 1.5
    when :decimal                     then "9.99"
    when :datetime, :timestamp, :time then Time.now.to_s(:db)
    when :date                        then Date.today.to_s(:db)
    when :string                      then "MyString"
    when :text                        then "MyText"
    when :boolean                     then false
    else
      ""
  end
end

#field_typeObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rails/generators/sequel/generated_attribute.rb', line 27

def field_type
  @field_type ||= case type
    when :Integer, :Float, :BigDecimal, :FixNum, :Numeric   then :text_field
    when :DateTime, :Time                                   then :datetime_select
    when :Date                                              then :date_select
    when :String                                            then :text_field
    when :Text                                              then :text_area
    when :Boolean, :TrueClass, :FalseClass                  then :check_box
    else
      :text_field
  end
end

#human_nameObject



55
56
57
# File 'lib/rails/generators/sequel/generated_attribute.rb', line 55

def human_name
  name.to_s.humanize
end

#reference?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/rails/generators/sequel/generated_attribute.rb', line 59

def reference?
  false
end