Class: ArDocStore::Attributes::Base
- Inherits:
-
Object
- Object
- ArDocStore::Attributes::Base
- Includes:
- CallbackSupport
- Defined in:
- lib/ar_doc_store/attributes/base.rb
Direct Known Subclasses
Array, Boolean, Date, Datetime, Decimal, EmbedsBase, Enumeration, Float, Integer, String
Instance Attribute Summary collapse
-
#attribute ⇒ Object
Returns the value of attribute attribute.
-
#conversion ⇒ Object
Returns the value of attribute conversion.
-
#default ⇒ Object
Returns the value of attribute default.
-
#model ⇒ Object
Returns the value of attribute model.
-
#options ⇒ Object
Returns the value of attribute options.
-
#predicate ⇒ Object
Returns the value of attribute predicate.
Class Method Summary collapse
Instance Method Summary collapse
- #add_ransacker ⇒ Object
- #attribute_options ⇒ Object
- #attribute_type ⇒ Object
- #build ⇒ Object
- #define_query_method ⇒ Object
- #embedded? ⇒ Boolean
-
#initialize(model, attribute, options) ⇒ Base
constructor
A new instance of Base.
-
#store_attribute ⇒ Object
:nodoc:.
Methods included from CallbackSupport
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, ) @model, @attribute, = model, attribute, @default = .delete(:default) end |
Instance Attribute Details
#attribute ⇒ Object
Returns the value of attribute attribute.
8 9 10 |
# File 'lib/ar_doc_store/attributes/base.rb', line 8 def attribute @attribute end |
#conversion ⇒ Object
Returns the value of attribute conversion.
8 9 10 |
# File 'lib/ar_doc_store/attributes/base.rb', line 8 def conversion @conversion end |
#default ⇒ Object
Returns the value of attribute default.
8 9 10 |
# File 'lib/ar_doc_store/attributes/base.rb', line 8 def default @default end |
#model ⇒ Object
Returns the value of attribute model.
8 9 10 |
# File 'lib/ar_doc_store/attributes/base.rb', line 8 def model @model end |
#options ⇒ Object
Returns the value of attribute options.
8 9 10 |
# File 'lib/ar_doc_store/attributes/base.rb', line 8 def end |
#predicate ⇒ Object
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, ={}) new(model, attribute, ).build end |
Instance Method Details
#add_ransacker ⇒ Object
58 59 60 61 62 |
# File 'lib/ar_doc_store/attributes/base.rb', line 58 def add_ransacker model.class_eval " add_ransacker :\#{attribute}, \"\#{predicate}\"\n CODE\nend\n", __FILE__, __LINE__ + 1 |
#attribute_options ⇒ Object
50 51 52 |
# File 'lib/ar_doc_store/attributes/base.rb', line 50 def {} end |
#attribute_type ⇒ Object
46 47 48 |
# File 'lib/ar_doc_store/attributes/base.rb', line 46 def attribute_type ActiveModel::Type::Value end |
#build ⇒ Object
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_method ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/ar_doc_store/attributes/base.rb', line 64 def define_query_method model.class_eval " def \#{attribute}?\n !!\#{attribute}\n end\n CODE\nend\n", __FILE__, __LINE__ + 1 |
#store_attribute ⇒ Object
:nodoc:
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/ar_doc_store/attributes/base.rb', line 30 def store_attribute attribute_name = @attribute attribute_type = self.attribute_type = .merge!(default: default) if default.present? model.class_eval do attribute attribute_name, attribute_type.new, ** 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) } end end |