Class: Indexer::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/indexer/model.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Model

New instance.

Parameters:

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

    The metadata to populate the instance.



84
85
86
87
88
# File 'lib/indexer/model.rb', line 84

def initialize(data={})
  initialize_attributes

  merge!(data)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &blk) ⇒ Object

Models are open collections. Any arbitrary settings can be made in order to support non-specification indexing.



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/indexer/model.rb', line 192

def method_missing(sym, *args, &blk)
  super(sym, *args, &blk) if blk
  name = sym.to_s
  type = name[-1,1]

  case type
  when '='
    value = (
      case v = args.first
      when String  then String(v)
      when Integer then Integer(v)
      when Float   then Float(v)
      when Array   then Array(v)
      when Hash    then Hash(v)
      else
        raise ValidationError, "custom metadata for `#{sym}' not simple type -- `#{value.class}'"
      end
    )
    @data[name.chomp('=').to_sym] = value
  when '!'
    super(sym, *args, &blk)
  when '?'
    super(sym, *args, &blk)
  else
    key?(name) ? @data[name.to_sym] : nil #super(sym, *args, &blk)
  end
end

Class Method Details

.attr_reader(name) ⇒ Object Also known as: attr



52
53
54
55
56
57
58
# File 'lib/indexer/model.rb', line 52

def attr_reader(name)
  module_eval %{
    def #{name}
      @data[:#{name}]
    end
  }
end

.attr_writer(name) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/indexer/model.rb', line 68

def attr_writer(name)
  module_eval %{
    def #{name}=(value)
      @data[:#{name}] = value
    end
  }
end

Instance Method Details

#[](key) ⇒ Object

Access metadata with hash-like getter method.

Parameters:

  • key (String, Symbol)

    The name of the metadata field.

Returns:

  • (Object)

    The value associated with the key.



106
107
108
109
110
111
112
# File 'lib/indexer/model.rb', line 106

def [](key)
  if respond_to?(key)
    __send__("#{key}")
  else
    @data[key.to_sym]
  end
end

#[]=(key, value) ⇒ Object

Assign metadata with hash-like setter method.

Parameters:

  • key (String, Symbol)

    The name of the metadata field.

  • value (Object)

    The value of the metadata field.

Returns:

  • (Object)

    The newly set value.

Raises:

  • (ArgumentError)

    The key is not known.



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/indexer/model.rb', line 129

def []=(key, value)
  #unless self.class.attributes.include?(key)
  #  #error = ArgumentError.new("unknown attribute: #{key.inspect}")
  #  #error.extend Error
  #  #raise(error)
  #end
  if respond_to?("#{key}=")
    __send__("#{key}=", value)
  else
    @data[key.to_sym] = value
  end
end

#initialize_attributesObject

Set default attributes.



93
94
95
# File 'lib/indexer/model.rb', line 93

def initialize_attributes
  @data = {}
end

#key?(name) ⇒ Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/indexer/model.rb', line 157

def key?(name)
  @data.key?(name.to_sym)
end

#merge!(data) ⇒ Object

Merge data source into metadata.

Parameters:

  • data (Hash)

    The data source which responds to #each like a Hash.



148
149
150
151
152
# File 'lib/indexer/model.rb', line 148

def merge!(data)
  data.each do |key, value|
    self[key] = value
  end
end

#store(key, value) ⇒ Object (private)



223
224
225
# File 'lib/indexer/model.rb', line 223

def store(key, value)
  @data[key.to_sym] = value
end

#to_hHash{String => Object}

Convert metadata to a Hash.

Returns:

  • (Hash{String => Object})


166
167
168
169
170
171
172
173
174
175
# File 'lib/indexer/model.rb', line 166

def to_h
  h = {}
  #self.class.attributes.each do |key|
  #  h[key.to_s] = __send__(name)
  #end
  @data.each do |k, v|
    h[k.to_s] = v if v
  end
  h
end

#to_yaml(io = {}) ⇒ Object

Converts the Hash-like object to YAML.

TODO: Should we have #to_yaml in here?

Parameters:

  • opts (Hash)

    Options used by YAML.



184
185
186
# File 'lib/indexer/model.rb', line 184

def to_yaml(io={})
  to_h.to_yaml(io)
end

#validate(value, field, *types) ⇒ Object (private)



228
229
230
231
232
233
# File 'lib/indexer/model.rb', line 228

def validate(value, field, *types)
  types.each do |type|
    Valid.send(type, value, field)
  end
  return value
end