Class: Hamachi::Model

Inherits:
Hash
  • Object
show all
Defined in:
lib/hamachi/source/model.rb

Constant Summary collapse

Boolean =
enum(true, false)

Class Method Summary collapse

Instance Method Summary collapse

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, options = {})
  update(snapshot) if options.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, options)
  end

  validate_fields! if options.fetch(:validate_fields, true)
  freeze if options.fetch(:freeze, false)
end

Class Method Details

.field(name, options) ⇒ Object

Raises:

  • (ArgumentError)


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, options)
  raise ArgumentError, "method ##{name} already defined" if method_defined?(name)
  raise ArgumentError, "method ##{name}= already defined" if method_defined?("#{name}=")

  field = options.fetch(:type)
  field = Field.new(field) unless Field === field
  field.initialize_options(options).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

.fieldsObject

——- 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, options = {})
  return snapshot unless Hash === snapshot
  self.new snapshot, options
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, options = {})
  snapshot = JSON.parse(string, symbolize_names: true)
  if Array === snapshot
    snapshot.map { |each| from_snapshot each, options }
  else
    from_snapshot snapshot, options
  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_sObject



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?
  gen_error_messages { return false }
  return true
end

#validate_fields!Object



100
101
102
# File 'lib/hamachi/source/model.rb', line 100

def validate_fields!
  gen_error_messages { |error| raise error }
end