Class: MaskableAttribute::Masks

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/maskable_attribute/mask.rb

Instance Method Summary collapse

Constructor Details

#initialize(masks) ⇒ Masks

:bar :foo => :bar :foo => Proc.new { |object| object.size * 3 } :foo => { :format => :two_digit } :foo => { :formats => [ :two_digit, :upcase, :downcase ] } :bar => { :exclusive_format => { :capitalized => Proc.new{ |mask| mask.captialized } } } :bar => { :exclusive_formats => { :capitalized => Proc.new{ |mask| mask.captialized }, :titleized => :titleize } } :baz => { :default_format => :titleize } :bar => { :method => :quux, { :formats => … } } :bar => { :method => Proc.new { |object| object.size * 3 }, :formats => … }



15
16
17
18
19
20
21
22
23
# File 'lib/maskable_attribute/mask.rb', line 15

def initialize(masks)
  @masks = masks.map do |mask|
    if mask.is_a? Array
      Mask.new mask.first => mask.last
    else
      Mask.new mask
    end
  end
end

Instance Method Details

#[](accessor) ⇒ Object



30
31
32
33
# File 'lib/maskable_attribute/mask.rb', line 30

def [](accessor)
  accessor = accessor.tr " ", "_"
  (find_by_accessor(accessor) || Mask.new).accessor(accessor)
end

#each(&block) ⇒ Object



25
26
27
28
# File 'lib/maskable_attribute/mask.rb', line 25

def each(&block)
  @masks.each(&block)
  return self
end

#find_by_accessor(accessor) ⇒ Object



35
36
37
38
39
40
# File 'lib/maskable_attribute/mask.rb', line 35

def find_by_accessor(accessor)
  accessor = accessor.tr " ", "_"
  @masks.find do |mask|
    mask.accessed_by? accessor
  end
end

#formatted_namesObject



46
47
48
49
50
51
52
53
54
# File 'lib/maskable_attribute/mask.rb', line 46

def formatted_names
  @masks.inject([]) do |names, mask|
    names.push mask.name.to_sym
    mask.formats.each do |format, proc|
      names.push "#{format}_#{mask.name}".to_sym
    end
    names
  end
end

#namesObject



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

def names
  @masks.map(&:name).map(&:to_sym)
end