Class: SMG::Mapping::Element
- Inherits:
-
Object
- Object
- SMG::Mapping::Element
- Defined in:
- lib/smg/mapping/element.rb
Instance Attribute Summary collapse
-
#accessor ⇒ Object
readonly
Returns the value of attribute accessor.
-
#at ⇒ Object
readonly
Returns the value of attribute at.
-
#cast_to ⇒ Object
readonly
Returns the value of attribute cast_to.
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#data_class ⇒ Object
readonly
Returns the value of attribute data_class.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#with ⇒ Object
readonly
Returns the value of attribute with.
Instance Method Summary collapse
- #cast(value) ⇒ Object
- #collection? ⇒ Boolean
- #in_context_of?(context) ⇒ Boolean
-
#initialize(path, options = {}) ⇒ Element
constructor
A new instance of Element.
- #with?(attrh) ⇒ Boolean
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, = {}) @name = ([:as] || [:at] || path.last.gsub(":","_")).to_sym @path = path @collection = !![:collection] @with = [:with] ? normalize_conditions([:with]) : nil @accessor = @collection ? :"append_to_#{@name}" : :"#{@name}=" @data_class = nil @cast_to = nil @context = nil if .key?(:context) @context = Array([:context]).compact @context.uniq! @context = nil if @context.empty? end if .key?(:class) klass = [: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 = .key?(:at) && !@data_class ? [:at].to_s : nil end |
Instance Attribute Details
#accessor ⇒ Object (readonly)
Returns the value of attribute accessor.
5 6 7 |
# File 'lib/smg/mapping/element.rb', line 5 def accessor @accessor end |
#at ⇒ Object (readonly)
Returns the value of attribute at.
5 6 7 |
# File 'lib/smg/mapping/element.rb', line 5 def at @at end |
#cast_to ⇒ Object (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 |
#context ⇒ Object (readonly)
Returns the value of attribute context.
5 6 7 |
# File 'lib/smg/mapping/element.rb', line 5 def context @context end |
#data_class ⇒ Object (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 |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/smg/mapping/element.rb', line 5 def name @name end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
5 6 7 |
# File 'lib/smg/mapping/element.rb', line 5 def path @path end |
#with ⇒ Object (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
46 47 48 |
# File 'lib/smg/mapping/element.rb', line 46 def collection? @collection end |
#in_context_of?(context) ⇒ 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
54 55 56 |
# File 'lib/smg/mapping/element.rb', line 54 def with?(attrh) @with.nil? || @with.all? { |k,v| attrh[k] == v } end |