Class: ArDocStore::Attributes::Base

Inherits:
Object
  • Object
show all
Includes:
CallbackSupport
Defined in:
lib/ar_doc_store/attributes/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CallbackSupport

included

Constructor Details

#initialize(model, attribute, options) ⇒ Base

Returns a new instance of Base.



17
18
19
20
# File 'lib/ar_doc_store/attributes/base.rb', line 17

def initialize(model, attribute, options)
  @model, @attribute, @options = model, attribute, options
  @default = options.delete(:default)
end

Instance Attribute Details

#attributeObject

Returns the value of attribute attribute.



8
9
10
# File 'lib/ar_doc_store/attributes/base.rb', line 8

def attribute
  @attribute
end

#conversionObject

Returns the value of attribute conversion.



8
9
10
# File 'lib/ar_doc_store/attributes/base.rb', line 8

def conversion
  @conversion
end

#defaultObject

Returns the value of attribute default.



8
9
10
# File 'lib/ar_doc_store/attributes/base.rb', line 8

def default
  @default
end

#modelObject

Returns the value of attribute model.



8
9
10
# File 'lib/ar_doc_store/attributes/base.rb', line 8

def model
  @model
end

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/ar_doc_store/attributes/base.rb', line 8

def options
  @options
end

#predicateObject

Returns the value of attribute predicate.



8
9
10
# File 'lib/ar_doc_store/attributes/base.rb', line 8

def predicate
  @predicate
end

Class Method Details

.build(model, attribute, options = {}) ⇒ Object



13
14
15
# File 'lib/ar_doc_store/attributes/base.rb', line 13

def self.build(model, attribute, options={})
  new(model, attribute, options).build
end

Instance Method Details

#add_ransackerObject



59
60
61
62
63
# File 'lib/ar_doc_store/attributes/base.rb', line 59

def add_ransacker
  model.class_eval <<-CODE, __FILE__, __LINE__ + 1
    add_ransacker :#{attribute}, "#{predicate}"
  CODE
end

#attribute_optionsObject



51
52
53
# File 'lib/ar_doc_store/attributes/base.rb', line 51

def attribute_options
  {}
end

#attribute_typeObject



47
48
49
# File 'lib/ar_doc_store/attributes/base.rb', line 47

def attribute_type
  ActiveModel::Type::Value
end

#buildObject



22
23
24
25
26
27
# File 'lib/ar_doc_store/attributes/base.rb', line 22

def build
  run_callbacks :build do
    store_attribute
  end
  self
end

#define_query_methodObject



65
66
67
68
69
70
71
# File 'lib/ar_doc_store/attributes/base.rb', line 65

def define_query_method
  model.class_eval <<-CODE, __FILE__, __LINE__ + 1
    def #{attribute}?
      !!#{attribute}
    end
  CODE
end

#embedded?Boolean

Returns:



55
56
57
# File 'lib/ar_doc_store/attributes/base.rb', line 55

def embedded?
  false
end

#store_attributeObject

:nodoc:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ar_doc_store/attributes/base.rb', line 30

def store_attribute
  attribute_name = @attribute
  attribute_type = self.attribute_type
  options = attribute_options
  options.merge!(default: default) if default.present?
  model.class_eval do
    attribute attribute_name, attribute_type.new, **options
    # define_method "#{attribute_name}=".to_sym, -> (value) {
    #   value = nil if value == '' || value == ['']
    #   write_attribute attribute_name, value
    #   # new_value = read_attribute attribute_name
    #   # write_store_attribute(json_column, attribute_name, new_value)
    #   attributes[attribute_name]
    # }
  end
end