Class: ROC::Base

Inherits:
Object
  • Object
show all
Includes:
CIMAttributes, Types::AllTypes
Defined in:
lib/roc/objects/base.rb

Direct Known Subclasses

Float, Hash, Integer, List, Set, SortedSet, String, Time

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Types::AllTypes

#eval

Methods included from Types::MethodGenerators

#deserializing_method, #nonserializing_method, #serializing_and_deserializing_method, #serializing_method, #zero_arg_method

Constructor Details

#initialize(key, *args) ⇒ Base

key, [storage], [seed_data], [opts]



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/roc/objects/base.rb', line 18

def initialize(key, *args)
  @key = key

  if args.last.is_a?(Hash)
    @options = args.pop
  end

  if args.first.is_a?(ROC::Store::ROCStore)
    @storage = args.shift
  end

  if !self.storage
    raise ArgumentError, 'no class-level storage set, so must initialize with a Store'
  end

  if args.size > 1
    raise ArgumentError, 'new(key, [storage], [seed_data], [opts])'
  end

  if !(seed_data = args.pop).nil?
    seed(seed_data)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/roc/objects/base.rb', line 63

def method_missing(method_name, *args, &block)
  if self.class.const_get('DELEGATE_OPTIONS') && 
      (delegate_type = self.class.const_get('DELEGATE_OPTIONS')[:on]) && 
      delegate_type.respond_to?(method_name) &&
      !['!', '='].include?(method_name.to_s[method_name.to_s.length - 1]) # we won't delegate modifying methods
    self.send(self.class.const_get('DELEGATE_OPTIONS')[:to]).send(method_name, *args, &block)
  else
    super(method_name, *args, &block)        
  end
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



13
14
15
# File 'lib/roc/objects/base.rb', line 13

def key
  @key
end

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/roc/objects/base.rb', line 13

def options
  @options
end

Class Method Details

.delegate_methods(options) ⇒ Object



54
55
56
57
# File 'lib/roc/objects/base.rb', line 54

def self.delegate_methods(options)
  raise ":on and :to required to delegate methods" unless options.has_key?(:on) && options.has_key?(:to)
  self.const_set('DELEGATE_OPTIONS', options)
end

Instance Method Details

#clobber(data) ⇒ Object



50
51
52
# File 'lib/roc/objects/base.rb', line 50

def clobber(data)
  raise "clobber must be overriden in subclasses"
end

#respond_to?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/roc/objects/base.rb', line 59

def respond_to?(method_name)
  self.methods.include?(method_name) || (self.class.const_get('DELEGATE_OPTIONS') && self.class.const_get('DELEGATE_OPTIONS')[:on].respond_to?(method_name))
end

#seed(data) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/roc/objects/base.rb', line 42

def seed(data)
  if self.exists?
    raise "#{self.key} already exists -- can't seed it"
  else
    self.clobber(data)
  end
end