Class: RailsAttrEnum::EntryValue

Inherits:
Struct
  • Object
show all
Defined in:
lib/rails_attr_enum/entry_value.rb

Overview

Forwarding class for actual attribute value

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rails_attr_enum/entry_value.rb', line 8

def method_missing(name, *args, &block)
  default = proc { value.send(name, *args, &block) }

  if matches = /^(.*?)((\?|!)?)$/.match(name)
    const_name = matches[1].to_s.upcase
    if enum.const_defined?(const_name)
      case matches[2]
      when '?'
        value == enum.const_get(const_name)
      when '!'
        self.value = enum.const_get(const_name)
      else
        enum.const_get(const_name)
      end
    else
      default.call
    end
  else
    default.call
  end
end

Instance Attribute Details

#attr_nameObject

Returns the value of attribute attr_name

Returns:

  • (Object)

    the current value of attr_name



5
6
7
# File 'lib/rails_attr_enum/entry_value.rb', line 5

def attr_name
  @attr_name
end

#enumObject

Returns the value of attribute enum

Returns:

  • (Object)

    the current value of enum



5
6
7
# File 'lib/rails_attr_enum/entry_value.rb', line 5

def enum
  @enum
end

#modelObject

Returns the value of attribute model

Returns:

  • (Object)

    the current value of model



5
6
7
# File 'lib/rails_attr_enum/entry_value.rb', line 5

def model
  @model
end

Instance Method Details

#keyObject



30
31
32
# File 'lib/rails_attr_enum/entry_value.rb', line 30

def key
  enum.get_key(value)
end

#valueObject



34
35
36
# File 'lib/rails_attr_enum/entry_value.rb', line 34

def value
  model.read_attribute(attr_name)
end