Class: OrientDB::AR::Embedded

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Translation
Includes:
Attributes, Conversion, Validations
Defined in:
lib/model/embedded.rb

Class Attribute Summary collapse

Attributes included from Validations

#errors

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validations

#valid?

Methods included from Conversion

#to_key, #to_model, #to_param

Methods included from Attributes

#[], #[]=, #attribute_names, #attributes, #changed, #changed?, #changed_attributes, #changes, #previous_changes

Constructor Details

#initialize(fields = {}) ⇒ Embedded

Returns a new instance of Embedded.



11
12
13
14
15
# File 'lib/model/embedded.rb', line 11

def initialize(fields = {})
  @odocument          = self.class.new_document fields
  @changed_attributes = {}
  @errors             = ActiveModel::Errors.new(self)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/model/embedded.rb', line 34

def method_missing(method_name, *args, &blk)
  # Simple field value lookup
  return self[method_name] if field?(method_name)
  # Dirty
  if method_name.to_s =~ /(\w*)(_changed\?|_change|_will_change!|_was)$/ && field?($1)
    __send__("attribute#{$2}", $1)
    # Setter
  elsif method_name.to_s =~ /(.*?)=$/
    self[$1] = args.first
    # Boolean
  elsif method_name.to_s =~ /(.*?)?$/ && field?($1)
    !!self[$1]
    # Unknown pattern
  else
    super
  end
end

Class Attribute Details

.oclass_nameObject



56
57
58
# File 'lib/model/embedded.rb', line 56

def oclass_name
  @oclass_name ||= name.downcase.gsub('::', '_')
end

Class Method Details

.field(name, type, options = {}) ⇒ Object



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

def field(name, type, options = {})
  name = name.to_sym
  if fields.key? name
    puts "Already defined field [#{name}]"
  else
    fields[name] = {:type => type}.update options
  end
end

.fieldsObject



64
65
66
# File 'lib/model/embedded.rb', line 64

def fields
  @fields ||= ActiveSupport::OrderedHash.new
end

.new_document(fields = {}) ⇒ Object



77
78
79
80
# File 'lib/model/embedded.rb', line 77

def new_document(fields = {})
  oclass
  OrientDB::Document.new connection, oclass_name, fields
end

.oclassObject



60
61
62
# File 'lib/model/embedded.rb', line 60

def oclass
  @oclass ||= connection.get_or_create_class oclass_name
end

Instance Method Details

#field?(name) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/model/embedded.rb', line 17

def field?(name)
  @odocument.field?(name)
end

#respond_to?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/model/embedded.rb', line 21

def respond_to?(method_name)
  # Simple field value lookup
  return true if field?(method_name)
  # Dirty
  return true if method_name.to_s =~ /(\w*)(_changed\?|_change|_will_change!|_was)$/ && field?($1)
  # Setter
  return true if method_name.to_s =~ /(.*?)=$/
  # Boolean
  return true if method_name.to_s =~ /(.*?)?$/ && field?($1)
  # Unknown pattern
  super
end