Class: Lurker::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/lurker/schema.rb

Constant Summary collapse

KEY =
'extensions'
DESCRPTIONS =
{
  'index' => 'listing',
  'show' => '',
  'edit' => 'editing',
  'create' => 'creation',
  'update' => 'updating',
  'destroy' => 'descruction'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json_schema_hash, extensions = {}) ⇒ Schema

Returns a new instance of Schema.



17
18
19
20
21
22
23
24
# File 'lib/lurker/schema.rb', line 17

def initialize(json_schema_hash, extensions={})
  @hash = json_schema_hash
  @extensions = if extensions.blank? && @hash.has_key?(KEY)
    @hash.delete(KEY) || {}
  else
    extensions
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



30
31
32
# File 'lib/lurker/schema.rb', line 30

def method_missing(method, *args, &block)
  @hash.send method, *args, &block
end

Instance Attribute Details

#extensionsObject (readonly)

Returns the value of attribute extensions.



15
16
17
# File 'lib/lurker/schema.rb', line 15

def extensions
  @extensions
end

Instance Method Details

#diff(schema) ⇒ Object



34
35
36
37
38
39
# File 'lib/lurker/schema.rb', line 34

def diff(schema)
  ::Diffy::Diff.new(
    schema.serialized_for_diff,
    serialized_for_diff,
    context: 1).to_s(:color)
end

#ordered!Object



63
64
65
66
67
# File 'lib/lurker/schema.rb', line 63

def ordered!
  @hash = Hash[@hash.sort]
  @extensions = Hash[@extensions.sort]
  self
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/lurker/schema.rb', line 26

def respond_to_missing?(method, include_private = false)
  @hash.send(:respond_to_missing?, method, include_private)
end

#to_yamlObject



57
58
59
60
61
# File 'lib/lurker/schema.rb', line 57

def to_yaml
  YAML.dump(@hash.merge(
    KEY => @extensions
  ))
end

#write_to(path) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/lurker/schema.rb', line 41

def write_to(path)
  if @hash['prefix'].blank?
    @hash['prefix'] = "#{default_subject} management"
  end
  if @hash['description'].blank?
    @hash['description'] = default_descrption.strip
  end

  dirname = File.dirname(path)
  FileUtils.mkdir_p(dirname) unless File.directory?(dirname)

  File.open(path, "w") do |file|
    file.write(to_yaml)
  end
end