Class: RGeoServer::ResourceInfo

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks
Includes:
ActiveModel::Dirty
Defined in:
lib/rgeoserver/resource.rb

Constant Summary collapse

OBJ_ATTRIBUTES =

mapping object parameters to profile elements

{:enabled => 'enabled'}
OBJ_DEFAULT_ATTRIBUTES =
{:enabled => 'true'}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(catalog = nil) ⇒ ResourceInfo

Returns a new instance of ResourceInfo.



58
59
60
61
# File 'lib/rgeoserver/resource.rb', line 58

def initialize catalog = nil
  @new = true
  @catalog = catalog
end

Instance Attribute Details

#catalogObject

Returns the value of attribute catalog.



12
13
14
# File 'lib/rgeoserver/resource.rb', line 12

def catalog
  @catalog
end

Class Method Details

.list(klass, catalog, names, options = {}, check_remote = false) {|RGeoServer::ResourceInfo, klass| ... } ⇒ Object

Generic object construction iterator

Parameters:

Yields:

Raises:

  • (ArgumentError)


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

def self.list klass, catalog, names, options = {}, check_remote = false
  raise ArgumentError, "Names required" if names.nil?
  names = [names] unless names.is_a? Array
  l = [] unless block_given?
  names.each do |name|
    obj = klass.new catalog, options.merge(:name => name)
    obj.new? if check_remote
    if block_given?
      yield obj 
    else
      l << obj
    end
  end
  l unless block_given?
end

.update_attribute_accessors(attributes) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rgeoserver/resource.rb', line 20

def self.update_attribute_accessors attributes
  attributes.each do |attribute, profile_name|
    class_eval <<-RUBY
    def #{attribute.to_s}
      @#{attribute} || profile['#{profile_name.to_s}'] || OBJ_DEFAULT_ATTRIBUTES[:#{attribute}]
    end

    def #{attribute.to_s}= val
      #{attribute.to_s}_will_change! unless val == #{attribute.to_s}
      @#{attribute.to_s} = val
    end
    RUBY
  end
end

Instance Method Details

#clearObject



134
135
136
137
# File 'lib/rgeoserver/resource.rb', line 134

def clear
  @profile = nil
  @changed_attributes = {}
end

#create_methodObject



67
68
69
# File 'lib/rgeoserver/resource.rb', line 67

def create_method
  :post
end

#delete(options = {}) ⇒ RGeoServer::ResourceInfo

Purge resource from Geoserver Catalog

Parameters:

  • options (Hash) (defaults to: {})

Returns:



119
120
121
122
123
124
125
# File 'lib/rgeoserver/resource.rb', line 119

def delete options = {}
  run_callbacks :destroy do
    @catalog.purge({@route => @name}, options) unless new?
    clear
    self
  end
end

#new?Boolean

Check if this resource already exists

Returns:

  • (Boolean)


129
130
131
132
# File 'lib/rgeoserver/resource.rb', line 129

def new?
  profile
  @new
end

#profileHash

Retrieve the resource profile as a hash and cache it

Returns:

  • (Hash)


147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/rgeoserver/resource.rb', line 147

def profile
  unless @profile
    begin
      self.profile = @catalog.search @route => @name
      @new = false
    rescue RestClient::ResourceNotFound # The resource is new
      @profile = {}
      @new = true
    end
    @profile.freeze unless @profile.frozen?
  end
  @profile
end

#profile=(profile_xml) ⇒ Object



161
162
163
164
# File 'lib/rgeoserver/resource.rb', line 161

def profile= profile_xml
  @profile = profile_xml_to_hash(profile_xml)
  @profile.freeze
end

#profile_xml_to_hash(profile_xml) ⇒ Object

Raises:

  • (NotImplementedError)


170
171
172
# File 'lib/rgeoserver/resource.rb', line 170

def profile_xml_to_hash profile_xml
  raise NotImplementedError, 'profile_xml_to_hash is abstract method'
end

#profile_xml_to_ng(profile_xml) ⇒ Object



166
167
168
# File 'lib/rgeoserver/resource.rb', line 166

def profile_xml_to_ng profile_xml
  Nokogiri::XML(profile_xml).xpath(self.class.member_xpath)
end

#refreshObject



139
140
141
142
143
# File 'lib/rgeoserver/resource.rb', line 139

def refresh
  clear
  profile
  self
end

#save(options = {}) ⇒ RGeoServer::ResourceInfo

Modify or save the resource

Parameters:

  • options (Hash) (defaults to: {})

    / query parameters

Returns:



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rgeoserver/resource.rb', line 84

def save options = {}
  @previously_changed = changes
  @changed_attributes.clear
  run_callbacks :save do
    unless @previously_changed[:name].nil?
      old_name, new_name = @previously_changed[:name]
      name_route = old_name if old_name != new_name
      update = true
    else
      name_route = name
      update = false
    end
    if !update && new?
      if self.respond_to?(:create_route)
        raise "Resource cannot be created directly" if create_route.nil?
        route = create_route
      else
        route = {@route => nil}
      end
      options = create_options.merge(options) if self.respond_to?(:create_options)
      @catalog.add(route, message, create_method, options)
      clear
    else
      options = update_params(name_route).merge(options)
      route = {@route => name_route}
      @catalog.modify(route, message, update_method, options) #unless changes.empty?
    end

    self
  end
end

#to_sObject



63
64
65
# File 'lib/rgeoserver/resource.rb', line 63

def to_s
  "#{self.class.name}: #{name} (#{new?}) on #{catalog}"
end

#update_methodObject



71
72
73
# File 'lib/rgeoserver/resource.rb', line 71

def update_method
  :put
end

#update_params(name_route = name) ⇒ Object

We pass the old name “name_route” in case the name of the resource is being edited Child classes should implement this



77
78
79
# File 'lib/rgeoserver/resource.rb', line 77

def update_params name_route = name
  { self.class.resource_name.downcase.to_sym => name_route }
end