Class: Effective::Attribute

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj, type = nil, klass: nil) ⇒ Attribute

We also use this class to do value parsing in Datatables. In that case it will be initialized with just a ‘name’



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/models/effective/attribute.rb', line 19

def initialize(obj, type = nil, klass: nil)
  @klass = klass

  if obj.present? && type.present?
    @name = obj.to_s
    @type = type.to_sym
  end

  @type ||= (
    case obj
    when :boolean     ; :boolean
    when :config      ; :config
    when :currency    ; :currency
    when :date        ; :date
    when :datetime    ; :datetime
    when :decimal     ; :decimal
    when :duration    ; :duration
    when :email       ; :email
    when :integer     ; :integer
    when :percent     ; :percent
    when :percentage  ; :percent
    when :phone       ; :phone
    when :price       ; :price
    when :nil         ; :nil
    when :resource    ; :resource
    when :select      ; :string
    when :radios      ; :string
    when :string      ; :string
    when :text        ; :string
    when :time        ; :time
    when :uuid        ; :uuid
    when :url         ; :url
    when FalseClass   ; :boolean
    when (defined?(Integer) ? Integer : Fixnum) ; :integer
    when Float        ; :decimal
    when NilClass     ; :nil
    when String       ; :string
    when TrueClass    ; :boolean
    when ActiveSupport::TimeWithZone  ; :datetime
    when Date                         ; :date
    when ActiveRecord::Base           ; :resource
    when :belongs_to                  ; :belongs_to
    when :belongs_to_polymorphic      ; :belongs_to_polymorphic
    when :has_many                    ; :has_many
    when :has_and_belongs_to_many     ; :has_and_belongs_to_many
    when :has_one                     ; :has_one
    when :effective_addresses         ; :effective_addresses
    when :effective_obfuscation       ; :effective_obfuscation
    when :effective_roles             ; :effective_roles
    when :active_storage              ; :active_storage
    else
      raise "unsupported type for #{obj}"
    end
  )
end

Instance Attribute Details

#klassObject

Returns the value of attribute klass.



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

def klass
  @klass
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#typeObject

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_written(input) ⇒ Object

This parses the written attributes



6
7
8
9
10
11
12
# File 'app/models/effective/attribute.rb', line 6

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

  if (scanned = input.scan(/^\W*(\w+)\W*:(\w+)/).first).present?
    new(*scanned)
  end
end

Instance Method Details

#<=>(other) ⇒ Object



203
204
205
# File 'app/models/effective/attribute.rb', line 203

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

#==(other) ⇒ Object



207
208
209
# File 'app/models/effective/attribute.rb', line 207

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

#human_nameObject



199
200
201
# File 'app/models/effective/attribute.rb', line 199

def human_name
  name.humanize
end

#parse(value, name: nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'app/models/effective/attribute.rb', line 75

