Module: WCC::Arena::Mappers::XML::ClassMethods

Defined in:
lib/wcc/arena/mappers/xml.rb

Instance Method Summary collapse

Instance Method Details

#add_association(name, options, &block) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/wcc/arena/mappers/xml.rb', line 103

def add_association(name, options, &block)
  options.fetch(:xpath) {
    raise ArgumentError, ":xpath is a required argument"
  }
  options.fetch(:klass) {
    raise ArgumentError, ":klass is a required argument"
  }
  options.fetch(:type) {
    raise ArgumentError, ":type is a required argument"
  }
  associations[name] = options.freeze
  define_method(name, &block) if block_given?
end

#associationsObject



85
86
87
# File 'lib/wcc/arena/mappers/xml.rb', line 85

def associations
  @associations ||= {}
end

#attribute(name, options) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/wcc/arena/mappers/xml.rb', line 73

def attribute(name, options)
  options.fetch(:xpath) {
    raise ArgumentError, ":xpath is a required argument to the `attribute' method"
  }
  attributes[name] = options.freeze
  define_method(name) { self[name] }
end

#attributesObject



81
82
83
# File 'lib/wcc/arena/mappers/xml.rb', line 81

def attributes
  @attributes ||= {}
end

#has_many(name, options) ⇒ Object



96
97
98
99
100
101
# File 'lib/wcc/arena/mappers/xml.rb', line 96

def has_many(name, options)
  add_association(name, options.merge(type: :many)) do
    @association_cache ||= {}
    @association_cache[name] ||= load_association(name)
  end
end

#has_one(name, options) ⇒ Object



89
90
91
92
93
94
# File 'lib/wcc/arena/mappers/xml.rb', line 89

def has_one(name, options)
  add_association(name, options.merge(type: :one)) do
    @association_cache ||= {}
    @association_cache[name] ||= load_association(name)
  end
end

#inherited(subclass) ⇒ Object



117
118
119
120
121
# File 'lib/wcc/arena/mappers/xml.rb', line 117

def inherited(subclass)
  super
  subclass.instance_variable_set(:@attributes, attributes.dup)
  subclass.instance_variable_set(:@associations, associations.dup)
end