Module: Importu::Dsl::ClassMethods

Defined in:
lib/importu/dsl.rb

Instance Method Summary collapse

Instance Method Details

#allow_actions(*actions) ⇒ Object



51
52
53
# File 'lib/importu/dsl.rb', line 51

def allow_actions(*actions)
  @allowed_actions = actions
end

#config_dsl(*methods) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/importu/dsl.rb', line 102

def config_dsl(*methods)
  options = methods.extract_options!
  options.assert_valid_keys(:default)
  default = (options[:default] || nil).deep_freeze

  methods.each do |m|
    instance_variable_set("@#{m}", default)

    singleton_class.send(:define_method, m) do |*args,&block|
      if block || !args.empty?
        val = (block ? instance_eval(&block) : args[0])
        instance_variable_set("@#{m}", val.deep_freeze)
      else
        instance_variable_defined?("@#{m}") \
          ? instance_variable_get("@#{m}")
          : superclass.send(m)
      end
    end
  end

  # make dsl methods available to importer instances
  delegate *methods, :to => :singleton_class
end

#convert_to(type, options = {}) ⇒ Object



98
99
100
# File 'lib/importu/dsl.rb', line 98

def convert_to(type, options = {})
  converters[type] # FIXME: raise error if not found?
end

#converter(name, &block) ⇒ Object



94
95
96
# File 'lib/importu/dsl.rb', line 94

def converter(name, &block)
  @converters = converters.merge(name => block)
end

#fields(*fields, &block) ⇒ Object Also known as: field



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/importu/dsl.rb', line 59

def fields(*fields, &block)
  block = fields.pop if fields.last.kind_of?(Proc)
  options = fields.extract_options!.symbolize_keys!

  @definitions ||= definitions.deep_dup
  fields.compact.each do |field_name|
    definition = (@definitions[field_name]||{}).merge(options)

    definition[:name] = field_name
    definition[:label] ||= (options['label'] || field_name).to_s
    definition[:required] = true unless definition.key?(:required)
    definition[:create] = true unless definition.key?(:create)
    definition[:update] = true unless definition.key?(:update)

    definition[:converter] = block if block
    definition[:converter] ||= converters[:clean]

    @definitions[field_name] = definition
  end

  return
end

#find_by(*field_groups, &block) ⇒ Object



55
56
57
# File 'lib/importu/dsl.rb', line 55

def find_by(*field_groups, &block)
  @finder_fields = block ? [block] : field_groups.map {|g|g&&[*g]}.compact
end

#postprocess(&block) ⇒ Object



89
90
91
92
# File 'lib/importu/dsl.rb', line 89

def postprocess(&block)
  # gets executed just after record converted to object
  @postprocessor = block
end

#preprocess(&block) ⇒ Object



84
85
86
87
# File 'lib/importu/dsl.rb', line 84

def preprocess(&block)
  # gets executed just before record converted to object
  @preprocessor = block
end