Module: Sem4rSoap::SoapAttributes::ClassMethods

Defined in:
lib/sem4r_soap/soap_attributes.rb

Instance Method Summary collapse

Instance Method Details

#_from_element(el) ⇒ Object



216
217
218
219
# File 'lib/sem4r_soap/soap_attributes.rb', line 216

def _from_element(el)
  return nil unless el
  new._from_element(el)
end

#attributesObject



90
91
92
# File 'lib/sem4r_soap/soap_attributes.rb', line 90

def attributes
  @attributes
end

#enum(set, values) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/sem4r_soap/soap_attributes.rb', line 79

def enum(set, values)
  tmp = values.map do |v|
    if const_defined?(v.to_sym)
      const_get(v.to_sym)
    else
      const_set(v.to_sym, v.to_s)
    end
  end
  const_set( set.to_sym, tmp )
end

#g_accessor(name, constraints = {}) ⇒ Object

constraints

:values_in
:if_type
:default

TODO: g_accessor prende in input anche elements ed estrae automaticamente l’elemento che interessa

cioe' invece di scrivere headline       el.at_xpath("headline").text dovrebbe bastare scrivere
headline el


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/sem4r_soap/soap_attributes.rb', line 109

def g_accessor(name, constraints = {})
  constraints.assert_valid_keys(:xpath, :values_in, :if_type, :default)
  attribute = add_attribute(name, false, constraints[:xpath])
  name = name.to_s

  #
  # define setter
  #

  # values_in
  enum = nil
  if constraints.key?(:values_in)
    enum = const_get(constraints[:values_in])
  end

  # if_type
  if_type = nil
  if constraints.key?(:if_type)
    if_type = constraints[:if_type]
    other_instance_var = "type"
  end

  define_method "#{name}=" do |value|
    if enum and !enum.include?(value)
      raise "Value '#{value}' not permitted "
    end
    if if_type
      type = instance_variable_get "@#{other_instance_var}"
      raise "type must be '#{if_type}' instead of '#{type}'" unless if_type == type
    end
    instance_variable_set "@#{name}", value
  end

  #
  # define getter ( almost :-) )
  #

  # default_value
  default_value = nil
  if constraints.key?(:default)
    default_value = constraints[:default]
  end

  define_method "#{name}" do |*values|  # |value = nil| is incorrect in ruby 1.8
    raise ArgumentError, "wrong number of arguments (#{values.size} for 0)" if values.length > 1
    value = values.first
    if value
      self.__send__("#{name}=", value)
    elsif instance_variable_defined? "@#{name}"
      instance_variable_get "@#{name}"
    else
      default_value
    end
  end
end

#g_reader(name) ⇒ Object



94
95
96
97
98
# File 'lib/sem4r_soap/soap_attributes.rb', line 94

def g_reader(name)
  @attributes  ||= []
  @attributes << MetaSoapAttribute.new(name, false)
  attr_reader name
end

#g_set_accessor(column, constraints = {}) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/sem4r_soap/soap_attributes.rb', line 165

def g_set_accessor(column, constraints = {})
  constraints.assert_valid_keys(:values_in)

  column  = column.to_s
  columns = "#{column}s"
  attribute = add_attribute(columns, true)

  # values_in
  enum = nil
  if constraints.key?(:values_in)
    enum = const_get(constraints[:values_in])
  end

  #
  # define
  #    column= :a
  #    column :a
  #

  define_method "#{column}=" do |value|
    if enum and !enum.include?(value)
      raise "Value not permitted #{value}"
    end
    instance_eval <<-EOFS
      @#{columns} ||= []
      @#{columns} << value unless @#{columns}.include?(value)
    EOFS
  end

  alias_method column, "#{column}="

  #
  # define
  #    colums [:a,:b,:c]
  # define
  #    colums
  #

  define_method "#{columns}" do |*values|
    if values and !values.empty?
      instance_eval "@#{columns} ||= []"
      values.each do |value|
        instance_eval "@#{column}(value)"
      end
    else
      instance_eval "@#{columns} ||= []"
      instance_variable_get "@#{columns}"
    end
  end
end