module SourcedAttributes::DSL

Public Instance Methods

aliased_attribute(local_name, source_name) click to toggle source

Define an attribute whose local name is different from its name in the source data.

# File lib/sourced_attributes/dsl.rb, line 21
def aliased_attribute local_name, source_name
  @attribute_map[local_name] = source_name
end
association(name, options={}) click to toggle source

Define an association whose value comes from the source data. `primary_key` here is the primary key to use for the associated table. `source_key` is the key to pick out of the source data.

# File lib/sourced_attributes/dsl.rb, line 45
def association name, options={}
  options[:name] ||= name
  options[:source_key] ||= options[:name]
  options[:preload] ||= false
  @associations << options
end
attributes(*args) click to toggle source

Short-hand for defining attributes whose local names map directly to field names in the source data.

# File lib/sourced_attributes/dsl.rb, line 15
def attributes *args
  args.each{ |arg| @attribute_map[arg] = arg }
end
complex_attribute(local_name, &block) click to toggle source
# File lib/sourced_attributes/dsl.rb, line 25
def complex_attribute local_name, &block
  attributes local_name
  @complex_attributes[local_name] = block
end
conditional_attribute(local_name, &block) click to toggle source

Conditional attributes only get updated when the block is true. If no block is given, a default block checking for the presence of the attribute in the source data will be used

# File lib/sourced_attributes/dsl.rb, line 33
def conditional_attribute local_name, &block
  attributes local_name
  if block_given?
    @conditional_attributes[local_name] = block
  else
    @conditional_attributes[local_name] = ->(record) { record[local_name] }
  end
end
configure(options={}) click to toggle source

Set options specific to this Source instance.

# File lib/sourced_attributes/dsl.rb, line 4
def configure options={}
  @config.merge! options
end
primary_key(local, opts={}) click to toggle source

Set the primary key that this Source will use to find records to update.

# File lib/sourced_attributes/dsl.rb, line 9
def primary_key local, opts={}
  @primary_key = { local: local, source: opts[:source] || local }
end