lib/sem4r_soap/soap_attributes.rb
#
# 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


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
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
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
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 enum = nil if constraints . key? ( :values_in ) enum = const_get ( constraints [ :values_in ] ) end 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 default_value = nil if constraints . key? ( :default ) default_value = constraints [ :default ] end define_method "#{name}" do | * values | 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
constraints
:values_in
:if_type
:default
constraints : values_in : if_type : default
# # define setter # # values_in # if_type # # define getter ( almost :-) ) # # default_value # |value = nil| is incorrect in ruby 1.8
name
<not_specific>
constraints
<not_specific>