Module: InFormat

Defined in:
lib/in_format.rb,
lib/in_format/core.rb,
lib/in_format/railtie.rb,
lib/in_format/version.rb,
lib/in_format/formatters/ssn.rb,
lib/in_format/formatters/phone.rb

Defined Under Namespace

Modules: Formatters Classes: Railtie

Constant Summary collapse

VERSION =
"0.0.1"

Instance Method Summary collapse

Instance Method Details

#in_format(attribute, opts = {}) ⇒ Object



3
4
5
6
7
8
9
10
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
37
38
39
40
# File 'lib/in_format/core.rb', line 3

def in_format(attribute, opts = {})

  if opts[:setter]
    if opts[:use_accessor]
      original_setter = self.instance_method("#{attribute}=")

      define_method("#{attribute}=") do |value|
        original_setter.bind(self).call(opts[:setter].call(value))
      end
    else
      define_method("#{attribute}=") do |value|
        self[attribute.to_sym] = opts[:setter].call(value)
      end
    end
  end

  if opts[:getter]
    if opts[:use_accessor]
      original_getter = self.instance_method(attribute)

      define_method("#{attribute}") do |raw = false|
        if raw
          original_getter.bind(self).call
        else
          opts[:getter].call(original_getter.bind(self).call)
        end
      end
    else
      define_method("#{attribute}") do |raw = false|
        if raw
          self[attribute.to_sym]
        else
          opts[:getter].call(self[attribute.to_sym])
        end
      end
    end
  end
end

#phone_format(attribute, opts = {}) ⇒ Object



3
4
5
6
7
8
# File 'lib/in_format/formatters/phone.rb', line 3

def phone_format(attribute, opts = {})
  opts[:setter] = Formatters::Phone::DEFAULT_SETTER unless opts.keys.include? :setter
  opts[:getter] = Formatters::Phone::DEFAULT_GETTER unless opts.keys.include? :getter

  in_format(attribute, opts)
end

#ssn_format(attribute, opts = {}) ⇒ Object



3
4
5
6
7
8
# File 'lib/in_format/formatters/ssn.rb', line 3

def ssn_format(attribute, opts = {})
  opts[:setter] = Formatters::Ssn::DEFAULT_SETTER unless opts.keys.include? :setter
  opts[:getter] = Formatters::Ssn::DEFAULT_GETTER unless opts.keys.include? :getter

  in_format(attribute, opts)
end