Class: Hamachi::Model
- Inherits:
-
Hash
- Object
- Hash
- Hamachi::Model
- Defined in:
- lib/hamachi/source/model.rb
Constant Summary collapse
- Boolean =
enum(true, false)
Class Method Summary collapse
- .field(name, options) ⇒ Object
-
.fields ⇒ Object
——- schema declaration —————————————.
- .from_snapshot(snapshot, options = {}) ⇒ Object
- .parse(string, options = {}) ⇒ Object
- .register_type(name, field_class) ⇒ Object
-
.schema(&block) ⇒ Object
for anonymous inline models.
- .to_s ⇒ Object
Instance Method Summary collapse
-
#initialize(snapshot, options = {}) ⇒ Model
constructor
——- initialization ——————————————-.
-
#valid? ⇒ Boolean
——- validation ———————————————–.
- #validate_fields! ⇒ Object
Constructor Details
#initialize(snapshot, options = {}) ⇒ Model
——- initialization ——————————————-
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/hamachi/source/model.rb', line 66 def initialize(snapshot, = {}) update(snapshot) if .fetch(:include_unknown_fields, true) self.class.fields.each do |name, field| value = snapshot.fetch(name, field.default_value) self[name] = field.from_snapshot(value, ) end validate_fields! if .fetch(:validate_fields, true) freeze if .fetch(:freeze, false) end |
Class Method Details
.field(name, options) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/hamachi/source/model.rb', line 15 def self.field(name, ) raise ArgumentError, "method ##{name} already defined" if method_defined?(name) raise ArgumentError, "method ##{name}= already defined" if method_defined?("#{name}=") field = .fetch(:type) field = Field.new(field) unless Field === field field.().freeze self.fields[name.to_sym] = field class_eval %{ def #{name} self[:#{name}] end } class_eval %{ def #{name}=(value) field = self.class.fields[:#{name}] if not field === value raise "expected #{name} to be \#{field}, got \#{value.inspect}" end self[:#{name}] = value end } return self end |
.fields ⇒ Object
——- schema declaration —————————————
11 12 13 |
# File 'lib/hamachi/source/model.rb', line 11 def self.fields @fields ||= {} end |
.from_snapshot(snapshot, options = {}) ⇒ Object
78 79 80 81 |
# File 'lib/hamachi/source/model.rb', line 78 def self.from_snapshot(snapshot, = {}) return snapshot unless Hash === snapshot self.new snapshot, end |
.parse(string, options = {}) ⇒ Object
83 84 85 86 87 88 89 90 |
# File 'lib/hamachi/source/model.rb', line 83 def self.parse(string, = {}) snapshot = JSON.parse(string, symbolize_names: true) if Array === snapshot snapshot.map { |each| from_snapshot each, } else from_snapshot snapshot, end end |
.register_type(name, field_class) ⇒ Object
51 52 53 54 55 |
# File 'lib/hamachi/source/model.rb', line 51 def self.register_type(name, field_class) singleton_class.send(:define_method, name) do |*args| field_class.new(*args) end end |
.schema(&block) ⇒ Object
for anonymous inline models
43 44 45 |
# File 'lib/hamachi/source/model.rb', line 43 def self.schema(&block) # for anonymous inline models Class.new Hamachi::Model, &block end |
.to_s ⇒ Object
47 48 49 |
# File 'lib/hamachi/source/model.rb', line 47 def self.to_s name ? name : "schema(#{fields.map { |name, field| "#{name}:#{field}"}.join(',')})" end |
Instance Method Details
#valid? ⇒ Boolean
——- validation ———————————————–
95 96 97 98 |
# File 'lib/hamachi/source/model.rb', line 95 def valid? { return false } return true end |
#validate_fields! ⇒ Object
100 101 102 |
# File 'lib/hamachi/source/model.rb', line 100 def validate_fields! { |error| raise error } end |