Class: EgovUtils::Fileuid

Inherits:
Object
  • Object
show all
Defined in:
lib/egov_utils/fileuid.rb

Defined Under Namespace

Classes: Coder, Snippet, Type

Constant Summary collapse

DASH_SNIPPET =
Snippet.new('-', :static)
SLASH_SNIPPET =
Snippet.new('/', :static)
BC_SNIPPET =
Snippet.new('bc', :integer, :any)
YEAR_SNIPPET =
Snippet.new('year', :integer, 4)
REGISTER_SNIPPET =
Snippet.new('agenda', '[-a-zA-Z]', [1,10])
COURT_AGEND_SNIPPET =
Snippet.new('agenda', :word, [1,4])
COURT_SENAT_SNIPPET =
Snippet.new('senat', :integer, :any)
TYPES =
{
  'court' => Type.new(COURT_SENAT_SNIPPET, DASH_SNIPPET, COURT_AGEND_SNIPPET, DASH_SNIPPET, BC_SNIPPET, SLASH_SNIPPET, YEAR_SNIPPET),
  'msp'   => Type.new(BC_SNIPPET, SLASH_SNIPPET, YEAR_SNIPPET, DASH_SNIPPET, REGISTER_SNIPPET)
}
AVAILABLE_ATTRIBUTES =
[:bc, :agenda, :senat, :year, :document_number]

Instance Method Summary collapse

Constructor Details

#initialize(str_val, **options) ⇒ Fileuid

Returns a new instance of Fileuid.



134
135
136
137
138
# File 'lib/egov_utils/fileuid.rb', line 134

def initialize(str_val, **options)
  @options = options.stringify_keys
  @str_val = str_val
  parse_str!(str_val) if str_val.is_a?(String)
end

Instance Method Details

#==(other) ⇒ Object



118
119
120
# File 'lib/egov_utils/fileuid.rb', line 118

def ==(other)
  other.is_a?(Fileuid) && AVAILABLE_ATTRIBUTES.all?{|a| self.public_send(a) == other.public_send(a) }
end

#as_json(**options) ⇒ Object



159
160
161
# File 'lib/egov_utils/fileuid.rb', line 159

def as_json(**options)
  invalid? ? nil : to_s
end

#determine_type(str_val) ⇒ Object



152
153
154
155
156
157
# File 'lib/egov_utils/fileuid.rb', line 152

def determine_type(str_val)
  TYPES.each do |k, type|
    return k if str_val =~ type.to_regex
  end
  nil
end

#invalid?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/egov_utils/fileuid.rb', line 130

def invalid?
  !type || @invalid
end

#parse_str!(str_val, type = self.type) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/egov_utils/fileuid.rb', line 140

def parse_str!(str_val, type=self.type)
  type ||= determine_type(str_val)
  @type = type
  return if invalid?
  match_data = str_val.match(type_definition.to_regex)
  if match_data
    type_definition.snippet_names.each_with_index{|s_name, idx| self.send(s_name+'=', match_data[idx+1]) }
  else
    @invalid = true
  end
end

#to_sObject



163
164
165
166
167
168
169
# File 'lib/egov_utils/fileuid.rb', line 163

def to_s
  if invalid?
    @str_val.to_s
  else
    type_definition.file_uid_to_s(self)
  end
end

#typeObject



122
123
124
# File 'lib/egov_utils/fileuid.rb', line 122

def type
  @type ||= @options['type']
end

#type_definitionObject



126
127
128
# File 'lib/egov_utils/fileuid.rb', line 126

def type_definition
  TYPES[type]
end