Class: Masamune::Schema::Store

Inherits:
Object
  • Object
show all
Includes:
HasEnvironment
Defined in:
lib/masamune/schema/store.rb

Constant Summary collapse

SUPPORTED_ATTRIBUTES =
%(table dimension fact file).freeze
DEFAULT_ATTRIBUTES =
{
  type:            nil,
  format:          ->(store) { default_format(store) },
  json_encoding:   ->(store) { default_json_encoding(store) },
  headers:         ->(store) { default_headers(store) },
  debug:           false
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HasEnvironment

#environment, #environment=

Constructor Details

#initialize(environment, opts = {}) ⇒ Store

Returns a new instance of Store.

Raises:

  • (ArgumentError)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/masamune/schema/store.rb', line 58

def initialize(environment, opts = {})
  self.environment = environment
  opts.symbolize_keys!
  raise ArgumentError, 'required parameter type: missing' unless opts.key?(:type)
  raise ArgumentError, "unknown type: '#{opts[:type]}'" unless self.class.types.include?(opts[:type])
  DEFAULT_ATTRIBUTES.merge(opts).each do |name, value|
    public_send("#{name}=", value.respond_to?(:call) ? value.call(self) : value)
  end

  @tables     = {}.with_indifferent_access
  @dimensions = {}.with_indifferent_access
  @facts      = {}.with_indifferent_access
  @files      = {}.with_indifferent_access
  @references = {}.with_indifferent_access
  @extra      = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *_args) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/masamune/schema/store.rb', line 75

def method_missing(method_name, *_args)
  *attribute_name, attribute_type = method_name.to_s.split('_')
  if type == :files
    files[method_name]
  elsif SUPPORTED_ATTRIBUTES.include?(attribute_type)
    send(attribute_type.pluralize)[attribute_name.join('_')]
  else
    super
  end
end

Instance Attribute Details

#dimensionsObject

Returns the value of attribute dimensions.



47
48
49
# File 'lib/masamune/schema/store.rb', line 47

def dimensions
  @dimensions
end

#factsObject

Returns the value of attribute facts.



48
49
50
# File 'lib/masamune/schema/store.rb', line 48

def facts
  @facts
end

#filesObject

Returns the value of attribute files.



49
50
51
# File 'lib/masamune/schema/store.rb', line 49

def files
  @files
end

#referencesObject

Returns the value of attribute references.



50
51
52
# File 'lib/masamune/schema/store.rb', line 50

def references
  @references
end

#tablesObject

Returns the value of attribute tables.



46
47
48
# File 'lib/masamune/schema/store.rb', line 46

def tables
  @tables
end

Class Method Details

.typesObject



53
54
55
# File 'lib/masamune/schema/store.rb', line 53

def types
  i[postgres hive files]
end

Instance Method Details

#dereference_column(id, options = {}) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/masamune/schema/store.rb', line 97

def dereference_column(id, options = {})
  column_id, reference_id = id.to_s.split(/\./).reverse
  column_options = options.dup
  column_options[:id] = column_id

  if reference_id
    raise ArgumentError, "dimension #{reference_id} not defined" unless references[reference_id]
    column_options[:reference] = references[reference_id]
  end

  Masamune::Schema::Column.new(column_options)
end

#extra(order = nil) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/masamune/schema/store.rb', line 110

def extra(order = nil)
  return @extra unless order
  result = Set.new
  @extra.each do |file|
    filename = File.basename(file)
    if filename =~ /\A\d+_/
      number = filename.split('_').first.to_i
      result << file if number <= 0 && order == :pre
      result << file if number > 0 && order == :post
    elsif order == :pre
      result << file
    end
  end
  result.to_a
end

#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
90
91
92
93
94
95
# File 'lib/masamune/schema/store.rb', line 86

def respond_to_missing?(method_name, _include_private = false)
  *attribute_name, attribute_type = method_name.to_s.split('_')
  if type == :files
    files.key?(method_name)
  elsif SUPPORTED_ATTRIBUTES.include?(attribute_type)
    send(attribute_type.pluralize).key?(attribute_name.join('_'))
  else
    super
  end
end