Class: MrMurano::SyncUpDown::Item

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/MrMurano/SyncUpDown-Item.rb

Overview

This is one item that can be synced.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Item

Initialize a new Item with a few, or all, attributes.

Examples:

Initializing with a Hash

Item.new(:name=>'Bob', :local_path => Pathname.new(

Initializing with an Item

item = Item.new(:name => 'get')
Item.new(item)

Parameters:

  • hsh (Hash{Symbol=>Object}, Item)

    Initial values



55
56
57
58
59
# File 'lib/MrMurano/SyncUpDown-Item.rb', line 55

def initialize(hash={})
  hash.each_pair do |key, val|
    self[key] = val
  end
end

Instance Attribute Details

#diffString

Returns If requested, the diff output.

Returns:

  • (String)

    If requested, the diff output.



35
36
37
# File 'lib/MrMurano/SyncUpDown-Item.rb', line 35

def diff
  @diff
end

#dup_countInteger

Returns Positive if multiple conflicting files found for same item.

Returns:

  • (Integer)

    Positive if multiple conflicting files found for same item.



45
46
47
# File 'lib/MrMurano/SyncUpDown-Item.rb', line 45

def dup_count
  @dup_count
end

#headerString

Returns The (optional) #ITEM <method> <path> :script header.

Returns:

  • (String)

    The (optional) #ITEM <method> <path> :script header.



33
34
35
# File 'lib/MrMurano/SyncUpDown-Item.rb', line 33

def header
  @header
end

#idObject

Read-only platform UUID, used to uniquely identify items. See also writable :alias property available on some items.



25
26
27
# File 'lib/MrMurano/SyncUpDown-Item.rb', line 25

def id
  @id
end

#line_begInteger

Returns The line in #local_path where this #script starts.

Returns:

  • (Integer)

    The line in #local_path where this #script starts.



29
30
31
# File 'lib/MrMurano/SyncUpDown-Item.rb', line 29

def line_beg
  @line_beg
end

#line_endInteger

Returns The line in #local_path where this #script ends.

Returns:

  • (Integer)

    The line in #local_path where this #script ends.



31
32
33
# File 'lib/MrMurano/SyncUpDown-Item.rb', line 31

def line_end
  @line_end
end

#local_pathPathname

Returns Where this item lives.

Returns:

  • (Pathname)

    Where this item lives.



22
23
24
# File 'lib/MrMurano/SyncUpDown-Item.rb', line 22

def local_path
  @local_path
end

#nameString

Returns The name of this item.

Returns:

  • (String)

    The name of this item.



20
21
22
# File 'lib/MrMurano/SyncUpDown-Item.rb', line 20

def name
  @name
end

#scriptString

Returns The Lua code for this item. (not all items use this.).

Returns:

  • (String)

    The Lua code for this item. (not all items use this.)



27
28
29
# File 'lib/MrMurano/SyncUpDown-Item.rb', line 27

def script
  @script
end

#selectedBoolean

Returns When filtering, did this item pass.

Returns:

  • (Boolean)

    When filtering, did this item pass.



37
38
39
# File 'lib/MrMurano/SyncUpDown-Item.rb', line 37

def selected
  @selected
end

#synckeyString

Returns The constructed name used to match local items to remote items.

Returns:

  • (String)

    The constructed name used to match local items to remote items.



39
40
41
# File 'lib/MrMurano/SyncUpDown-Item.rb', line 39

def synckey
  @synckey
end

#synctypeString

Returns The syncable type.

Returns:

  • (String)

    The syncable type.



41
42
43
# File 'lib/MrMurano/SyncUpDown-Item.rb', line 41

def synctype
  @synctype
end

#updated_atString

Returns The updated_at time from the server is used to detect changes.

Returns:

  • (String)

    The updated_at time from the server is used to detect changes.



43
44
45
# File 'lib/MrMurano/SyncUpDown-Item.rb', line 43

def updated_at
  @updated_at
end

Instance Method Details

#<=>(other) ⇒ Object



185
186
187
188
# File 'lib/MrMurano/SyncUpDown-Item.rb', line 185

def <=>(other)
  # rubocop:disable Style/RedundantSelf: Redundant self detected.
  self.to_h <=> other.to_h
end

#[](key) ⇒ Object

Get attribute as if this was a Hash

Parameters:

  • key (String, Symbol)

    attribute name

Returns:

  • (Object)

    The value



76
77
78
79
80
# File 'lib/MrMurano/SyncUpDown-Item.rb', line 76

def [](key)
  public_send(key.to_sym)
rescue NoMethodError
  nil
end

#[]=(key, value) ⇒ Object

Set attribute as if this was a Hash

Parameters:

  • key (String, Symbol)

    attribute name

  • value (Object)

    value to set



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/MrMurano/SyncUpDown-Item.rb', line 85

def []=(key, value)
  public_send("#{key}=", value)
rescue StandardError => err
  # DEVS: (lb): This isn't an error so much as whatever Item class this
  # is is missing some members (perhaps they were added recently to the
  # service, and the CLI has yet to be updated).
  if $cfg['tool.developer']
    MrMurano::Verbose.error(
      "Unable to set key: #{key} / value: #{value} / err: #{err} / self: #{inspect}"
    )
  end
end

#delete(key) ⇒ Object

Delete a key

Parameters:

  • key (String, Symbol)

    attribute name

Returns:

  • (Object)

    The value



101
102
103
104
# File 'lib/MrMurano/SyncUpDown-Item.rb', line 101

def delete(key)
  inst = as_inst(key)
  remove_instance_variable(inst) if instance_variable_defined?(inst)
end

#each_pair {|key, value| ... } ⇒ Item

Calls block once for each non-nil key

Yield Parameters:

  • key (Symbol)

    The name of the key

  • value (Object)

    The value for that key

Returns:



130
131
132
133
134
135
# File 'lib/MrMurano/SyncUpDown-Item.rb', line 130

def each_pair
  instance_variables.each do |key|
    yield as_sym(key), instance_variable_get(key)
  end
  self
end

#location_friendly(show_type: false, full_path: false) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
# File 'lib/MrMurano/SyncUpDown-Item.rb', line 190

def location_friendly(show_type: false, full_path: false)
  desc = location_path_and_line(full_path: full_path)
  desc += ' [phantom]' if defined?(@phantom) && @phantom
  return desc unless show_type
  unless @pp_desc.to_s.empty? || (@pp_desc == @synckey)
    desc += " (#{@pp_desc})"
  end
  # NOTE: This path is the endpoint path.
  desc += " [#{@method} #{@path}]" if (@method && @path)
  desc
end

#location_path_and_line(full_path: false) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/MrMurano/SyncUpDown-Item.rb', line 202

def location_path_and_line(full_path: false)
  if !defined?(@local_path) || @local_path.nil?
    @synckey
  else
    desc = @local_path
    desc = desc.relative_path_from(Pathname.pwd) unless full_path
    desc = desc.to_s
    if defined?(@line_beg) && !@line_beg.nil? && @line_beg > 0
      desc = "#{desc}:#{@line_beg}"
    end
    if defined?(@line_end) && !@line_end.nil? && @line_end > 0
      desc = "#{desc}-#{@line_end}"
    end
    desc
  end
end

#merge(item) ⇒ Item

A new Item containing our plus items.

Parameters:

Returns:

  • (Item)

    New item with contents of both



122
123
124
# File 'lib/MrMurano/SyncUpDown-Item.rb', line 122

def merge(item)
  dup.merge!(item)
end

#merge!(item) ⇒ Item

Adds the contents of item to self.

Parameters:

Returns:

  • (Item)

    ourself



114
115
116
117
# File 'lib/MrMurano/SyncUpDown-Item.rb', line 114

def merge!(item)
  item.each_pair { |k, v| self[k] = v }
  self
end

#reject {|key, value| ... } ⇒ Item

A new Item with keys deleted where block is true

Yield Parameters:

  • key (Symbol)

    The name of the key

  • value (Object)

    The value for that key

Yield Returns:

  • (Boolean)

    True to delete this key

Returns:

  • (Item)

    New Item with keys deleted



155
156
157
# File 'lib/MrMurano/SyncUpDown-Item.rb', line 155

def reject(&block)
  dup.reject!(&block)
end

#reject! {|key, value| ... } ⇒ Item

Delete items in self that block returns true.

Yield Parameters:

  • key (Symbol)

    The name of the key

  • value (Object)

    The value for that key

Yield Returns:

  • (Boolean)

    True to delete this key

Returns:

  • (Item)

    Ourself.



142
143
144
145
146
147
148
# File 'lib/MrMurano/SyncUpDown-Item.rb', line 142

def reject!(&_block)
  instance_variables.each do |key|
    drop = yield as_sym(key), instance_variable_get(key)
    delete(key) if drop
  end
  self
end

#reject_ephemeralObject

Scrub local, non-BizAPI/Pegasus attrs from Item (before uploading).



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/MrMurano/SyncUpDown-Item.rb', line 160

def reject_ephemeral
  reject do |attr_key, _|
    [
      # Writeable platform properties:
      #  :name,
      #  :script,
      # Read-only platform properties:
      :id,
      :updated_at,
      # Local-only attrs:
      :diff,
      :dup_count,
      :header,
      :line_beg,
      :line_end,
      :local_path,
      :selected,
      :synckey,
      :synctype,
    ].include? attr_key
  end
end

#to_hHash{Symbol=>Object}

Returns A hash that represents this Item.

Returns:

  • (Hash{Symbol=>Object})

    A hash that represents this Item



107
108
109
# File 'lib/MrMurano/SyncUpDown-Item.rb', line 107

def to_h
  Hash[instance_variables.sort.map { |k| [as_sym(k), instance_variable_get(k)] }]
end