Class: LcpRuby::JsonItemWrapper
- Inherits:
-
Object
- Object
- LcpRuby::JsonItemWrapper
- Includes:
- ActiveModel::Model
- Defined in:
- lib/lcp_ruby/json_item_wrapper.rb
Overview
Wraps a plain hash (one item from a JSON column array) with ActiveModel::Model so it can participate in validations and provide typed getter/setter access based on a ModelDefinition.
Usage:
wrapper = JsonItemWrapper.new({"name" => "Alice"}, model_def)
wrapper.name # => "Alice"
wrapper.name = "Bob"
wrapper.valid? # runs model_def validations
wrapper.to_hash # => {"name" => "Bob"}
Instance Attribute Summary collapse
-
#model_definition ⇒ Object
readonly
Returns the value of attribute model_definition.
Instance Method Summary collapse
-
#initialize(data = {}, model_definition = nil) ⇒ JsonItemWrapper
constructor
A new instance of JsonItemWrapper.
- #method_missing(method_name, *args) ⇒ Object
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Dynamic field access — falls back to hash lookup.
-
#to_hash ⇒ Object
(also: #to_h)
Convert back to a plain hash for JSON persistence.
-
#validate_with_model_rules! ⇒ Object
Apply validations from the model definition using ActiveModel’s validation framework.
Constructor Details
#initialize(data = {}, model_definition = nil) ⇒ JsonItemWrapper
Returns a new instance of JsonItemWrapper.
17 18 19 20 21 22 |
# File 'lib/lcp_ruby/json_item_wrapper.rb', line 17 def initialize(data = {}, model_definition = nil) @data = (data || {}).transform_keys(&:to_s) @model_definition = model_definition apply_defaults! if model_definition define_accessors! if model_definition end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/lcp_ruby/json_item_wrapper.rb', line 34 def method_missing(method_name, *args) name = method_name.to_s if name.end_with?("=") @data[name.chomp("=")] = args.first elsif @data.key?(name) @data[name] else super end end |
Instance Attribute Details
#model_definition ⇒ Object (readonly)
Returns the value of attribute model_definition.
15 16 17 |
# File 'lib/lcp_ruby/json_item_wrapper.rb', line 15 def model_definition @model_definition end |
Instance Method Details
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Dynamic field access — falls back to hash lookup
25 26 27 28 29 30 31 32 |
# File 'lib/lcp_ruby/json_item_wrapper.rb', line 25 def respond_to_missing?(method_name, include_private = false) name = method_name.to_s if name.end_with?("=") true else @data.key?(name) || super end end |
#to_hash ⇒ Object Also known as: to_h
Convert back to a plain hash for JSON persistence
46 47 48 |
# File 'lib/lcp_ruby/json_item_wrapper.rb', line 46 def to_hash @data.dup end |
#validate_with_model_rules! ⇒ Object
Apply validations from the model definition using ActiveModel’s validation framework. This mirrors how ValidationApplicator works for AR models, ensuring consistent semantics (error messages, options). Returns true if no errors, false otherwise.
55 56 57 58 59 60 |
# File 'lib/lcp_ruby/json_item_wrapper.rb', line 55 def validate_with_model_rules! return true unless model_definition apply_dynamic_validations! valid? end |