Class: Multilocale::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/multilocale/project.rb

Overview

A project: the unit an API key is scoped to, and the thing that owns the locale list every download is written from.

The wire format is the API's camelCase document; the readers are the Ruby names. #to_h always returns the untouched document, so a round-trip through this class never drops a field the API added yesterday.

Constant Summary collapse

ATTRIBUTE_NAMES =
{
  id: "_id",
  name: "name",
  default_locale: "defaultLocale",
  locales: "locales",
  context: "context",
  paths: "paths"
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Project

Returns a new instance of Project.



13
14
15
# File 'lib/multilocale/project.rb', line 13

def initialize(attributes)
  @to_h = attributes.is_a?(Project) ? attributes.to_h : (attributes || {})
end

Instance Attribute Details

#to_hObject (readonly)

Returns the value of attribute to_h.



11
12
13
# File 'lib/multilocale/project.rb', line 11

def to_h
  @to_h
end

Class Method Details

.to_api(attributes) ⇒ Object

Snake_case keyword arguments -> the camelCase document the API stores. String keys are passed through untouched so anything not modelled here can still be sent.



87
88
89
90
91
92
93
# File 'lib/multilocale/project.rb', line 87

def self.to_api(attributes)
  return attributes.to_h if attributes.is_a?(Project)

  attributes.each_with_object({}) do |(key, value), document|
    document[ATTRIBUTE_NAMES[key] || key.to_s] = value
  end
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



62
63
64
# File 'lib/multilocale/project.rb', line 62

def ==(other)
  other.is_a?(Project) && other.to_h == to_h
end

#[](key) ⇒ Object



58
59
60
# File 'lib/multilocale/project.rb', line 58

def [](key)
  @to_h[key.to_s]
end

#contextObject

Free-text translation context, prepended to every machine translation.



40
41
42
# File 'lib/multilocale/project.rb', line 40

def context
  @to_h["context"]
end

#creation_timeObject



50
51
52
# File 'lib/multilocale/project.rb', line 50

def creation_time
  @to_h["creationTime"]
end

#default_localeObject



29
30
31
# File 'lib/multilocale/project.rb', line 29

def default_locale
  @to_h["defaultLocale"]
end

#hashObject



67
68
69
# File 'lib/multilocale/project.rb', line 67

def hash
  to_h.hash
end

#idObject



17
18
19
# File 'lib/multilocale/project.rb', line 17

def id
  @to_h["_id"]
end

#inspectObject



71
72
73
# File 'lib/multilocale/project.rb', line 71

def inspect
  "#<Multilocale::Project id=#{id.inspect} name=#{name.inspect} locales=#{locales.size}>"
end

#last_edit_timeObject



54
55
56
# File 'lib/multilocale/project.rb', line 54

def last_edit_time
  @to_h["lastEditTime"]
end

#localesObject

The complete locale list. Updating it REPLACES it: a project update that omits a locale removes it, it does not merge.



35
36
37
# File 'lib/multilocale/project.rb', line 35

def locales
  @to_h["locales"] || []
end

#nameObject



21
22
23
# File 'lib/multilocale/project.rb', line 21

def name
  @to_h["name"]
end

#organization_idObject



25
26
27
# File 'lib/multilocale/project.rb', line 25

def organization_id
  @to_h["organizationId"]
end

#pathsObject

Server-side download paths. The npm CLI resolves project.paths || config.paths, so a value set here wins over the local multilocale.json.



46
47
48
# File 'lib/multilocale/project.rb', line 46

def paths
  @to_h["paths"]
end