Module: ActiveStripper::ClassMethods

Defined in:
lib/active_stripper.rb

Instance Method Summary collapse

Instance Method Details

#strip_value_from(*args) ⇒ Object

Helper to use in your Class

Parameters:

  • *args (Array<Any>)

    List of attributes to use for setter generation Length must be > 2, last argument is the preprocessor to run on value



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/active_stripper.rb', line 82

def strip_value_from(*args)
  raise if args.count < 2
  operator = args.pop

  args = args.reject { | field | self.respond_to?(field) }

  # Dynamically generate an anonymous module to be prepended
  # self is equal to the object where the current method is called
  mod = Module.new do
    args.each do | field |
      define_method :"#{field}=" do | val |
        val = case operator.class.name
              when "Proc"
                operator.call(val)
              when "Hash", "Array"
                ActiveStripper.iterable_apply(val, operator, self)
              when "String", "Symbol"
                ActiveStripper.method_apply(val,
                                            operator,
                                            self,
                                            "ActiveStripper::Helpers")
              end

        super(val)
      end
    end
  end

  prepend mod
end