Class: RareMap::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/rare_map/options.rb

Overview

RareMap::Options defines all available options of RareMap.

Author:

  • Wei-Ming Wu

Constant Summary collapse

DEFAULT_GROUP =

A default group name

'default'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_opts = {}) ⇒ Options

Creates a Options.

Parameters:

  • raw_opts (Hash) (defaults to: {})

    the details of options



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/rare_map/options.rb', line 17

def initialize(raw_opts = {})
  raw_opts ||= {}
  raw_opts = raw_opts.with_indifferent_access
  @opts = { group: DEFAULT_GROUP,
            primary_key: {},
            foreign_key: { suffix: nil, alias: {} } }.with_indifferent_access
            
  if raw_opts.kind_of? Hash
    if raw_opts[:group]
      @opts[:group] = raw_opts[:group]
    end
    if raw_opts[:primary_key].kind_of? Hash
      @opts[:primary_key] = raw_opts[:primary_key].select { |k, v| k.kind_of? String and v.kind_of? String }
    end
    if raw_opts[:foreign_key].kind_of? Hash and raw_opts[:foreign_key][:suffix].kind_of? String
       @opts[:foreign_key][:suffix] = raw_opts[:foreign_key][:suffix]
    end
    if raw_opts[:foreign_key].kind_of? Hash and raw_opts[:foreign_key][:alias].kind_of? Hash
       @opts[:foreign_key][:alias] = raw_opts[:foreign_key][:alias].select { |k, v| k.kind_of? String and v.kind_of? String }
    end
    if raw_opts[:group].kind_of? String
      @opts[:group] = raw_opts[:group]
    end
  end
end

Instance Attribute Details

#optsHash (readonly)

Returns the details of options.

Returns:

  • (Hash)

    the details of options



8
9
10
11
12
13
14
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rare_map/options.rb', line 8

class Options
  # A default group name
  DEFAULT_GROUP = 'default'
  attr_reader :opts
  
  # Creates a Options.
  #
  # @param raw_opts [Hash] the details of options
  # @return [Options] a Options object
  def initialize(raw_opts = {})
    raw_opts ||= {}
    raw_opts = raw_opts.with_indifferent_access
    @opts = { group: DEFAULT_GROUP,
              primary_key: {},
              foreign_key: { suffix: nil, alias: {} } }.with_indifferent_access
              
    if raw_opts.kind_of? Hash
      if raw_opts[:group]
        @opts[:group] = raw_opts[:group]
      end
      if raw_opts[:primary_key].kind_of? Hash
        @opts[:primary_key] = raw_opts[:primary_key].select { |k, v| k.kind_of? String and v.kind_of? String }
      end
      if raw_opts[:foreign_key].kind_of? Hash and raw_opts[:foreign_key][:suffix].kind_of? String
         @opts[:foreign_key][:suffix] = raw_opts[:foreign_key][:suffix]
      end
      if raw_opts[:foreign_key].kind_of? Hash and raw_opts[:foreign_key][:alias].kind_of? Hash
         @opts[:foreign_key][:alias] = raw_opts[:foreign_key][:alias].select { |k, v| k.kind_of? String and v.kind_of? String }
      end
      if raw_opts[:group].kind_of? String
        @opts[:group] = raw_opts[:group]
      end
    end
  end
  
  # Checks if this Options belongs to a group.
  #
  # @return [true, false] true if this Options contains a group, false otherwise
  def group?
    @opts[:group] != DEFAULT_GROUP
  end
  
  # Returns the name of this Options' group
  #
  # @return [String] the name of this Options' group
  def group
    @opts[:group] || DEFAULT_GROUP
  end
  
  # Returns the primary key of a table specified by this Options
  #
  # @return [String, nil] the primary key of a table specified by this Options
  def find_primary_key_by_table(table_name)
    @opts[:primary_key].values_at(table_name).first
  end
  
  # Returns the table of a foreign key specified by this Options
  #
  # @return [String, nil] the table of a foreign key specified by this Options
  def find_table_by_foreign_key(column_name)
    @opts[:foreign_key][:alias].values_at(column_name).first
  end
  
  # Returns the suffix of a foreign key should have
  #
  # @return [String, nil] the suffix of a foreign key should have
  def fk_suffix
    @opts[:foreign_key][:suffix]
  end
end

Instance Method Details

#find_primary_key_by_table(table_name) ⇒ String?

Returns the primary key of a table specified by this Options

Returns:

  • (String, nil)

    the primary key of a table specified by this Options



60
61
62
# File 'lib/rare_map/options.rb', line 60

def find_primary_key_by_table(table_name)
  @opts[:primary_key].values_at(table_name).first
end

#find_table_by_foreign_key(column_name) ⇒ String?

Returns the table of a foreign key specified by this Options

Returns:

  • (String, nil)

    the table of a foreign key specified by this Options



67
68
69
# File 'lib/rare_map/options.rb', line 67

def find_table_by_foreign_key(column_name)
  @opts[:foreign_key][:alias].values_at(column_name).first
end

#fk_suffixString?

Returns the suffix of a foreign key should have

Returns:

  • (String, nil)

    the suffix of a foreign key should have



74
75
76
# File 'lib/rare_map/options.rb', line 74

def fk_suffix
  @opts[:foreign_key][:suffix]
end

#groupString

Returns the name of this Options’ group

Returns:

  • (String)

    the name of this Options’ group



53
54
55
# File 'lib/rare_map/options.rb', line 53

def group
  @opts[:group] || DEFAULT_GROUP
end

#group?true, false

Checks if this Options belongs to a group.

Returns:

  • (true, false)

    true if this Options contains a group, false otherwise



46
47
48
# File 'lib/rare_map/options.rb', line 46

def group?
  @opts[:group] != DEFAULT_GROUP
end