Module: AttributeExtras

Defined in:
lib/attribute_extras.rb,
lib/attribute_extras/version.rb,
lib/attribute_extras/modifier.rb,
lib/attribute_extras/hook_builder.rb,
lib/attribute_extras/extra_builder.rb

Overview

Used for automatic attribute manipulation

Defined Under Namespace

Classes: ExtraBuilder, HookBuilder, Modifier

Constant Summary collapse

VERSION =

current version of the gem

'0.1.4'

Class Method Summary collapse

Class Method Details

.configure {|_self| ... } ⇒ Object

wrap up configuration options into one block

Yields:

  • (_self)

Yield Parameters:



14
15
16
# File 'lib/attribute_extras.rb', line 14

def self.configure(&block)
  yield self
end

.loggerObject

the logger for attribute extras



19
20
21
# File 'lib/attribute_extras.rb', line 19

def self.logger
  @logger ||= Logger.new($stderr)
end

.register_extra(verb, function, past:, validator:, options: nil) ⇒ Object

register the extra and build the functions



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/attribute_extras.rb', line 24

def self.register_extra(verb, function, past:, validator:, options: nil)
  past ||= verb
  compiled_validator = validator.is_a?(Proc) ? validator : ->(options){ validator }
  options ||= ->(attribute){ {} }

  extra = ExtraBuilder.new(verb, past, function, compiled_validator, options).build
  hook = HookBuilder.new(verb, past).build

  self.const_set(:"#{verb.capitalize}Attributes", extra)
  self.extras << extra
  ActiveRecord::Base.extend(hook)
end