Class: Microscope::InstanceMethod

Inherits:
Struct
  • Object
show all
Defined in:
lib/microscope/instance_method.rb,
lib/microscope/instance_method/date_instance_method.rb,
lib/microscope/instance_method/boolean_instance_method.rb,
lib/microscope/instance_method/datetime_instance_method.rb

Defined Under Namespace

Classes: BooleanInstanceMethod, DateInstanceMethod, DatetimeInstanceMethod

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ InstanceMethod

Returns a new instance of InstanceMethod.



3
4
5
6
# File 'lib/microscope/instance_method.rb', line 3

def initialize(*args)
  super
  @field_name = field.name
end

Instance Attribute Details

#fieldObject

Returns the value of attribute field

Returns:

  • (Object)

    the current value of field



2
3
4
# File 'lib/microscope/instance_method.rb', line 2

def field
  @field
end

#modelObject

Returns the value of attribute model

Returns:

  • (Object)

    the current value of model



2
3
4
# File 'lib/microscope/instance_method.rb', line 2

def model
  @model
end

Class Method Details

.inject_instance_methods(model, fields, options = {}) ⇒ Object

Inject ActiveRecord scopes into a model



13
14
15
16
17
18
19
20
21
# File 'lib/microscope/instance_method.rb', line 13

def self.inject_instance_methods(model, fields, options = {})
  fields.each do |field|
    scope = "#{field.type.to_s.camelize}InstanceMethod"

    if Microscope::InstanceMethod.const_defined?(scope)
      "Microscope::InstanceMethod::#{scope}".constantize.new(model, field).apply
    end
  end
end

.past_participle_to_infinitive(key) ⇒ Object

Convert a past participle to its infinitive form



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/microscope/instance_method.rb', line 24

def self.past_participle_to_infinitive(key)
  *key, participle = key.split('_')

  infinitive = if Microscope.irregular_verbs.include?(participle)
    Microscope.irregular_verbs[participle]
  elsif participle =~ /ed$/
    participle.sub(/d$/, '')
  else
    participle
  end

  (key << infinitive).join('_')
end

.value_to_boolean(value) ⇒ Object

Convert a value to its boolean representation



39
40
41
# File 'lib/microscope/instance_method.rb', line 39

def self.value_to_boolean(value)
  ![nil, false, 0, '0', 'f', 'F', 'false', 'FALSE'].include?(value.presence)
end

Instance Method Details

#cropped_fieldObject



8
9
10
# File 'lib/microscope/instance_method.rb', line 8

def cropped_field
  @cropped_field ||= @field_name.gsub(@cropped_field_regex, '')
end