Class: Schema

Inherits:
Object
  • Object
show all
Includes:
DataMapper::Resource, Dataflow
Defined in:
lib/yogo/model/schema.rb

Constant Summary collapse

REQUIRED_JSON_KEYS =
[:name, :operations]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse_json(body) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/yogo/model/schema.rb', line 74

def self.parse_json(body)
  json = JSON.parse(body)

  ret = { :name => json['name'], :operations => json['operations'] }

  return nil if REQUIRED_JSON_KEYS.any? { |r| ret[r].nil? }
  
  ret
end

Instance Method Details

#as_json(*a) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/yogo/model/schema.rb', line 64

def as_json(*a)
  {
    :guid => self.to_url,
    :name => self.name,
    :operations => self.operation_definitions,
  }
end

#data_modelObject



43
44
45
# File 'lib/yogo/model/schema.rb', line 43

def data_model
  @data_model ||= by_need { gen_model }
end

#operation(op_name, *args) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/yogo/model/schema.rb', line 34

def operation(op_name, *args)
  op_def = replace_nil_items([op_name.to_s, args].flatten)
  
  unless self.operation_definitions.include?(op_def)
    # We need to dup this else the model doesn't get marked as dirty and won't save.
    self.operation_definitions =  self.operation_definitions.dup << op_def
  end
end

#operations=(ops) ⇒ Object

Set only the operations we want, don’t add to them



27
28
29
30
31
32
# File 'lib/yogo/model/schema.rb', line 27

def operations=(ops)
  # Clear the current operations
  self.operation_definitions = []
  # Load operations with the set given
  ops.each{|op| operation(*op) }
end

#to_json(*args) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/yogo/model/schema.rb', line 51

def to_json(*args)
  options = args.first || {}
  options = options.to_h if options.respond_to?(:to_h)
  
  result = as_json(*args)
  
  if options.fetch(:to_json, true)
    result.to_json
  else
    result
  end
end

#to_procObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/yogo/model/schema.rb', line 84

def to_proc
  base_op = Yogo::DataMapper::Model::Operations['add/default_properties']
  # base_op = Yogo::DataMapper::Model::Operations['add/yogo_methods']
  ops = operation_definitions.map{|op_def| Yogo::DataMapper::Model::Operations[op_def.first] }
  partial_ops = []
  ops.each_with_index do |op, i|
    next unless op
    partial_ops[i] = op.partial(X, *operation_definitions[i][1..-1])
  end
  partial_ops << Yogo::DataMapper::Model::Operations['add/yogo_methods']
  partial_ops.compact!
  partial_ops.reduce(base_op){|composed, partial_op| composed * partial_op}

end

#to_urlObject



47
48
49
# File 'lib/yogo/model/schema.rb', line 47

def to_url
  "/schema/#{self.name}"
end