Module: DocumentRecord::Base

Extended by:
ActiveSupport::Concern
Defined in:
lib/document_record.rb

Instance Method Summary collapse

Instance Method Details

#document_field(name, options = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/document_record.rb', line 54

def document_field name, options = {}
  raise "Field must exist in record in order to become a document field" unless column_names.include? name.to_s
  class_eval do
    alias_method :regular_assign_attributes, :assign_attributes
    alias_method :regular_method_missing, :method_missing
    alias_method :regular_save, :save

    @@_document_field_name = name
    @@_schema_fields = options[:schema_fields] || []
    @@_index_fields ||= []

    def read_serialized_hash_attribute field_name
      raw = read_attribute field_name
      raw && Serializer.load( raw ) || {}
    end
    
    def write_serialized_hash_attribute field_name, hash
      write_attribute field_name, Serializer.dump(hash)
    end

    def document
      @document ||= ::DocumentHash::Core[read_serialized_hash_attribute(@@_document_field_name)].tap do |d|
        d.before_change do |path, value|
          key = path.join "_"
          
          value = case self.class.columns_hash[key].type
            when :integer then value.to_i
            else value
          end if self.class.columns_hash[key]

          value
        end
        d.after_change do |path, value|
          key = path.join "_"
          write_attribute key, value if self.class.columns_hash[key]
        end
      end
    end

    def save *arguments
      save_document
      regular_save *arguments
    end

    def save! *arguments
      save(*arguments) || raise(RecordNotSaved)
    end

    def touch!
      self.document.touch!
      save
    end

    def save_document
      write_serialized_hash_attribute @@_document_field_name, document.to_hash
    end

    def assign_attributes new_attributes, options = {}
      new_attributes.each do | key, value |
        assign_key = :"#{key}="
        method_missing assign_key, value
        self.send assign_key, value if self.respond_to? assign_key
      end 
    end

    def method_missing method, *args
      if method =~ /(.*)=$/ 
        write_document $1, args.shift
      else
        read_document method.to_s
      end
    end

    def is_schema_field? attribute
      @@_schema_fields.include? attribute.to_sym
    end

    def read_document attribute
      if is_schema_field? attribute
        read_attribute attribute
      else
        document[attribute] || read_attribute(attribute)
      end
    end

    def write_document attribute, value
      if is_schema_field? attribute
        write_attribute attribute, value
      else
        document[attribute] = value
      end
    end

    def is_indexed? attr
      self.class.column_names.include? attr.to_s
    end

    def as_json options = {}
      included_fields = super.select{ |k, v|
        @@_schema_fields.include?( k.to_sym ) 
      }.stringify_keys!

      document.to_hash( stringify_keys: true ).
        merge( included_fields ).
        merge( method_values(options) )
    end

    def method_values options
      {}.tap do | result | 
        return result unless options[:methods].is_a? Array
        options[:methods].each do | method |
          result[method] = self.__send__(method)
        end
      end
    end


    def _deep_key_values hash = nil, path = []
      hash = document.to_hash unless hash
      result = []

      hash.each do | k, v |
        current = path.dup
        
        current << k
        if v.is_a? Hash
          result += _deep_key_values hash[ k ], current 
        else
          result += [ current.join("_"), v ]
        end
      end
      result
    end

    def deep_key_values
      Hash[*_deep_key_values]
    end

    def deep_keys
      deep_key_values.keys
    end
  end

  column_names.each do |column|
    class_eval "      def \#{column}\n        # document[\"\#{column}\"] || read_attribute(\"\#{column}\")\n        read_document \"\#{ column }\"\n      end\n\n      def \#{column}= value\n        # document[\"\#{column}\"] = value\n        write_document \"\#{ column }\", value\n      end\n    METHOD\n  end\nend\n"