Class: SMG::Mapping::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/smg/mapping/element.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ Element

Returns a new instance of Element.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/smg/mapping/element.rb', line 7

def initialize(path, options = {})

  @name       = (options[:as] || options[:at] || path.last.gsub(":","_")).to_sym
  @path       = path
  @collection = !!options[:collection]
  @with       = options[:with] ? normalize_conditions(options[:with]) : nil
  @accessor   = @collection ? :"append_to_#{@name}" : :"#{@name}="
  @data_class = nil
  @cast_to    = nil
  @context    = nil

  if options.key?(:context)
    @context = Array(options[:context]).compact
    @context.uniq!
    @context = nil if @context.empty?
  end

  if options.key?(:class)
    klass = options[:class]
    if SMG::Model === klass
      @data_class = klass
    elsif SMG::Mapping::TypeCasts.key?(klass)
      @cast_to = klass
    else
      raise ArgumentError, "+options[:class]+ should be an SMG::Model or a valid typecast"
    end
  end

  #ignore options[:at] on nested collections of resources
  @at = options.key?(:at) && !@data_class ? options[:at].to_s : nil

end

Instance Attribute Details

#accessorObject (readonly)

Returns the value of attribute accessor.



5
6
7
# File 'lib/smg/mapping/element.rb', line 5

def accessor
  @accessor
end

#atObject (readonly)

Returns the value of attribute at.



5
6
7
# File 'lib/smg/mapping/element.rb', line 5

def at
  @at
end

#cast_toObject (readonly)

Returns the value of attribute cast_to.



5
6
7
# File 'lib/smg/mapping/element.rb', line 5

def cast_to
  @cast_to
end

#contextObject (readonly)

Returns the value of attribute context.



5
6
7
# File 'lib/smg/mapping/element.rb', line 5

def context
  @context
end

#data_classObject (readonly)

Returns the value of attribute data_class.



5
6
7
# File 'lib/smg/mapping/element.rb', line 5

def data_class
  @data_class
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/smg/mapping/element.rb', line 5

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/smg/mapping/element.rb', line 5

def path
  @path
end

#withObject (readonly)

Returns the value of attribute with.



5
6
7
# File 'lib/smg/mapping/element.rb', line 5

def with
  @with
end

Instance Method Details

#cast(value) ⇒ Object



40
41
42
43
44
# File 'lib/smg/mapping/element.rb', line 40

def cast(value)
  @cast_to ? SMG::Mapping::TypeCasts[@cast_to, value] : value
rescue
  raise ArgumentError, "#{value.inspect} is not a valid source for #{@cast_to.inspect}"
end

#collection?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/smg/mapping/element.rb', line 46

def collection?
  @collection
end

#in_context_of?(context) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/smg/mapping/element.rb', line 50

def in_context_of?(context)
  @context.nil? || @context.include?(context)
end

#with?(attrh) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/smg/mapping/element.rb', line 54

def with?(attrh)
  @with.nil? || @with.all? { |k,v| attrh[k] == v }
end