Class: Effective::Attribute

Inherits:
Object
  • Object
show all
Defined in:
app/models/effective/attribute.rb

Constant Summary collapse

REGEX =
/^\W*(\w+)\W*:(\w+)/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type = nil, options = {}) ⇒ Attribute

Returns a new instance of Attribute.



19
20
21
22
23
# File 'app/models/effective/attribute.rb', line 19

def initialize(name, type = nil, options = {})
  @name = name.to_s
  @type = (type.presence || :string).to_sym
  @options = options
end

Instance Attribute Details

#name ⇒ Object

Returns the value of attribute name.



3
4
5
# File 'app/models/effective/attribute.rb', line 3

def name
  @name
end

#type ⇒ Object

Returns the value of attribute type.



3
4
5
# File 'app/models/effective/attribute.rb', line 3

def type
  @type
end

Class Method Details

.parse(input) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'app/models/effective/attribute.rb', line 7

def self.parse(input)
  input = input.to_s

  if (scanned = input.scan(REGEX).first).present?
    new(*scanned)
  elsif input.start_with?('#')
    new(nil)
  else
    new(input)
  end
end

Instance Method Details

#<=>(other) ⇒ Object



50
51
52
# File 'app/models/effective/attribute.rb', line 50

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

#==(other) ⇒ Object



54
55
56
# File 'app/models/effective/attribute.rb', line 54

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

#field_type ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/models/effective/attribute.rb', line 29

def field_type
  @field_type ||= case type
    when :integer              then :number_field
    when :float, :decimal      then :text_field
    when :time                 then :time_select
    when :datetime, :timestamp then :datetime_select
    when :date                 then :date_select
    when :text                 then :text_area
    when :boolean              then :check_box
    else :text_field
  end
end

#human_name ⇒ Object



46
47
48
# File 'app/models/effective/attribute.rb', line 46

def human_name
  name.humanize
end

#present? ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'app/models/effective/attribute.rb', line 42

def present?
  name.present?
end

#to_s ⇒ Object



25
26
27
# File 'app/models/effective/attribute.rb', line 25

def to_s
  name
end