Class: KeywordAttribute

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/keyword_attribute.rb

Constant Summary collapse

StdName =

// standard name space –>

{
  'title' => ['title','subject','long_name','standard_name','name'],
  'description' => ['description'],
  'who' => ['creator','maintainer'],
  'information_url' => [{:conditions => "name = 'information_url' OR ( (name = 'reference' OR name = 'references' ) AND value LIKE 'http://%' ) OR name = 'url'", :order => 'name'}]  # order: information_url > reference% > url
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_by_stdname(name) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/keyword_attribute.rb', line 18

def self.find_by_stdname(name)
  if aliases=StdName[name]        # substitution, not ==
    strs = Array.new
    has = Array.new
    aliases.each do |expr|
      case expr
      when String
        strs.push expr
      when Hash
        has.push expr
      else
        raise "[BUG] Unsupported expression type #{expr.class}"
      end
    end
    if strs.length > 0
      if ar = find(:first, :conditions => strs.collect{|st| "name='#{st}'"}.join(" OR "))
        return ar
      end
    end
    if has.length > 0
      has.each{|ha|
        if ar = find(:first, ha)
          return ar
        end
      }
    end
  end
  nil
end

.find_by_stdname_or_name(name) ⇒ Object



48
49
50
# File 'app/models/keyword_attribute.rb', line 48

def self.find_by_stdname_or_name(name)
  find_by_stdname(name) || find(:first,:conditions=>["name=?",name])
end

.format_value_search(val, mode = :equal, name = "") ⇒ Object



79
80
81
82
83
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
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
# File 'app/models/keyword_attribute.rb', line 79

def self.format_value_search(val, mode=:equal, name="")
  name += "." unless name==""
  allowed = [:equal, :like]
  if !allowed.include?(mode)
    raise ArgumentError,"2nd arg must be one of #{allowed.inspect}"
  end
  case val
  when String
    case mode
    when :like
      "#{name}value LIKE '%#{val}%'"
    when :equal
      "#{name}value = '#{val}'"
    else
      raise "Unsupported mode  #{mode.inspect}"
    end
  when Numeric
    if val.is_a?(Integer)
      tcrange = 1..5    # check all integer- and float-types
    elsif val.is_a?(Float)
      tcrange = 4..5    # check float-types
    else
      raise "Unsupported Numeric type  #{val.class}"
    end
    case mode
    when :like
      exprs = []
      for tc in tcrange
        str = NArray.new(tc,1).fill!(val).hton.to_s
        if connection.adapter_name=="SQLite"
          str.gsub!(/\000/,'%00')
          wild_chr = "*"
          operator = "GLOB"
        else
          str = str.gsub(/_/,'\_').gsub(/%/,'\%')
          wild_chr = "%"
          operator = "LIKE BINARY"
        end
        exprs.push( "#{name}num_value #{operator} '#{tc.to_s}#{wild_chr}#{str}#{wild_chr}'")
      end
      exprs.join(' OR ')
    when :equal
      exprs = []
      for tc in tcrange
        str = NArray.new(tc,1).fill!(val).hton.to_s

        str.gsub!(/\000/,'%00') if connection.adapter_name=="SQLite"
        exprs.push( "#{name}num_value = '" + tc.to_s + str  + "'")

      end
      exprs.join(' OR ')
    else
      raise "Unsupported mode  #{mode.inspect}"
    end
  when NArray
    # :like are treated as :equal here
    str = val.hton.to_s
    str.gsub!(/\000/,'%00') if connection.adapter_name=="SQLite"
    "#{name}num_value = '" + val.typecode.to_s + str + "'"
  end
end

Instance Method Details

#_nodeObject



144
# File 'app/models/keyword_attribute.rb', line 144

alias _node node

#node(reload = false, hash = {}) ⇒ Object



145
146
147
148
149
150
151
# File 'app/models/keyword_attribute.rb', line 145

def node(reload=false, hash={})
  if u = hash[:user]
    return Node.find(node_id, :user=>u)
  else
    return _node(reload)
  end
end

#to_xml(opt) ⇒ Object



154
155
156
157
158
159
# File 'app/models/keyword_attribute.rb', line 154

def to_xml(opt)
  opt = opt.dup.update(:except => [:id, :node_id, :value, :num_value])
  super(opt) do |xml|
    xml.value self.value
  end
end

#valueObject



70
71
72
73
74
75
76
77
# File 'app/models/keyword_attribute.rb', line 70

def value
  if (buf=num_value) && buf != "NULL"               # substitution into buf, not ==
    v=NArray.to_na(buf[1..-1],buf[0..0].to_i).ntoh  # typecode is one-digit
  else
    v=super
  end
  v
end

#value=(v) ⇒ Object

// support numerical (NArray) attribute values with num_value –>



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/models/keyword_attribute.rb', line 56

def value= (v)
  case v
  when String
    super(v)
  when NArray
    raise "Supported typecodes are 1 digit" if v.typecode>=10
          # Currently (narray-0.5), it never happens since typecode<= 8
    self.num_value= v.typecode.to_s + v.hton.to_s
  else
    raise ArgumentError, "Unsupported class for a keyword value: #{v.class} (name=#{name})"
  end
  v
end