Module: SourcedAttributes::DSL

Included in:
Source
Defined in:
lib/sourced_attributes/dsl.rb

Instance Method Summary collapse

Instance Method Details

#aliased_attribute(local_name, source_name) ⇒ Object

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



21
22
23
# 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 = {}) ⇒ Object

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.



45
46
47
48
49
50
# 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) ⇒ Object

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



15
16
17
# File 'lib/sourced_attributes/dsl.rb', line 15

def attributes *args
  args.each{ |arg| @attribute_map[arg] = arg }
end

#complex_attribute(local_name, &block) ⇒ Object



25
26
27
28
# 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) ⇒ Object

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



33
34
35
36
37
38
39
40
# 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 = {}) ⇒ Object

Set options specific to this Source instance.



4
5
6
# File 'lib/sourced_attributes/dsl.rb', line 4

def configure options={}
  @config.merge! options
end

#primary_key(local, opts = {}) ⇒ Object

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



9
10
11
# File 'lib/sourced_attributes/dsl.rb', line 9

def primary_key local, opts={}
  @primary_key = { local: local, source: opts[:source] || local }
end