Class: RailsDataGenerator

Inherits:
GeneratorBase show all
Defined in:
lib/metarecord/generators/rails/data_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from GeneratorBase

#_append, _use_by_files, _use_by_models, #get_classname, #get_pluralized_name, #get_singular_name, #get_type, #get_value, #id_type, #indent, #make_block, #null_id, prepare, #ptr_type, #should_generate_for, #should_generate_from_manifest, #should_skip_on_client?, sourcefile_to_destfile, #unindent, use

Class Method Details

.extensionObject



139
# File 'lib/metarecord/generators/rails/data_generator.rb', line 139

def extension ; ".rb" ; end

.is_file_based?Boolean

Returns:



6
# File 'lib/metarecord/generators/rails/data_generator.rb', line 6

def is_file_based? ; false ; end

.make_file(filename, data) ⇒ Object



141
142
143
144
145
146
# File 'lib/metarecord/generators/rails/data_generator.rb', line 141

def make_file filename, data
  <<RUBY
module #{METARECORD_NAMESPACE}
#{data[:bodies].join "\n"}end
RUBY
end

Instance Method Details

#generate_default_authorized_propertiesObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/metarecord/generators/rails/data_generator.rb', line 43

def generate_default_authorized_properties
  _append "class << self"
  indent do
    _append "def permitted_attributes"
    indent do
      symbols = @default_authorized_properties.collect do |v| v.to_sym.inspect end
      _append "[#{symbols.join ','}]"
    end
    _append "end"
  end
  _append "end"
end

#generate_for(object) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/metarecord/generators/rails/data_generator.rb', line 26

def generate_for object
  reset
  @inherits = object[:inherits]
  indent do
    _append "class #{object[:name]} < #{super_class}"
    indent do
      _append "self.abstract_class = true"
      _append ""
      self.instance_eval &object[:block]
      _append ""
      generate_default_authorized_properties
    end
    _append "end"
  end
  @src
end

#has_many(type, name, options = {}) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/metarecord/generators/rails/data_generator.rb', line 126

def has_many type, name, options = {}
  db_options = options[:db] || {}
  if options[:joined] != false
    suffix = []
    suffix << "class_name: #{type.to_s.inspect}"
    suffix << "dependent: #{options[:dependent].to_sym.inspect}" if options[:dependent] && options[:dependent].to_sym != :unlink
    _append "has_many #{name.to_sym.inspect}, #{suffix.join ','}"
  else
    throw "id based has_many is not supported by the rails generator"
  end
end

#has_one(type, name, options = {}) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/metarecord/generators/rails/data_generator.rb', line 114

def has_one type, name, options = {}
  db_options  = options[:db] || {}
  foreign_key = db_options[:column] || "#{name}_id"
  _append "belongs_to #{name.to_sym.inspect},"
  indent do
    optional = if db_options[:null].nil? then true else db_options[:null] end
    _append "class_name: #{type.to_s.inspect},"
    _append "foreign_key: #{foreign_key.to_s.inspect},"
    _append "optional: #{optional}"
  end
end

#model_base_classObject



9
10
11
12
13
14
15
# File 'lib/metarecord/generators/rails/data_generator.rb', line 9

def model_base_class
  if defined? RAILS_RECORD_BASE
    RAILS_RECORD_BASE
  else
    "ActiveRecord::Base"
  end
end

#order_by(name, flow = nil) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/metarecord/generators/rails/data_generator.rb', line 87

def order_by name, flow = nil
  src = if flow.nil?
    name.to_s.inspect
  else
    "#{name}: :#{flow}"
  end
  _append "scope :default_order, -> { order(#{src}) }"
end

#property(type, name, options = {}) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/metarecord/generators/rails/data_generator.rb', line 96

def property type, name, options = {}
  has_custom_column_name = !options[:column].nil?
  rails_name = (options[:column] || name).to_s
  if type == 'DataTree'
    _append "store #{rails_name.inspect}, coder: JSON"
    _append "def #{name} ; self.#{rails_name} ; end" if has_custom_column_name
  elsif has_custom_column_name
    _append "def #{name}"
    indent do _append "self.#{options[:column]}" end
    _append "end"
    _append "def #{name}=(value)"
    indent do _append "self.#{options[:column]} = value"end
    _append "end"
  end
  validation type, rails_name, options[:validate] unless options[:validate].nil?
  @default_authorized_properties << name unless options[:read_only]
end

#resetObject



21
22
23
24
# File 'lib/metarecord/generators/rails/data_generator.rb', line 21

def reset
  super
  @default_authorized_properties = []
end

#resource_name(name) ⇒ Object



83
84
85
# File 'lib/metarecord/generators/rails/data_generator.rb', line 83

def resource_name name
  _append "RESOURCE_NAME = #{name.to_s.inspect}"
end

#super_classObject



17
18
19
# File 'lib/metarecord/generators/rails/data_generator.rb', line 17

def super_class
  if @inherits.nil? then model_base_class else @inherits end
end

#validation(type, name, data) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/metarecord/generators/rails/data_generator.rb', line 56

def validation type, name, data
  src = "validates #{name.to_s.inspect}"
  src += ", presence: true"     if data[:required] == true
  src += ", allow_blank: false" if data[:required] && type == "std::string"
  src += ", uniqueness: true"   if data[:uniqueness] == true
  if !data[:min].nil? || !data[:max].nil?
    src += validation_numericality name, data
  end
  _append src
end

#validation_numericality(name, data) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/metarecord/generators/rails/data_generator.rb', line 67

def validation_numericality name, data
  src = ", numericality: { "
  if !data[:min].nil?
    src += "greater_than_or_equal_to: #{data[:min]}"
  end
  if !data[:max].nil?
    src += ", " if !data[:min].nil?
    src += "less_than_or_equal_to: #{data[:max]}"
  end
  src += " }"
  src
end

#visibility(value) ⇒ Object



80
81
# File 'lib/metarecord/generators/rails/data_generator.rb', line 80

def visibility value
end