Module: Lutaml::Model::Liquefiable::ClassMethods
- Included in:
- Serialize::ClassMethods
- Defined in:
- lib/lutaml/model/liquefiable.rb
Instance Attribute Summary collapse
Instance Method Summary collapse
- #base_drop_class ⇒ Object
- #custom_liquid_class_name ⇒ Object
- #drop_class ⇒ Object
- #drop_class_name ⇒ Object
- #generate_mapping_methods ⇒ Object
- #inherited(child) ⇒ Object
- #liquid(&block) ⇒ Object
- #liquid_class(class_name) ⇒ Object
- #liquid_mappings ⇒ Object
- #register_class_if_liquid_defined ⇒ Object
- #register_drop_method(method_name) ⇒ Object
- #register_liquid_drop_class ⇒ Object
- #register_methods ⇒ Object
- #to_liquid_class ⇒ Object
- #validate_liquid! ⇒ Object
Instance Attribute Details
#mappings ⇒ Object
137 138 139 |
# File 'lib/lutaml/model/liquefiable.rb', line 137 def mappings @mappings ||= {} end |
Instance Method Details
#base_drop_class ⇒ Object
68 69 70 71 72 |
# File 'lib/lutaml/model/liquefiable.rb', line 68 def base_drop_class const_get(drop_class_name) rescue StandardError nil end |
#custom_liquid_class_name ⇒ Object
32 33 34 |
# File 'lib/lutaml/model/liquefiable.rb', line 32 def custom_liquid_class_name @custom_liquid_class_name end |
#drop_class ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/lutaml/model/liquefiable.rb', line 74 def drop_class if custom_liquid_class_name begin return Object.const_get(custom_liquid_class_name) rescue NameError raise Lutaml::Model::LiquidClassNotFoundError, custom_liquid_class_name end end base_drop_class end |
#drop_class_name ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/lutaml/model/liquefiable.rb', line 60 def drop_class_name @drop_class_name ||= if name "#{name.split('::').last}Drop" else "Drop" end end |
#generate_mapping_methods ⇒ Object
115 116 117 118 119 120 121 122 123 |
# File 'lib/lutaml/model/liquefiable.rb', line 115 def generate_mapping_methods return unless liquid_mappings&.mappings liquid_mappings.mappings.each do |key, method_name| base_drop_class.define_method(key) do liquefy_value(@object.public_send(method_name)) end end end |
#inherited(child) ⇒ Object
14 15 16 17 18 |
# File 'lib/lutaml/model/liquefiable.rb', line 14 def inherited(child) super child.register_class_if_liquid_defined child.mappings = Utils.deep_dup(mappings) || {} end |
#liquid(&block) ⇒ Object
125 126 127 128 129 130 131 |
# File 'lib/lutaml/model/liquefiable.rb', line 125 def liquid(&block) return unless Object.const_defined?(:Liquid) mappings[:liquid] ||= ::Lutaml::Model::Liquid::Mapping.new mappings[:liquid].instance_eval(&block) if block mappings[:liquid] end |
#liquid_class(class_name) ⇒ Object
28 29 30 |
# File 'lib/lutaml/model/liquefiable.rb', line 28 def liquid_class(class_name) @custom_liquid_class_name = class_name end |
#liquid_mappings ⇒ Object
133 134 135 |
# File 'lib/lutaml/model/liquefiable.rb', line 133 def liquid_mappings mappings[:liquid] end |
#register_class_if_liquid_defined ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/lutaml/model/liquefiable.rb', line 20 def register_class_if_liquid_defined return unless is_a?(Class) return unless Object.const_defined?(:Liquid) return if base_drop_class register_liquid_drop_class end |
#register_drop_method(method_name) ⇒ Object
107 108 109 110 111 112 113 |
# File 'lib/lutaml/model/liquefiable.rb', line 107 def register_drop_method(method_name) return if base_drop_class.method_defined?(method_name) base_drop_class.define_method(method_name) do liquefy_value(@object.public_send(method_name)) end end |
#register_liquid_drop_class ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/lutaml/model/liquefiable.rb', line 36 def register_liquid_drop_class validate_liquid! if base_drop_class raise Lutaml::Model::LiquidDropAlreadyRegisteredError, drop_class_name end const_set(drop_class_name, Class.new(::Liquid::Drop) do def initialize(object) super() @object = object end def liquefy_value(value) if value.is_a?(Array) value.map(&:to_liquid) else value.to_liquid end end end) end |
#register_methods ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/lutaml/model/liquefiable.rb', line 93 def register_methods @methods_generated = true if self <= Serializable raise Lutaml::Model::NoAttributesDefinedLiquidError.new(name) if attributes.empty? register_drop_method(:element_order) if method_defined?(:element_order) attributes&.each_key { |attr_name| register_drop_method(attr_name) } end generate_mapping_methods end |
#to_liquid_class ⇒ Object
87 88 89 90 91 |
# File 'lib/lutaml/model/liquefiable.rb', line 87 def to_liquid_class register_methods unless @methods_generated base_drop_class end |
#validate_liquid! ⇒ Object
141 142 143 144 145 |
# File 'lib/lutaml/model/liquefiable.rb', line 141 def validate_liquid! return if Object.const_defined?(:Liquid) raise Lutaml::Model::LiquidNotEnabledError end |