Module: OrientDB::AR::DocumentMixin

Included in:
Base, Embedded
Defined in:
lib/orientdb-ar/document_mixin.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/orientdb-ar/document_mixin.rb', line 116

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

Instance Attribute Details

#odocumentObject (readonly)

Returns the value of attribute odocument.



47
48
49
# File 'lib/orientdb-ar/document_mixin.rb', line 47

def odocument
  @odocument
end

Class Method Details

.included(base) ⇒ 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
# File 'lib/orientdb-ar/document_mixin.rb', line 18

def self.included(base)
  base.class_eval do
    include Comparable

    include Validatable
    alias_method_chain :valid?, :default_group

    include OrientDB::AR::Attributes
    include OrientDB::AR::Conversion

    extend ActiveModel::Translation
    include ActiveModel::Serializers::JSON
    include ActiveModel::Serializers::Xml

    extend ActiveModel::Callbacks

    class_inheritable_hash :fields
    self.fields = ActiveSupport::OrderedHash.new

    class_inheritable_hash :relationships
    self.relationships = ActiveSupport::OrderedHash.new

    class_inheritable_accessor :default_validation_group

    class_attribute :connection
  end
  base.extend OrientDB::AR::DocumentMixin::ClassMethods
end

Instance Method Details

#<=>(other) ⇒ Object



163
164
165
# File 'lib/orientdb-ar/document_mixin.rb', line 163

def <=>(other)
  to_s <=> other.to_s
end

#connectionObject



138
139
140
# File 'lib/orientdb-ar/document_mixin.rb', line 138

def connection
  self.class.connection
end

#embedded?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/orientdb-ar/document_mixin.rb', line 134

def embedded?
  self.class.embeddable?
end

#field?(name) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/orientdb-ar/document_mixin.rb', line 60

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

#human_idObject



56
57
58
# File 'lib/orientdb-ar/document_mixin.rb', line 56

def human_id
  rid
end

#initialize(fields = { }) ⇒ Object



49
50
51
52
53
54
# File 'lib/orientdb-ar/document_mixin.rb', line 49

def initialize(fields = { })
  @odocument          = self.class.new_document
  @changed_attributes = { }
  @errors             = ActiveModel::Errors.new(self)
  fields.each { |k, v| send "#{k}=", v }
end

#inspectObject Also known as: to_s



154
155
156
157
158
159
# File 'lib/orientdb-ar/document_mixin.rb', line 154

def inspect
  attrs       = attributes.map { |k, v| "#{k}:#{v.inspect}" }.join(' ')
  super_klass = self.class.descends_from_base? ? '' : "(#{self.class.superclass.name})"
  rid_str     = embedded? ? '(E)' : ":#{@odocument.rid}"
  %{#<#{self.class.name}#{super_klass}#{rid_str} #{attrs}>}
end

#oclassObject



142
143
144
# File 'lib/orientdb-ar/document_mixin.rb', line 142

def oclass
  self.class.oclass
end

#oclass_nameObject



146
147
148
# File 'lib/orientdb-ar/document_mixin.rb', line 146

def oclass_name
  self.class.oclass_name
end


96
97
98
99
100
101
# File 'lib/orientdb-ar/document_mixin.rb', line 96

def related
  relationships.map do |rel_name, options|
    rel_obj = send options[:name]
    rel_obj.blank? ? nil : [rel_name, rel_obj]
  end.compact
end

Returns:

  • (Boolean)


76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/orientdb-ar/document_mixin.rb', line 76

def related_valid?
  res = related.all? do |rel_name, row_or_coll|
    case row_or_coll
      when Array
        row_or_coll.all? do |result|
          result.validate_to_parent errors, rel_name
        end
      else
        row_or_coll.validate_to_parent errors, rel_name
    end
  end
  res
end

#respond_to?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/orientdb-ar/document_mixin.rb', line 103

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

#ridObject



150
151
152
# File 'lib/orientdb-ar/document_mixin.rb', line 150

def rid
  @odocument.rid
end

#valid_with_default_group?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/orientdb-ar/document_mixin.rb', line 72

def valid_with_default_group?
  valid_for_group?(default_validation_group) && related_valid?
end

#validateObject



64
65
66
67
68
69
70
# File 'lib/orientdb-ar/document_mixin.rb', line 64

def validate
  @last_validation_result = nil
  _run_validation_callbacks do
    @last_validation_result = valid?
  end
  @last_validation_result
end

#validate_to_parent(parent_errors, rel_name) ⇒ Object



90
91
92
93
94
# File 'lib/orientdb-ar/document_mixin.rb', line 90

def validate_to_parent(parent_errors, rel_name)
  return true if valid?
  errors.full_messages.each { |msg| parent_errors[rel_name] = "#{human_id} #{msg}" }
  false
end