Class: ActiveModule::Base

Inherits:
ActiveModel::Type::Value
  • Object
show all
Defined in:
lib/active_module/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(possible_modules_or_mapping = [], possible_modules: [], mapping: {}, enum_compatibility: false) ⇒ Base

Returns a new instance of Base.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/active_module/base.rb', line 7

def initialize(possible_modules_or_mapping = [],
               possible_modules: [],
               mapping: {},
               enum_compatibility: false)
  @enum_compatibility = enum_compatibility
  if possible_modules_or_mapping.is_a?(Array)
    @possible_modules =
      (possible_modules_or_mapping + possible_modules + mapping.keys).uniq
    @mapping = default_mapping.merge(mapping)
  else
    @possible_modules =
      (possible_modules_or_mapping.keys + possible_modules + mapping.keys)
      .uniq
    @mapping = default_mapping.merge(possible_modules_or_mapping)
                              .merge(mapping)
  end
  super()
end

Instance Attribute Details

#mappingObject (readonly)

Returns the value of attribute mapping.



5
6
7
# File 'lib/active_module/base.rb', line 5

def mapping
  @mapping
end

#possible_modulesObject (readonly)

Returns the value of attribute possible_modules.



5
6
7
# File 'lib/active_module/base.rb', line 5

def possible_modules
  @possible_modules
end

Instance Method Details

#==(other) ⇒ Object



26
27
28
29
30
# File 'lib/active_module/base.rb', line 26

def ==(other)
  other.is_a?(Base) &&
    possible_modules == other.possible_modules &&
    mapping == other.mapping
end

#cast(value) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/active_module/base.rb', line 40

def cast(value)
  case value
  when nil
    nil
  when ::Symbol
    sym_to_module(value)
  when ::Module
    if possible_module?(value)
      value
    else
      raise_invalid_module_value_error(value)
    end
  when ::String
    str_to_module(value)
  else
    raise_invalid_module_value_error(value)
  end
end

#deserialize(str) ⇒ Object



63
64
65
# File 'lib/active_module/base.rb', line 63

def deserialize(str)
  from_db[str]
end

#serializable?(object) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/active_module/base.rb', line 36

def serializable?(object)
  possible_module?(object)
end

#serialize(module_instance) ⇒ Object



59
60
61
# File 'lib/active_module/base.rb', line 59

def serialize(module_instance)
  mapping[cast(module_instance)]
end

#typeObject



32
33
34
# File 'lib/active_module/base.rb', line 32

def type
  :active_module
end