Module: ArDocStore::Storage::ClassMethods

Defined in:
lib/ar_doc_store/storage.rb

Instance Method Summary collapse

Instance Method Details

#add_ransacker(key, predicate = nil) ⇒ Object

:nodoc:



82
83
84
85
86
87
88
89
90
91
# File 'lib/ar_doc_store/storage.rb', line 82

def add_ransacker(key, predicate = nil)
  return unless respond_to?(:ransacker)
  ransacker key do
    sql = "(data->>'#{key}')"
    if predicate
      sql = "#{sql}::#{predicate}"
    end
    Arel.sql(sql)
  end
end

#attribute(name, *args) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/ar_doc_store/storage.rb', line 70

def attribute(name, *args)
  type = args.shift if args.first.is_a?(Symbol)
  options = args.extract_options!
  type ||= options.delete(:as) || :string
  class_name = ArDocStore.mappings[type] || "ArDocStore::AttributeTypes::#{type.to_s.classify}Attribute"
  raise "Invalid attribute type: #{class_name}" unless const_defined?(class_name)
  class_name.constantize.build self, name, options
  define_virtual_attribute_method name
  define_method "#{name}?", -> { public_send(name).present? }
end

#boolean_attributes(*args) ⇒ Object

Allows you to define several boolean attributes at once. Deprecated.



142
143
144
145
146
# File 'lib/ar_doc_store/storage.rb', line 142

def boolean_attributes(*args)
  args.each do |arg|
    attribute arg, as: :boolean
  end
end

#define_virtual_attribute_method(attr_name) ⇒ Object

Pretty much the same as define_attribute_method but skipping the matches that create read and write methods



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ar_doc_store/storage.rb', line 94

def define_virtual_attribute_method(attr_name)
  attr_name = attr_name.to_s
  attribute_method_matchers.each do |matcher|
    method_name = matcher.method_name(attr_name)
    next if instance_method_already_implemented?(method_name)
    next if %w{attribute attribute= attribute_before_type_cast}.include? matcher.method_missing_target
    generate_method = "define_method_#{matcher.method_missing_target}"
    if respond_to?(generate_method, true)
      send(generate_method, attr_name)
    else
      define_proxy_call true, generated_attribute_methods, method_name, matcher.method_missing_target, attr_name.to_s
    end
  end
  attribute_method_matchers_cache.clear
end

#enumerates(field, *args) ⇒ Object

Shorthand for attribute :name, as: :enumeration, values: %wb c Deprecated.



150
151
152
153
154
# File 'lib/ar_doc_store/storage.rb', line 150

def enumerates(field, *args)
  options = args.extract_options!
  options[:as] = :enumeration
  attribute field, options
end

#float_attributes(*args) ⇒ Object

Allows you to define several float attributes at once. Deprecated.



128
129
130
131
132
# File 'lib/ar_doc_store/storage.rb', line 128

def float_attributes(*args)
  args.each do |arg|
    attribute arg, as: :float
  end
end

#integer_attributes(*args) ⇒ Object

Allows you to define several integer attributes at once. Deprecated.



135
136
137
138
139
# File 'lib/ar_doc_store/storage.rb', line 135

def integer_attributes(*args)
  args.each do |arg|
    attribute arg, as: :integer
  end
end

#store_attributes(typecast_method, predicate = nil, attributes = [], default_value = nil) ⇒ Object

TODO: Remove the following deprecated methods once projects that use them have been refactored. :nodoc:



113
114
115
116
117
118
# File 'lib/ar_doc_store/storage.rb', line 113

def store_attributes(typecast_method, predicate=nil, attributes=[], default_value=nil)
  attributes = [attributes] unless attributes.respond_to?(:each)
  attributes.each do |key|
    store_attribute key, typecast_method, predicate, default_value
  end
end

#string_attributes(*args) ⇒ Object

Allows you to define several string attributes at once. Deprecated.



121
122
123
124
125
# File 'lib/ar_doc_store/storage.rb', line 121

def string_attributes(*args)
  args.each do |arg|
    attribute arg, as: :string
  end
end