Class: Rets::Metadata::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/rets/metadata/resource.rb

Defined Under Namespace

Classes: MissingRetsClass

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource) ⇒ Resource

Returns a new instance of Resource.



11
12
13
14
15
16
17
# File 'lib/rets/metadata/resource.rb', line 11

def initialize(resource)
  self.rets_classes = []
  self.lookup_types = {}

  self.id = resource["ResourceID"]
  self.key_field = resource["KeyField"]
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



9
10
11
# File 'lib/rets/metadata/resource.rb', line 9

def id
  @id
end

#key_fieldObject

Returns the value of attribute key_field.



7
8
9
# File 'lib/rets/metadata/resource.rb', line 7

def key_field
  @key_field
end

#lookup_typesObject

Returns the value of attribute lookup_types.



6
7
8
# File 'lib/rets/metadata/resource.rb', line 6

def lookup_types
  @lookup_types
end

#rets_classesObject

Returns the value of attribute rets_classes.



5
6
7
# File 'lib/rets/metadata/resource.rb', line 5

def rets_classes
  @rets_classes
end

Class Method Details

.build(resource_fragment, metadata, logger) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/rets/metadata/resource.rb', line 61

def self.build(resource_fragment, , logger)
  resource = new(resource_fragment)

  resource.lookup_types = build_lookup_tree(resource, )
  resource.rets_classes = build_classes(resource, )
  resource
rescue MissingRetsClass => e
  logger.warn(e.message)
  nil
end

.build_classes(resource, metadata) ⇒ Object



55
56
57
58
59
# File 'lib/rets/metadata/resource.rb', line 55

def self.build_classes(resource, )
  find_rets_classes(, resource).map do |rets_class_fragment|
    RetsClass.build(rets_class_fragment, resource, )
  end
end

.build_lookup_tree(resource, metadata) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rets/metadata/resource.rb', line 36

def self.build_lookup_tree(resource, )
  lookup_types = Hash.new {|h, k| h[k] = Array.new }

  find_lookup_containers(, resource).each do |lookup_container|
    lookup_container.lookups.each do |lookup_fragment|
      lookup_name = lookup_fragment["LookupName"]

      find_lookup_type_containers(, resource, lookup_name).each do |lookup_type_container|

        lookup_type_container.lookup_types.each do |lookup_type_fragment|
          lookup_types[lookup_name] << LookupType.new(lookup_type_fragment)
        end
      end
    end
  end

  lookup_types
end

.find_lookup_containers(metadata, resource) ⇒ Object



19
20
21
# File 'lib/rets/metadata/resource.rb', line 19

def self.find_lookup_containers(, resource)
  [:lookup].select { |lc| lc.resource == resource.id }
end

.find_lookup_type_containers(metadata, resource, lookup_name) ⇒ Object



23
24
25
# File 'lib/rets/metadata/resource.rb', line 23

def self.find_lookup_type_containers(, resource, lookup_name)
  [:lookup_type].select { |ltc| ltc.resource == resource.id && ltc.lookup == lookup_name }
end

.find_rets_classes(metadata, resource) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/rets/metadata/resource.rb', line 27

def self.find_rets_classes(, resource)
  class_container = [:class].detect { |c| c.resource == resource.id }
  if class_container.nil?
    raise MissingRetsClass.new("No Metadata classes for #{resource.id}")
  else
    class_container.classes
  end
end

Instance Method Details

#find_rets_class(rets_class_name) ⇒ Object



78
79
80
# File 'lib/rets/metadata/resource.rb', line 78

def find_rets_class(rets_class_name)
  rets_classes.detect {|rc| rc.name == rets_class_name }
end


72
73
74
75
76
# File 'lib/rets/metadata/resource.rb', line 72

def print_tree
  puts "Resource: #{id} (Key Field: #{key_field})"

  rets_classes.each(&:print_tree)
end