Class: MaskableAttribute::Formatting::Formats

Inherits:
Hash
  • Object
show all
Defined in:
lib/maskable_attribute/formatting.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Formats

Returns a new instance of Formats.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/maskable_attribute/formatting.rb', line 11

def initialize(options = {})
  @are_exclusive, @has_default = false, false
  @default = nil
  unless options.nil?
    options.each do |type, formats|
      if formats.is_a? Symbol or formats.is_a? Proc
        self[formats] = Format.new formats
      elsif formats.is_a? Array
        formats.each do |format|
          self[format] = Format.new format
        end
      else
        formats.each do |format, method|
          self[format] = Format.new method || format
        end
      end

      if type.to_s.include? "exclusive"
        @are_exclusive = true
      elsif type.to_s.include? "default"
        @has_default = true
        @default = self[formats]
      end
    end
  end
end

Instance Attribute Details

#are_exclusiveObject

{ :format => :two_digit } { :formats => [ :upcase, :downcase ] } { :exclusive_format => { :capitalized => Proc.new{ |mask| mask.captialized } } } { :exclusive_formats =>{ :capitalized => Proc.new{ |mask| mask.captialized }, :titleized => :titleize } } { :default_format => :titleize }



10
11
12
# File 'lib/maskable_attribute/formatting.rb', line 10

def are_exclusive
  @are_exclusive
end

#defaultObject

{ :format => :two_digit } { :formats => [ :upcase, :downcase ] } { :exclusive_format => { :capitalized => Proc.new{ |mask| mask.captialized } } } { :exclusive_formats =>{ :capitalized => Proc.new{ |mask| mask.captialized }, :titleized => :titleize } } { :default_format => :titleize }



10
11
12
# File 'lib/maskable_attribute/formatting.rb', line 10

def default
  @default
end

#has_defaultObject

{ :format => :two_digit } { :formats => [ :upcase, :downcase ] } { :exclusive_format => { :capitalized => Proc.new{ |mask| mask.captialized } } } { :exclusive_formats =>{ :capitalized => Proc.new{ |mask| mask.captialized }, :titleized => :titleize } } { :default_format => :titleize }



10
11
12
# File 'lib/maskable_attribute/formatting.rb', line 10

def has_default
  @has_default
end

Instance Method Details

#apply(format, &block) ⇒ Object



38
39
40
# File 'lib/maskable_attribute/formatting.rb', line 38

def apply(format, &block)
  fetch(format, (has_default? ? @default : Format.new)).apply yield unless yield.nil?
end

#are_exclusive?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/maskable_attribute/formatting.rb', line 46

def are_exclusive?
  @are_exclusive
end

#has_default?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/maskable_attribute/formatting.rb', line 50

def has_default?
  @has_default
end

#namesObject



42
43
44
# File 'lib/maskable_attribute/formatting.rb', line 42

def names
  keys
end