def parse(value, name: nil)
  case type
  when :boolean
    [true, 'true', 't', '1'].include?(value)
  when :config
    raise('expected an ActiveSupport::OrderedOptions') unless value.kind_of?(ActiveSupport::OrderedOptions)
    parse_ordered_options(value)
  when :date, :datetime
    if (digits = value.to_s.scan(/(\d+)/).flatten).present?
      date = if digits.first.length == 4  # 2017-01-10
        (Time.zone.local(*digits) rescue nil)
      else # 01/10/2016
        year = digits.find { |d| d.length == 4 }
        digits = [year] + (digits - [year])
        (Time.zone.local(*digits) rescue nil)
      end

      name.to_s.start_with?('end_') ? date.end_of_day : date
    end
  when :time
    if (digits = value.to_s.scan(/(\d+)/).flatten).present?
      now = Time.zone.now
      (Time.zone.local(now.year, now.month, now.day, *digits) rescue nil)
    end
  when :decimal, :currency
    (value.kind_of?(String) ? value.gsub(/[^0-9|\-|\.]/, '') : value).to_f
  when :duration
    if value.to_s.include?('h')
      (hours, minutes) = (value.to_s.gsub(/[^0-9|\-|h]/, '').split('h'))
      (hours.to_i * 60) + ((hours.to_i < 0) ? -(minutes.to_i) : minutes.to_i)
    else
      value.to_s.gsub(/[^0-9|\-|h]/, '').to_i
    end
  when :effective_obfuscation
    klass.respond_to?(:deobfuscate) ? klass.deobfuscate(value) : value.to_s
  when :effective_roles
    EffectiveRoles.roles.include?(value.to_sym) ? value : EffectiveRoles.roles_for(value)
  when :integer
    (value.kind_of?(String) ? value.gsub(/\D/, '') : value).to_i
  when :percent # Integer * 1000. Percentage to 3 digits.
    value.kind_of?(Integer) ? value : (value.to_s.gsub(/[^0-9|\-|\.]/, '').to_f * 1000.0).round
  when :phone
    digits = value.to_s.gsub(/\D/, '').chars
    digits = (digits.first == '1' ? digits[1..10] : digits[0..9]) # Throw away a leading 1

    return nil unless digits.length == 10

    "(#{digits[0..2].join}) #{digits[3..5].join}-#{digits[6..10].join}"
  when :integer
    (value.kind_of?(String) ? value.gsub(/\D/, '') : value).to_i
  when :nil
    value.presence
  when :price # Integer * 100. Number of cents.
    value.kind_of?(Integer) ? value : (value.to_s.gsub(/[^0-9|\-|\.]/, '').to_f * 100.0).round
  when :string
    value.to_s
  when :url
    return nil unless value.kind_of?(String)

    if value.downcase.start_with?('http://', 'https://')
      value
    elsif value.include?('.')
      "https://#{value}"
    end

  when :email
    return nil unless value.kind_of?(String)

    if value.include?('<') && value.include?('>')
      value = value.match(/<(.+)>/)[1].to_s  # John Smith <[email protected]>
    end

    if value.include?(',')
      value = value.split(',').find { |str| str.include?('@') }.to_s
    end

    if value.include?(' ')
      value = value.split(' ').find { |str| str.include?('@') }.to_s
    end

    value = value.to_s.downcase.strip

    return nil unless value.include?('@') && value.include?('.')

    if defined?(Devise)
      return nil unless Devise::email_regexp.match?(value)
    end

    value
  when :belongs_to_polymorphic
    value.to_s
  when :belongs_to, :has_many, :has_and_belongs_to_many, :has_one, :resource, :effective_addresses  # Returns an Array of ints, an Int or String
    if value.kind_of?(Integer) || value.kind_of?(Array)
      value
    else
      digits = value.to_s.gsub(/[^0-9|,]/, '') # '87' or '87,254,300' or 'something'

      if digits == value || digits.length == 10
        if klass.respond_to?(:deobfuscate)
          digits.split(',').map { |str| klass.deobfuscate(str).to_i }
        else
          digits.split(',').map { |str| str.to_i }
        end
      else
        value.to_s
      end
    end
  when :uuid
    value.to_s
  when :active_storage
    value.to_s
  else
    raise "unsupported type #{type}"
  end
end

#parse_ordered_options(obj) ⇒ Object

This returns a nested ActiveSupport::OrderedOptions.new config



212
213
214
215
216
217
218
# File 'app/models/effective/attribute.rb', line 212

def parse_ordered_options(obj)
  return obj unless obj.kind_of?(Hash)

  ActiveSupport::OrderedOptions.new.tap do |config|
    obj.each { |key, value| config[key] = parse_ordered_options(value) }
  end
end

#present?Boolean

Returns:

  • (Boolean)


195
196
197
# File 'app/models/effective/attribute.rb', line 195

def present?
  name.present? || type.present?
end

#to_sObject



191
192
193
# File 'app/models/effective/attribute.rb', line 191

def to_s
  name
end