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(id, key_field, rets_classes, rets_objects) ⇒ Resource

Returns a new instance of Resource.



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

def initialize(id, key_field, rets_classes, rets_objects)
  @id = id
  @key_field = key_field
  @rets_classes = rets_classes
  @rets_objects = rets_objects
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#key_fieldObject (readonly)

Returns the value of attribute key_field.



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

def key_field
  @key_field
end

#rets_classesObject (readonly)

Returns the value of attribute rets_classes.



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

def rets_classes
  @rets_classes
end

#rets_objectsObject (readonly)

Returns the value of attribute rets_objects.



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

def rets_objects
  @rets_objects
end

Class Method Details

.build(resource_fragment, metadata, logger) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rets/metadata/resource.rb', line 70

def self.build(resource_fragment, , logger)
  resource_id = resource_fragment["ResourceID"]
  key_field = resource_fragment["KeyField"]

  lookup_types = build_lookup_tree(resource_id, )
  rets_classes = build_classes(resource_id, lookup_types, )
  rets_objects = build_objects(resource_id, )

  new(resource_id, key_field, rets_classes, rets_objects)
rescue MissingRetsClass => e
  logger.warn(e.message)
  nil
end

.build_classes(resource_id, lookup_types, metadata) ⇒ Object



58
59
60
61
62
# File 'lib/rets/metadata/resource.rb', line 58

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

.build_lookup_tree(resource_id, metadata) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rets/metadata/resource.rb', line 40

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

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

      find_lookup_type_containers(, resource_id, 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

.build_objects(resource_id, metadata) ⇒ Object



64
65
66
67
68
# File 'lib/rets/metadata/resource.rb', line 64

def self.build_objects(resource_id, )
  find_rets_objects(, resource_id).map do |rets_object_fragment|
    RetsObject.build(rets_object_fragment)
  end
end

.find_lookup_containers(metadata, resource_id) ⇒ Object



14
15
16
# File 'lib/rets/metadata/resource.rb', line 14

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

.find_lookup_type_containers(metadata, resource_id, lookup_name) ⇒ Object



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

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

.find_rets_classes(metadata, resource_id) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/rets/metadata/resource.rb', line 22

def self.find_rets_classes(, resource_id)
  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

.find_rets_objects(metadata, resource_id) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/rets/metadata/resource.rb', line 31

def self.find_rets_objects(, resource_id)
  objects = [:object]
  if objects
    objects.select { |object| object.resource == resource_id }.map(&:objects).flatten
  else
    []
  end
end

Instance Method Details

#find_rets_class(rets_class_name) ⇒ Object



97
98
99
# File 'lib/rets/metadata/resource.rb', line 97

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

Print the tree to a file

out

The file to print to. Defaults to $stdout.



87
88
89
90
91
92
93
94
95
# File 'lib/rets/metadata/resource.rb', line 87

def print_tree(out = $stdout)
  out.puts "# Resource: #{id} (Key Field: #{key_field})"
  rets_classes.each do |rets_class|
    rets_class.print_tree(out)
  end
  rets_objects.each do |rets_object|
    rets_object.print_tree(out)
  end
end